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
dselatdselat 

Apex trigger compare two lists and update records within a list

How to compare two lists in your trigger and update one of them so that you can do a single update. My trigger now has two update statements on Account object as below:

    if(!OppAccts.isEmpty()){update(OppAccts);}
    if(!CCPAccts.isEmpty()){update(CCPAccts);}

The reason for two different list is due to differeing criteria and logic that need to occur after update.

Any idea?



shruthishruthi

If the two lists OppAccts and CCPAccts contain different accounts [If, An account in OppAccts list does not appear with different values in CCPAccts], you can have a new list of accounts
List<Account> toUpdateAccts = new List<Account>();
toUpdateAccts.addAll(OppAccts);
toUpdateAccts.addAll(CCPAccts);
if(toUpdateAccts.size()>0) update toUpdateAccts;

dselatdselat

Thanks Shruthi. Then I will run into duplicates in the list if an account satisfies to be in both list.

vishal@forcevishal@force

I think the only way to compare two lists would be by looping through one, take a set or any collection and compare the values in that set by looping through the second list.