Search code examples
triggerssalesforceapexapex-code

Connecting Unrelated Objects during Apex Trigger


I am trying to create a trigger that does the following:

  1. Once an Account has been created, create an unrelated record (called a "Portal Content" record) that bears the same name, assuming the default RecordTypeId
  2. Take the ID of the newly created "Portal Content" record, and insert it into a lookup field on the originally created Account
  3. Add the ID of the original Account, and enter it into a field of the newly created "Portal Content" record

Steps 1 and 2 were addressed in post Populate Lookup Field with Record Created From Trigger. The new problem is that when attempting Item 3, in the Trigger.isAfter code block, a.Portal_Content_Record__r returns null rather than with the Id value populated after insert p in the Trigger.isBefore block.

trigger newAccountCreated on Account (before insert, after insert) {

    List<Account> 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;
            }
        }
    }
}

My question has two parts:

  1. How do I assign a field on the newly inserted Portal_Content__c record with the ID of the Account that started the trigger?
  2. Can this be done within this trigger, or is a secondary "helper" trigger needed?

Solution

  • I was able to figure out a way to answer this issue within the same Trigger. The Trigger.isAfter && Trigger.isInsert code block is what solved my problem.

    trigger newAccountCreated on Account (before insert, after insert, after delete) {
    
        List<Account> alist = Trigger.New;
        List<Account> oldlist = Trigger.old;
    
        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;
                }
            }
        }
        else if (Trigger.isAfter && Trigger.isDelete){
            for(Account a : oldlist){
                for(Portal_Content__c p : [SELECT ID FROM Portal_Content__c WHERE ID = :a.Portal_Content_Record__c]){
                    delete p;
                }
            }
    
        }
    
        if (Trigger.isAfter && Trigger.isInsert){
            for(Account a : alist){
    
                List<Portal_Content__c> plist = [SELECT ID FROM Portal_Content__c WHERE Id = :a.Portal_Content_Record__c];
    
                for(Portal_Content__c p : plist){
                    p.School_SFDC_ID__c = a.Id;
                    update p;
                }
    
            }
        }
    
    }
    

    This code block does a query for Portal_Contact__c records that match the Account record's Portal_Content_Record__c field value, which is assigned in the first code block. It then takes the Portal_Content__c records found, and assigns the original Account's Id to the record's School_SFDC_ID__c field value.