function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TheReportDoctorTheReportDoctor 

The relationship between Opportunity and OpportunityLineItem

In the following sample of code.  I want to find values from the fields in the Opportunity object.  This trigger is from the OpportunityLineItem.  The Apex Explorer says I should be able to address those fields as li.Opportunity.FieldName.  When I do that no matter what field I am looking at the come across NULL.  The tempation is to use "Opportunity__r", but that does not work. Any suggestions? 

 

 

trigger OpportunityFulfillmentItem on OpportunityLineItem (after insert, after update) {
    String opID;
    String liID;
    String pbID;
    String wsID;
    String newStatus;
    for(OpportunityLineItem li : trigger.new) {
   li.AddError('Hi Hugh: ' + trigger.new[0].Opportunity.CloseDate);
           if (li.Opportunity.Stage_of_Install__c == '7 - Pipeline') {
               opID = li.OpportunityId;
               liID = li.PricebookEntryId;

JimRaeJimRae

I don't believe the related objects are pulled through like that in a trigger, in an apex class for a controller they are.

My suggestion would be that you have to query for those values in the related object, and then reference them from a map.

 

 

trigger OpportunityFulfillmentItem on OpportunityLineItem (after insert, after update) { String opID; String liID; String pbID; String wsID; String newStatus; Set<ID> OLIOppIDs = new Set<ID>(); for(OpportunityLineItem oli : trigger.new) { OLIOppIDs.put(oli.OpportunityID); } Map<ID,Opportunity> OLIOppMAP = new Map<ID,Opportunity>([select id,closedate,Stage_of_Install__c from Opportunity where id in :OLIOppIDs]); for(OpportunityLineItem li : trigger.new) { li.AddError('Hi Hugh: ' + OLIOppMAP.get(li.Opportunityid).CloseDate); if (OLIOppMAP.get(li.Opportunityid).Stage_of_Install__c == '7 - Pipeline') { opID = li.OpportunityId; liID = li.PricebookEntryId;

 

 

 

jeffdonthemic2jeffdonthemic2

I actually just blogged about this yesterday. See...

 

Relationship Lookup Objects in Triggers are NULL?

 

HTH

 

Jeff Douglas
Appirio, Inc.
http://blog.jeffdouglas.com