samedi 4 janvier 2020

Compare two lists of different sObjects without double loops in Apex

I need to check a list of custom objects (instance), to find matches in another list of custom objects (template). A match is where the fields contain the same values, or value is null on the template. The existing code has a double loop like:

for(instance theInstance : theInstanceList){
    for(template thTemplate: theTemplateList){
        if((theInstanve.field1 = theTemplate.field1 || theTemplate.field1 == null) &&{
           (theInstanve.field2 = theTemplate.field2 || theTemplate.field2 == null) &&{
            and so on for 8-10 fields ....{
           ){
                Carry out requirements ........{
           }
     }
}

It's pretty inefficient when there are lots of records. Any thoughts on a nicer solution?

Aucun commentaire:

Enregistrer un commentaire