2

I have two lists for object OpportunityLineItem and Order_Line_Item__c,

List <OpportunityLineItem> opplinprodtlst = [SELECT product_id__c, CartItemGuid__c FROM OpportunityLineItem WHERE OpportunityId =:this.oppID];
List <Order_Line_Item__c>  ordlinprodtlst = [SELECT Product_ID__c, CartItemGuid__c FROM Order_Line_Item__c WHERE Opportunity_ID__c =:this.oppID];

I have to compare if combination of (product id , Cartitemguid) of opportunity line item is same as order line item or not .

Please help

2 Answers 2

3

Try below,

List <OpportunityLineItem> opplinprodtlst = [SELECT product_id__c, CartItemGuid__c FROM OpportunityLineItem WHERE OpportunityId =:this.oppID];
List <Order_Line_Item__c>  ordlinprodtlst = [SELECT Product_ID__c, CartItemGuid__c FROM Order_Line_Item__c WHERE Opportunity_ID__c =:this.oppID];
Set<String> setOrdKey = new Set<String>();

for(Order_Line_Item__c order : ordlinprodtlst){
   if(order.Product_ID__c!=null && order.CartItemGuid__c!=null){
         setOrdKey.add(order.Product_ID__c+''+order.CartItemGuid__c);
   }
}

for(OpportunityLineItem oli : opplinprodtlst){
     boolean isSame = false;
     if(oli.product_id__c!=null && oli.CartItemGuid__c!=null && setOrdKey.contains(oli.product_id__c+''+oli.CartItemGuid__c)){
         isSame=true;
     }
     System.debug('****is Same - '+isSame);
}
2
  • Did you edit your question and change it to the code from the answer by @Anup?
    – BarCotter
    Apr 2, 2015 at 7:55
  • No. You are getting me wrong. Both answers provide different results though he has used variable name,key construction & boolean as mine in first place.
    – snehakem
    Apr 2, 2015 at 8:04
1
List <OpportunityLineItem> opplinprodtlst = [SELECT product_id__c, CartItemGuid__c FROM OpportunityLineItem WHERE OpportunityId =:this.oppID];
List <Order_Line_Item__c>  ordlinprodtlst = [SELECT Product_ID__c, CartItemGuid__c FROM Order_Line_Item__c WHERE Opportunity_ID__c =:this.oppID];
map<string,OpportunityLineItem> opplinprodtMap = new map<string,OpportunityLineItem>();

for(OpportunityLineItem oli : opplinprodtlst){
    string oliKey = oli.product_id__c+oli.CartItemGuid__c;
    opplinprodtMap.put(oliKey,oli);
}

boolean isSame = false;
for(Order_Line_Item__c order : ordlinprodtlst){
   string orKey = order.Product_ID__c+''+order.CartItemGuid__c;
   if(opplinprodtMap.containsKey(orKey)){
       isSame=true;
   }
}
System.debug('****is Same - '+isSame);
3
  • Same answer is posted by #snehakem , You would have suggested Map use in comment instead of adding it in other developers answers code and making it yours . By the way some syntax are still wrong in your code. Apr 2, 2015 at 7:33
  • @Nilesh, Basically using nested for loop will degrade system performance. the map code written is getting encrypted after saving the comment and hence dropping some characters. so if you could help me out to format the comment, will be helpful.
    – Anup
    Apr 2, 2015 at 7:38
  • I have formatted your code by highlighting the code and clicking the "{}" button.
    – BarCotter
    Apr 2, 2015 at 7:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .