+ Start a Discussion
Linda 98Linda 98 

Error:Initial term field expression of type List:

Please help me in making the code work for multiple records.

Ii am having two queries ..I wrote where condition as xyz[0].abc.Xyz is another list.

Here are my queries:

List<Opportunitylineitem> Opplinitemlist =[select Id,PricebookEntry.Product2.Name,PricebookEntry.Product2.id,
from Opportunitylineitem  where Opportunityid IN :Trigger.newMap.keySet() and Acc__c =''];
    if(opplinitemlist.size()>0)       
 List<acc__c>   query=[select id,name,serfvice date  from Acc__c  where Acc__c.product__c=:opplinitemlist[0].PricebookEntry.Product2.id and Acc__c.Account__c =: Opportunitylist[0].accountid ];

I am aware that i have to use for loop and run through each record.but i am getting error:INitiall term field expression.

Pls help

 
Best Answer chosen by Linda 98
Sumitkumar_ShingaviSumitkumar_Shingavi
You will do it like below:
//Take 2 set variables for collecting Ids
Set<Id> sProductIds = new Set<Id>();
Set<Id> sAccountIds = new Set<Id>();

//Create Set of Ids
for(OpportunityLineItem oli : [SELECT Id, PricebookEntry.Product2.Name,PricebookEntry.Product2.id FROM OpportunityLineItem WHERE Opportunityid IN :Trigger.newMap.keySet() AND Acc__c='']) {
	sProductIds.add(oli.PricebookEntry.Product2.id);
	sAccountIds.add(oli.Accountid);
}

//Use Set variables for fetch required Acc__c
List<Acc__c> lAccs = [SELECT Id, Name FROM Acc__c WHERE Acc__c.product__c IN :sProductIds and Acc__c.Account__c IN :sAccountIds];
Hope this helps!

All Answers

James LoghryJames Loghry

I believe the [0] might be throwing your query off.  Try setting opplinitemlist[0].Pricebookentry.product2.Id to it's own variable, and then use that in the binding instead.  For instance:

Id productId = opplinitemlist[0].PricebookEntry.Product2.Id;

query = [Select .... Where Acc__c.Product__c = :productId];

But please update your code to handle bulkification and don't throw your SOQL in a for loop.
Sumitkumar_ShingaviSumitkumar_Shingavi
Try below code:
List<Opportunitylineitem> Opplinitemlist = [SELECT Id, PricebookEntry.Product2.Name,PricebookEntry.Product2.id FROM OpportunityLineItem WHERE Opportunityid IN :Trigger.newMap.keySet() AND Acc__c=''];

if(!opplinitemlist.isEMpty()) {
	List<Acc__c> lAccs = [SELECT Id, Name FROM Acc__c WHERE Acc__c.product__c = :opplinitemlist[0].PricebookEntry.Product2.id and Acc__c.Account__c =: Opportunitylist[0].accountid];
}
PS; Mark it as solution if problem solved!
Linda 98Linda 98
But if i use opplistitemlist[0] then it will only work for one record right??

How can i make it work for multiple records...
Sumitkumar_ShingaviSumitkumar_Shingavi
You will do it like below:
//Take 2 set variables for collecting Ids
Set<Id> sProductIds = new Set<Id>();
Set<Id> sAccountIds = new Set<Id>();

//Create Set of Ids
for(OpportunityLineItem oli : [SELECT Id, PricebookEntry.Product2.Name,PricebookEntry.Product2.id FROM OpportunityLineItem WHERE Opportunityid IN :Trigger.newMap.keySet() AND Acc__c='']) {
	sProductIds.add(oli.PricebookEntry.Product2.id);
	sAccountIds.add(oli.Accountid);
}

//Use Set variables for fetch required Acc__c
List<Acc__c> lAccs = [SELECT Id, Name FROM Acc__c WHERE Acc__c.product__c IN :sProductIds and Acc__c.Account__c IN :sAccountIds];
Hope this helps!
This was selected as the best answer
Linda 98Linda 98
thank you james and sumit..