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
jReneejRenee 

Opportunity Descount Calculation Apex trigger error

Hello:

 

I'm back with the same issue with further developments. I'm hoping someone will take a look and recognize what's happening and why.

 

No one claimed to be using the discount, so I copied the apex trigger into my sandbox, preparing to deactivate it.  However, yesterday I found out someone is using the apex trigger and so I probably need to modify the code or something.  Below is the error message that we get when trying to upload a NEWLY CREATED product onto an opportunity page.

 

Apex trigger OpportunityDiscountCalculation caused an
unexpected exception, contact your administrator:
OpportunityDiscountCalculation: execution of BeforeInsert caused by:
System.NullPointerException: Attempt to de-reference a null object:
Trigger.OpportunityDiscountCalculation: line 11, column 1
  

 

Below is the actual apex trigger that I'm convinced someone from Salesforce created for our previous System Administrator, as she was brand new to the system.  Anyone recognize it?

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40trigger OpportunityDiscountCalculation on OpportunityLineItem (before insert, before update) {   List<String> priceBookIds = new List<String>();   for(OpportunityLineItem oli : Trigger.New) {     if(oli.PricebookEntryId != null)       priceBookIds.add(oli.PriceBookEntryId);   }   Map<Id, PricebookEntry> productCodeMap = new Map<Id, PricebookEntry>([Select Id, ProductCode, Product2.Revenue_Type__c from PricebookEntry where Id in :priceBookIds]);   for(OpportunityLineItem oli : Trigger.New) {     if(trigger.isInsert || oli.Quantity != Trigger.oldMap.get(oli.Id).Quantity) {       Double totalDiscount = 0.0;       if(productCodeMap.containsKey(oli.PricebookEntryId) && (productCodeMap.get(oli.PriceBookEntryId).ProductCode.startsWith('PPM') || productCodeMap.get(oli.PriceBookEntryId).ProductCode.startsWith('PPE')) && (productCodeMap.get(oli.PriceBookEntryId).Product2.Revenue_Type__c == null || !productCodeMap.get(oli.PriceBookEntryId).Product2.Revenue_Type__c.contains('Maintenance'))) {         for(Integer i = 1; i <= oli.Quantity; i++) {           if(i >= 6 && i <= 10) {             totalDiscount += 5;           }           else if(i >= 11 && i <= 20) {             totalDiscount += 10;           }           else if(i >= 21 && i <= 30) {             totalDiscount += 15;           }           else if(i >= 31 && i <= 40) {             totalDiscount += 20;           }           else if(i >= 41 && i <= 50) {             totalDiscount += 25;           }           else if(i >= 51 && i <= 75) {             totalDiscount += 30;           }           else if(i >= 76) {             totalDiscount += 35;           }         }       }       totalDiscount = totalDiscount / oli.Quantity;       oli.Discount_Percentage__c = totalDiscount;     }   } }

 

Oh well, doesn't look like it copied correctly on this.  Sorry.

 

I was hoping that someone could assist me in modifying the code so we don't get this error anymore.

 

Thanks again,

Renee

pigginsbpigginsb

Which is line 11?

 

I first thought that since trigger.old is null in a before insert trigger then that would be the cause, but the if trigger.isInsert should avoid that from being evaluated. Then I thought that there is no Id for a record in a before insert trigger, the if isInsert would avoid that as well.

 

It seems like there may be a field that is populated upon saving a record (maybe Product Code?), and the value wouldn't yet exist in the before insert trigger. in which case an after insert trigger may be the answer.

 

I recommend an individual debug or assert statement for every object or field referenced in line 11. That would at least tell you where the null occurs. The assert statements would only be for testing, and would not remain in the trigger when it is put to use in production. They can be a bit quicker than looking up the line in a debug log.