+ Start a Discussion

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:


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

Any idea?


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>();
if(toUpdateAccts.size()>0) update toUpdateAccts;


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


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.