title: Connecting Unrelated Objects during Apex Trigger
body: <p>I am trying to create a trigger that does the following:</p>
  
  <ol>
  <li>Once an Account has been created, create an unrelated record (called a "Portal Content" record) that bears the same name, assuming the default RecordTypeId</li>
  <li>Take the ID of the newly created "Portal Content" record, and insert it into a lookup field on the originally created Account</li>
  <li>Add the ID of the original Account, and enter it into a field of the newly created "Portal Content" record</li>
  </ol>
  
  <p>Steps 1 and 2 were addressed in post <a href="https://stackoverflow.com/questions/54877311/populate-lookup-field-with-record-created-from-trigger">Populate Lookup Field with Record Created From Trigger</a>. The new problem is that when attempting Item 3, in the <code>Trigger.isAfter</code> code block, <code>a.Portal_Content_Record__r</code> returns null rather than with the Id value populated after <code>insert p</code> in the <code>Trigger.isBefore</code> block.</p>
  
  <pre><code>trigger newAccountCreated on Account (before insert, after insert) {
  
      List&lt;Account&gt; alist = Trigger.New;
  
      if(Trigger.isBefore){
  
          for(Account a : alist) {
  
              if (a.RecordTypeId == '012i0000001Iy1H') {
                  Portal_Content__c p = new Portal_Content__c(
                      Name=a.Name,
                      RecordTypeId='012i0000001J1zZ'
                  );
                  insert p;
  
                  a.Portal_Content_Record__c = p.Id;
                  system.debug('Made it to insert p. P = ' + p.Id +'. a.Portal_Content_Record__c = ' + a.Portal_Content_Record__c);                
              }
          }
      }
  
      if (Trigger.isAfter) {
          for(Account a : alist){
              system.debug('a.Id = ' + a.Id + ', p = ' +a.Portal_Content_Record__r);
              String p = a.Portal_Content_Record__c;
              for(Portal_Content__c port : [SELECT ID FROM Portal_Content__c WHERE Id = :p]){
                  port.School_SFDC_ID__c = a.Id;
                  update port;
              }
          }
      }
  }
  </code></pre>
  
  <p>My question has two parts:</p>
  
  <ol>
  <li>How do I assign a field on the newly inserted Portal_Content__c record with the ID of the Account that started the trigger?</li>
  <li>Can this be done within this trigger, or is a secondary "helper" trigger needed?</li>
  </ol>