Skip to main content
2 of 3
added 428 characters in body
pjcarly
  • 7.5k
  • 5
  • 37
  • 56

If I understand you correctly, you want to check if values exists in lists,

Since you have a unique name, it's best to leverage that uniqueness in combination with a map, you start of by adding all the values in your lists to 2 maps.

Map<String, Obj__c> objMap1 = new Map<String, Obj__c>();
Map<String, Obj__c> objMap2 = new Map<String, Obj__c>();

// List 1
for(Obj__c o : list1)
{
    objMap1.put(o.UniqueName__c, o);
}

// List 2
for(Obj__c o : list2)
{
    objMap2.put(o.UniqueName__c, o);
}

// Now you can easily check if that value is in your map by doing.
objMap1.containsKey('WhateverYouWantToCompare');
// or
objMap2.containsKey(newObj.UniqueName__c);

// ... do your logic

Here is the documentation for Maps and Sets try to use this as much as possible to reduce the number of script statements you have for manually looping over lists just to check if it exists or not.

pjcarly
  • 7.5k
  • 5
  • 37
  • 56