+ Start a Discussion
Sunil_sfcd803Sunil_sfcd803 

Fetch map values of related objects into if()?

I need help.I'm trying query records from three objects which are releted and use on particular field in if().See below code Im getting error 
Error: Compile Error: Variable does not exist: Product2 at line 39 column 86..Please help.
Map<id,Quote> QuoteProductMap=new Map<Id,Quote>([Select id,recordTypeId,Status,(Selectid,Product2.Name,Product2.Additional_Spe__c FROM QuoteLineItems)from Quote where Id in:newItems.keyset()]);
 
          for(Quote quoteToProduct:newItems.values()){
if(QuoteProductMap.get(quoteToProduct.id).QuoteLineItems.Product2.Additional_Spe__c== listProductAttribute[0].Additional_spe__c)
                            {
                              quoteToProduct.sp_Products__c=true;
                               listQuoteProduct.add(quoteToProduct);       
                            }
}

 
v varaprasadv varaprasad
Hi Sunil,

Space is missing between id,product2
 
Map<id,Quote> QuoteProductMap=new Map<Id,Quote>([Select id,recordTypeId,Status,(Select id,Product2.Name,Product2.Additional_Spe__c FROM QuoteLineItems)from Quote where Id in:newItems.keyset()]);

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 
Raghavendra Raj K SRaghavendra Raj K S
Hi Sunil,

Check the below code this might be helpful.
 
Map<id,List<QuoteLineItem>> QuoteProductMap = new Map<Id,List<QuoteLineItem>> ();

  List<QuoteLineItem> quoteLineItemsList = [Select id, QuoteId, Product2.Name, Product2.Additional_Spe__c FROM QuoteLineItem WHERE QuoteId IN : newItems.keyset()];
        
        for(QuoteLineItem qItem : quoteLineItemsList) {
            if(QuoteProductMap.containsKey(qItem.QuoteId)) {
                QuoteProductMap.get(qItem.QuoteId).add(qItem);
            }else {
                QuoteProductMap.put(qItem.QuoteId, new List<QuoteLineItem>{qItem});        
            }
        }
        
        
        for(Id quoteId : QuoteProductMap.keyset()){
            for(QuoteLineItem  qItem :  QuoteProductMap.get(quoteId)) {
               if(qItem.Product2.Additional_Spe__c == listProductAttribute[0].Additional_spe__c) {
                  qItem.sp_Products__c=true;
                  listQuoteProduct.add(qItem);   
               }             
            }       
        }