You need to sign in to do that
Don't have an account?

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:
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.
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.
There is not direct function to compare two list but indirectly you can do it as follows: Thanks
Banwari
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.
Hope This helps!!!
Thanks,
Gaurav
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.
Thanks
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
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