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
Abhishek Prasad 8Abhishek Prasad 8 

How to compare two list values - Apex

HI Friends,
I am new to Apex. I am facing this challenege where i want to compare fields of two different lists. Here is the pseudo code:
 
Opportunity Opp = ([select, id, Site__C from Opportunity where id = Opp.id]); // Opp.id passed from the calling method

List<Contract__c> Contra= new List <Contract__c>([select, id, Site__c from Contract__c where Opportunity__c = Opp.id]);

List<Deal__c> Deals= new List <Contract>([select, id, Site__c from Deal__c where Opportunity__c = Opp.id]);

for(Contract__c Con:Contra){
       for(Deal__c D: Deals){
              if(Con.Site__c == D.Site){
                 
                        //PUT THE VALUE IN LIST FOR UPDATE

                      }
              else
                      {
                            //PUT THE VALUE IN LIST FOR INSERT
                          }

       }
}

Now, this code is not perfect. But i am using two for loops, which i want to avoid. What should be the best way to achieve this (comparing two list values)? If possible, please provide some code snippent.

Thanks in advance.
Banwari kevat1Banwari kevat1
Hi Abhisek,
There is not direct function to compare two list but indirectly you can do it as follows:
List<Account> firstList;
List<Account> secondList;

Map<id, Account> firstMap = new Map<id, Account>(firstList);
Map<id, Account> secondMap = new Map<id, Account>(secondList);

If( firstMap.keySet().contailsAll(secondMap.keySet()) && secondMap.keySet().contailsAll(firstMap.keySet())  ){
 System.debug('List are equals');
}
else{
System.debug('List are not equals');

}
Thanks
Banwari
 
GauravGargGauravGarg
Hi Abhishek,

Salesforce has a concept of Map in salesforce which store value with a ID or unique identifier:
Map<Id, Value> which allow us to easily find the actual record. 
 
Opportunity Opp = ([select, id, Site__C from Opportunity where id = Opp.id]); // Opp.id passed from the calling method

List< Contract__c> contraList = new List< Contract__c>();
for(Contract__c c: [select, id, Site__c from Contract__c where Opportunity__c = Opp.id]) {
         contraList.add(c);
 }

List<Deal__c> dealList = new List< Deal__c>();
for(Deal__c d: [select, id, Site__c from Deal__c where Opportunity__c = Opp.id]) {
       dealList.add( d);
}

for(Integer i = 0; i < contraList.size() ; i++){
     if(contraList != null && contraList[i].site__c != null && dealList != null && dealList[i].site___c != null){
         if(contraList[0].site__c == dealList[i].site__c){
               // update list 
        }else {}   
    }
}
}

Hope This helps!!!

Thanks,
Gaurav
 
Abhishek Prasad 8Abhishek Prasad 8
@GauravGarg
Hey Gaurav,
Thanks for the reply man. Really appreciate it.
Just one question:
in Line 15 of your code, you are taking contraList[0]. But my contraList can have multiple Sites attached to it. but here, isnt it just comparing the 1st siteof Contract with all the sites on deal. is it a typo or am i missing something?
Please help me on this...
Thanks in advance.
GauravGargGauravGarg
oops I missed, Please change "0" with  "i"

Thanks
Abhishek Prasad 8Abhishek Prasad 8
@GauravGarg
Hey Gaurav,
Again at line 15, i am getting list out of bound : 1 error.
the reason being: COntract has 2 sites, and deal has only one. that second site i need to copy in the deal, provided, its not already present on the deal. Am i making my self clear?

Please have a look
GauravGargGauravGarg
Hi Abhishek,

Could you repost the new code, also which field you want to compare and which field is going to copy to another object. Could you elaborate this. 

Thanks,
Gaurav
skype: gaurav62990