We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20215
    • 144 Posts
    Unable to create url of a newly resource created by code

    .......
    $doc->save();
    $doc->joinGroup((int) $groupId);
    
    $modx->cacheManager->refresh(); //makes no difference
    
    $idDOC = $doc->get('id');
    $contextDoc = $doc->get('context_key');
    
    $modx->switchContext($contextDoc);                          
    $docURL = $modx->makeUrl($idDOC,$contextDoc,'','full'); //does not work
    $modx->switchContext('mgr');
                                
    $docName = $doc->get('pagetitle');                
                                
    $mailSubject = "New Doc";
    $mailSender = '[email protected]';
    $mailFromName = 'xxxx';
    $mailFrom = '[email protected]';
    $mailTo = $userX['email'];
    $cFields['userRagS'] = $userX['fullname'];
    $cFields['docName'] = $docName;
    $cFields['docID'] = $idDOC;
    $cFields['tipoDoc'] = $value['nomeRes'];
    $Cmessage = $modx->getChunk('tplMailNewDoc', $cFields);
    ....
    


    chunk tplMailNewDoc:
    [[+docID]] - [[!makeurlX? &idDOCx=`[[+docID]]`]] - [[~[[+docID]]? &scheme=`full`]] --> 334 - - (returns only [[+docID]])

    snippet makeurlX
    $doc = $modx->getObject('modResource', $idDOCx);
    $contextDoc = $doc->get('context_key');
    $modx->switchContext($contextDoc);
    $docURLx = $modx->makeUrl($idDOCx,$contextDoc,'','full');
    return $docURLx;
    


    How could I solve?

    This question has been answered by BobRay. See the first response.

      ----------------------------------
      canale irc Italiano #modx server: tophost.azzurra.org
    • discuss.answer
      • 3749
      • 24,544 Posts
      Most likely it's because the new document is not in the document map yet. Try putting this right after the save() line:

      $modx->reloadContext($contextDoc);


      If that doesn't work, try hard-coding the context name like this:

      $modx->reloadContext('web');

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 20215
        • 144 Posts
        works
        $modx->reloadContext($contextDoc);


        Thx Bob!
          ----------------------------------
          canale irc Italiano #modx server: tophost.azzurra.org
          • 3749
          • 24,544 Posts
          I'm glad it worked for you. Thanks for reporting back (first thing that's gone right for me today). wink
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting