You need to sign in to do that
Don't have an account?
isalew
Dot Notation Query Returning Null
I'm building a trigger handler that references multiple objects to update the OpportunityLineItem object. I want to use dot notation as much as possible to eliminate SOQL queries.
The following loop shows an example of one cross-object reference that returns null, but I do not understand why:
The following cookbook recipe (http://developer.force.com/cookbook/recipe/using-relationship-queries) suggests I can access these fields with dot notation:
Why am I unable to access this field with dot notation? How would I write things differently?
Thank you all for your help!
The following loop shows an example of one cross-object reference that returns null, but I do not understand why:
for(OpportunityLineItem oli :olis){ System.debug('Revenue Type: ' + oli.PriceBookEntry.Revenue_Type__c); }
The following cookbook recipe (http://developer.force.com/cookbook/recipe/using-relationship-queries) suggests I can access these fields with dot notation:
SELECT Id, Name, Account.Name, (SELECT Quantity, UnitPrice, TotalPrice, PricebookEntry.Name, PricebookEntry.Product2.Family FROM OpportunityLineItems) FROM Opportunity
Why am I unable to access this field with dot notation? How would I write things differently?
Thank you all for your help!
Is likely throwing an NPE because the data simply doesn't exist in the trigger context. The trigger context will contain non-formula fields and also Ids of parent relationships (like PriceBookEntryId in your case), but you will not able to access cross object fields.
What you need to do is requery for the Opportunity Line Items or PriceBookEntries to get the fields you need.
The following would work in an after insert or before update / after update trigger (Trigger.newMap is not available with before insert triggers)
Otherwise, if this is for a before insert trigger, you'll have to query the PriceBookEntry object and compare it using the OpportunityLineItem's PriceBookEntryId relationship(s).
Hope that helps.
All Answers
In order to do this with dot notation you'll need to query from product2 and even then I'm not sure it'll work.
Is likely throwing an NPE because the data simply doesn't exist in the trigger context. The trigger context will contain non-formula fields and also Ids of parent relationships (like PriceBookEntryId in your case), but you will not able to access cross object fields.
What you need to do is requery for the Opportunity Line Items or PriceBookEntries to get the fields you need.
The following would work in an after insert or before update / after update trigger (Trigger.newMap is not available with before insert triggers)
Otherwise, if this is for a before insert trigger, you'll have to query the PriceBookEntry object and compare it using the OpportunityLineItem's PriceBookEntryId relationship(s).
Hope that helps.
I went with a before trigger scenario, so I queried the PriceBookEntry object and used a Map to compare the records.