1

I'm getting the following error:

Save error: Initial term of field expression must be a concrete SObject: LIST Force.com save problem

Here are the snippets of code in question:

List <PricebookEntry> newProdsList = [SELECT ID //Creating a new PricebookEntry list with all relevant information
                                        ,ProductCode
                                        ,UnitPrice
                                        ,Product2.Description
                                    FROM
                                        PricebookEntry 
                                    WHERE 
                                        ProductCode in: productCodes
                                        AND
                                        Pricebook2Id in: Pbook2Id.Id
                                ];

The error is ocurring on the line Pricebook2Id in: Pbook2Id.Id.

Here is what Pbook2Id looks like:

List<Pricebook2> Pbook2Id = [SELECT Name
                            ,ID 
                                FROM Pricebook2 
                                    WHERE Name='Support Renewal' AND IsActive=true];

My question is, what does this error mean? I've looked at other such errors here and I'm not sure I fully understand why it's being caused.

1 Answer 1

1

Pbook2Id is a collection, so it doesnt have a field called Id.

The dot notation to access a field can only be used on an sObject (you can access static / instance variables using Class / Object names too)

In summary,

AND
PriceBook2Id IN : Pbook2Id

This works in Apex for an ID Field, if it didn't you'd have to construct a List for comparison

List<Id> PriceBookIds = new List<Id>();
for(PriceBook2 pb : Pbook2Id) //iterate over the list and collect ids for comparison
PriceBookIds.add(pb.Id);

and then use

PriceBook2Id IN :PriceBookIds

You can also cast to a Map dynamically and use the keySet for comparison

Map<Id, Pricebook2> Pbook2Id = new Map<Id, PriceBook2>([SELECT Id, Name
                                FROM Pricebook2 
                                    WHERE Name='Support Renewal' AND IsActive=true]);

And then Use

PriceBook2Id IN :Pbook2Id.keySet()
2
  • but since Pbook2Id has two columns (Name and ID), shouldn't i need to specify which one of those columns I want it to match with? Or does apex know to look only at the ID column of the list? Your answer worked completely but I just don't understand the logic behind it. Thanks for your help!
    – SSadat
    Commented Jul 4, 2013 at 14:23
  • It understands when used with an Id, I don't believe you'd be able to do it with a different field, for e.g. name. If it didnt, you've have to iterate over PBook2Id and add the Ids to a List<Id> and then use an IN on the List<Id> Commented Jul 4, 2013 at 14:26

You must log in to answer this question.

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