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
Rajesh MetiRajesh Meti 

Null Pointer exception when retrieving PriceBookEntryId from OpportunityLineItem on before update event

Hi,

I am getting null pointer exception while retrieving PricebookEntry value from OpportunityLineItem and code details are below:

I am getting error for the venet if(Trigger.isBefore && Trigger.isUpdate)  and it was working fine eralier sudeenly satrted throwing error today:

Trigger:
trigger OpportunityLineItemTriggers on OpportunityLineItem (after insert, 
			after update, after delete ,before insert, before update ) {

	OLIClass OLineItemClass  = new OLIClass();

	if(Trigger.isBefore && Trigger.isInsert) {    
		OLIClass.updateOppLineItem_Field(Trigger.New);       
	}

	if(Trigger.isBefore && Trigger.isUpdate) {    
		OLIClass.updateOppLineItem_Field(Trigger.New);  
	}
}

Apex Class:
public without sharing class OLIClass {
	// Function to copy Offer Field Value from Product to OpportunityLineItem  P
	public void updateOppLineItem_Field(List<OpportunityLineItem> newList) {
		Set<Id> PBEIds = new Set<Id>();

		for(OpportunityLineItem oli: newList) {        
			PBEIds.add(oli.PricebookEntryId);
		}

		Map<ID,PricebookEntry> pbeMap = new Map<ID,PriceBookEntry>([Select p.Product2.Offer__c, p.Product2Id, p.Id From PricebookEntry p where ID IN : PBEIds]);
		for(OpportunityLineItem oli: newList) {
			oli.Offer__c  = map_PBE.get(oli.PricebookEntryId).Product2.Offer__c;
		}
	}
}

Can any one please help me to overcome this error.

Regards,
Rajesh Meti
James LoghryJames Loghry
Which line is your NPE (Null pointer exception) coming from?

oli.Offer__c = map_PBE.get(oli.PricebookEntryId).Product2.Offer__c;

If it's the above line, it's likely that either your map_PBE map is null (I see a pbeMap, but no map_PBE, so curious to how that compiles), or your Opportunity Line Item doesn't have any pricebookentryid associated with it.
Rajesh MetiRajesh Meti
Thanks James for quick reply. 

Sorry its pbeMap and corrected class is below and this is working code suddenly its started throwing exception today for event if(Trigger.isBefore && Trigger.isUpdate) but the same works fine for event if(Trigger.isBefore && Trigger.isInsert).

Apex Class:
public without sharing class OLIClass {
	// Function to copy Offer Field Value from Product to OpportunityLineItem  P
	public void updateOppLineItem_Field(List<OpportunityLineItem> newList) {
		Set<Id> PBEIds = new Set<Id>();

		for(OpportunityLineItem oli: newList) {        
			PBEIds.add(oli.PricebookEntryId);
		}		
		
		Map<ID,PricebookEntry> pbeMap = new Map<ID,PriceBookEntry>([Select p.Product2.Offer__c, p.Product2Id, p.Id From PricebookEntry p where ID IN : PBEIds]);
		for(OpportunityLineItem oli: newList) {
			oli.Offer__c  = pbeMap.get(oli.PricebookEntryId).Product2.Offer__c;
		}
	}
}
Regards,
Rajesh Meti

Rajesh MetiRajesh Meti
It works fine if I  change for loop query from for(OpportunityLineItem oli: newList)
to  
for(OpportunityLineItem oli: [Select Id, Offer__c,PricebookEntryId from OpportunityLineItem Where Id in :newList])

I wanted to know why it is not working for previous where it use to work earlier??

Can any one pknow why it is that for before Update event??

Thanks & Regards,
Rajesh Meti
James LoghryJames Loghry
Interesting.  With the SOQL query in your for loop working, this implies to me that at somepoint in the update, the PriceBookEntryId is being updated, probably in a separate process like another trigger.  Can you check all your workflow rule / field updates and triggers and make sure they are not effecting your Opportunity Line Item?  You could also check in your trigger by using System.debug('Is PBE ID the same?' + Trigger.oldMap(oli.Id).PricebookEntryId == oli.PriceBookEntryId);
Rajesh MetiRajesh Meti
Hi James,

I have not done any changes to system almost from last 1 month. I was working with same code yesterday but suddenly today :-)

I have debugged as per your suggestion and result is bleow:
System.debug('Is PBE ID the same?' + Trigger.oldMap(oli.Id).PricebookEntryId == oli.PriceBookEntryId);
FATAL_ERROR|System.StringException: Invalid id: Is PBE ID the same?01uK00000045RxxIAE

System.debug('Is PBE ID the same?' + Trigger.oldMap(oli.Id).PricebookEntryId +'=='+ oli.PriceBookEntryId);
16:41:38.982 (982606495)|USER_DEBUG|[45]|DEBUG|Is PBE ID the same?01uK00000045RxxIAE==null

Thanks & Regards,
Rajesh Meti
Jesper KristensenJesper Kristensen
I have the same error. Starting this week the PricebookEntryId field is no longer populated in a Before Update trigger context on OpportunityLineItem in my sandboxes on CS17. It seems like Winter 15 was deployed there this weekend.
Enara MarcosEnara Marcos
We have the same problem: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AiIcIAK

Since Winter 15 was developed (this monday) in sandbox we have the following error in SF internal code, but not our code. We will explain you an example.

We have a controller class with pricebookentryid in an opportunitylineitem update. We triggered the update in opportunitylineitem with a value in pricebookentryid.
Then, opportunitylineitem before update trigger started without pricebookentryid (=null).

We have found out that when the code executes a line with an update on an opportunity line item in a controller class, right before the OpportunityLineItem Before Update trigger gets executed, the pricebookEntry value gets lost (it becomes null). That doesn't happen with other OpportunityLineItem fields nor any other objects.

We have not got this problem before and now in production there is not any problem about it. We haven't change the code. There is the same code in sandbox an production. The code was deployed to production and it works. This only happens in sandbox.
Arnt mongoDBArnt mongoDB
Same here, it is a bug in the new release in my opinion. I will log a case
Arnt mongoDBArnt mongoDB
Update: This is now a know issue for Winter 15:
https://success.salesforce.com/issues_view?id=a1p30000000T5c9AAC
Rajesh MetiRajesh Meti
I have logged case with Salesforce.com support and they are working on it. Will update if I hear from Salesforce.com Support