[SalesForce] Need a Update trigger to fire after record insert

I had formula fields on my custom object, but the Formula got too big so Salesforce wouldn't let me save it. It went Formula Field to Formula Field to Formula Field.

So I am trying to create one of the Formulas in APEX by updating a Custom field that is in the middle of the set – so it now goes Formula Field to Custom Field to Formula Field. But I can't get the value of the 1st Formula Field in a Before Insert trigger since it doesn't exist.

Is there a way to make a trigger fire an Update trigger after the insert? Or is there a way to run a Before trigger inside an After trigger? The trigger I have works great when you Edit and Save the record… but I need it to work right when it's created.

Code that works for update… need it to work for insert:

trigger ClientCare_UpdateClient on Submitted_Documents__c (before update) {

List<ID> RequireContactId = new  List<ID>() ;

for(Submitted_Documents__c  SD : trigger.new) 
{
    if(SD.Contact_ID__c != null && SD.RecordTypeId == '012J00000004aQ4'){ 
        RequireContactId.add(SD.Contact_ID__c);
    }    
}

Map<ID,Contact> ContactData = new Map<Id,Contact>([Select Minor__c,Program__c, ID from Contact where id in : RequireContactId limit 1000 ]);

        Map<Id,Submitted_Documents__c> newSDMap = Trigger.newMap;
        for(Id SDId:newSDMap.keySet()){
            Submitted_Documents__c myNewSD = newSDMap.get(SDId);
            Contact theContact1 = ContactData.get(myNewSD.Contact_ID__c);
               if ((theContact1.Program__c == 'Open Doors' && myNewSD.TT_NUM__c == 10) || (theContact1.Minor__c == TRUE && myNewSD.TT_NUM__c == 12) || (theContact1.Program__c == 'Project HEAL' && myNewSD.TT_NUM__c == 11)){
                   myNewSD.All_Received_Client_Graduate2__c = TRUE;
                   if(theContact1.Program__c == 'Open Doors'){
                       myNewSD.Received_Client_Graduate2__c = myNewSD.TT_NUM__c + ' out of 10.';
                   }else if(theContact1.Minor__c == TRUE){
                       myNewSD.Received_Client_Graduate2__c = myNewSD.TT_NUM__c + ' out of 12.';                   
                   }else if(theContact1.Program__c == 'Project HEAL'){
                       myNewSD.Received_Client_Graduate2__c = myNewSD.TT_NUM__c + ' out of 11.';                   
                   }
               }else{
                   myNewSD.All_Received_Client_Graduate2__c = FALSE;
                   if(theContact1.Program__c == 'Open Doors'){
                       myNewSD.Received_Client_Graduate2__c = myNewSD.TT_NUM__c + ' out of 10.';
                   }else if(theContact1.Minor__c == TRUE){
                       myNewSD.Received_Client_Graduate2__c = myNewSD.TT_NUM__c + ' out of 12.';                   
                   }else if(theContact1.Program__c == 'Project HEAL'){
                       myNewSD.Received_Client_Graduate2__c = myNewSD.TT_NUM__c + ' out of 11.';                   
                   }
               }
        }        

}

Best Answer

Members of Trigger.new are read only in the after cycle.

In order to take action on an record in Trigger.new after insert/update, you need to create a new instance of each sObject record you want to work with and add it to a new list for update purposes.

...assuming after insert trigger on account...
List<Account> accts = new List<Account>();
for (Account a: Trigger.new) {

  if (some condition) {
    ...take action on a...
    Account acctToUpdate = new Account(id=a.Id, ...other updates...);
    accts.add(acctToUpdate);
  }
}

update accts; 

The only thing here is if you also do this on update, you will run into a recursive trigger problem, but this is easily remedied by using the recursive trigger pattern found here in this cookbook recipe.

Related Topic