function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sandra OSandra O 

Help with trigger - only updates after edit/save

I have a trigger on the Account - I am trying to trigger an upadte on the Account to do an update on related Integration Account records.  However, the update is only applied after I do an edit/save on indivudual Interation Account records.

this is what I have so far:
 
trigger UpdateIA on Account (after update) 
    {
        Integer index = 0;       
        for(Account acc : Trigger.New)
            {
                //Check entry criteria
                if(acc.Complexity__c != Trigger.Old[index].Complexity__c)                
                    {
                         
                         List<Integration_Account__c> listIA = [Select Complexity_roll_up__c from Integration_Account__c where Integration_Account__c.Account_ID__c =: acc.id];      
                
                           for(Integration_Account__c ia : listIA)
                      
                             {
                                 ia.Complexity_Roll_Up__c = acc.Complexity__c;
                             }
                         update listIA;    
                    }
                index++;
            }
    }

 
Best Answer chosen by Sandra O
Sandra OSandra O
Mithun,

I was able to accomplish this functionality with Flow and a Flow trigger.   THanks for you assistance.

All Answers

MithunPMithunP
Hi Sandra,

Since we have governer limits in salesforce, we shouldn't write soql queries inside FOR loops. I have updated your trigger, you can use this.
 
trigger UpdateIA on Account (after update) 
    {
    list<Integration_Account__c> intaccsList = [Select Complexity_roll_up__c,Account_ID__c from Integration_Account__c where Integration_Account__c.Account_ID__c in : trigger.newmap.keyset()];
    Map<id,list<Integration_Account__c>> accIntMap = Map<id,list<Integration_Account__c>>();
    for(Account acc : Trigger.New){
         list<Integration_Account__c> tempList = new list<Integration_Account__c>();
         for(Integration_Account__c int :intaccsList ){
              if(acc.id == int.Account_ID__c){
                   tempList.add(int);
              }
         }
         accIntMap.put(acc.id,tempList );
    }
    list<Integration_Account__c> updateList = new list<Integration_Account__c>();
        Integer index = 0;       
        for(Account acc : Trigger.New)
            {
                //Check entry criteria
                if(acc.Complexity__c != Trigger.Old[index].Complexity__c)                
                    {
                          list<Integration_Account__c> secTempList =  accIntMap.get(acc.id);
                         
                            for(Integration_Account__c ia : secTempList)
                      
                             {
                                 ia.Complexity_Roll_Up__c = acc.Complexity__c;
                                 updateList.add(ia);
                             }
                           
                    }
                index++;
            }
            update updateList;
    }

Best Regards,
Mithun.
Sandra OSandra O
HI Mithun,

THank you for your help!  I am getting the following error:

[Error] Error: Compile Error: unexpected token: 'Map' at line 5 column 4
MithunPMithunP
Hi Sandra,

Sorry, we had missed the "New" keyword in map definition. Here is the updated one.
 
trigger UpdateIA on Account (after update) 
    {
    list<Integration_Account__c> intaccsList = [Select Complexity_roll_up__c,Account_ID__c from Integration_Account__c where Integration_Account__c.Account_ID__c in : trigger.newmap.keyset()];
    Map<id,list<Integration_Account__c>> accIntMap = new  Map<id,list<Integration_Account__c>>();
    for(Account acc : Trigger.New){
         list<Integration_Account__c> tempList = new list<Integration_Account__c>();
         for(Integration_Account__c int :intaccsList ){
              if(acc.id == int.Account_ID__c){
                   tempList.add(int);
              }
         }
         accIntMap.put(acc.id,tempList );
    }
    list<Integration_Account__c> updateList = new list<Integration_Account__c>();
        Integer index = 0;       
        for(Account acc : Trigger.New)
            {
                //Check entry criteria
                if(acc.Complexity__c != Trigger.Old[index].Complexity__c)                
                    {
                          list<Integration_Account__c> secTempList =  accIntMap.get(acc.id);
                         
                            for(Integration_Account__c ia : secTempList)
                      
                             {
                                 ia.Complexity_Roll_Up__c = acc.Complexity__c;
                                 updateList.add(ia);
                             }
                           
                    }
                index++;
            }
            update updateList;
    }

Best Regards,
Mithun.
Sandra OSandra O
Mithun,

This is working - but its only updating after a update an individual Integration account record and hit edit/make no changes/save.
Sandra OSandra O
Mithun,

I was able to accomplish this functionality with Flow and a Flow trigger.   THanks for you assistance.
This was selected as the best answer