You need to sign in to do that
Don't have an account?
JackHaymes
Error: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object:
Hi peeps,
I'm creating a trigger to update a custom object when another object is updated, inserted or deleted. The class saves fine and passes the test with 100% code coverage but when I try to update the object in salesforce i get the following error:
Apex trigger TrackOpportunityItems caused an unexpected exception, contact your administrator: TrackOpportunityItems: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.OpportunityItemsUpdater.addOppItems: line 29, column 1
Here is the code for the class. Any help would be greatly appreciated,
Thanks :)
I'm creating a trigger to update a custom object when another object is updated, inserted or deleted. The class saves fine and passes the test with 100% code coverage but when I try to update the object in salesforce i get the following error:
Apex trigger TrackOpportunityItems caused an unexpected exception, contact your administrator: TrackOpportunityItems: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.OpportunityItemsUpdater.addOppItems: line 29, column 1
Here is the code for the class. Any help would be greatly appreciated,
Thanks :)
public class OpportunityItemsUpdater { public static void addOppItems(List<OpportunityLineItem> lineItems){ List<Opportunity_Item__c> OppItems = new List<Opportunity_Item__c>(); List<Id> productIds = new List<Id>(); for(OpportunityLineItem a : (List<OpportunityLineItem>)lineItems) { productIds.add(a.Product2Id); System.debug(productIds); } List<Product_Item__c> productItemsList = [SELECT Id, Quantity__c, Product__c, Item__c FROM Product_Item__c WHERE Product__c IN :productIds ]; System.debug(productItemsList); //Map<ProductID, List<Product Items that lookup to that product>> Map<Id, List<Product_Item__c>> productItemMap = new Map<Id, List<Product_Item__c>>(); for(Product_Item__c pi : productItemsList) { if(productItemMap.get(pi.Product__c ) == null) { productItemMap.put(pi.Product__c, new List<Product_Item__c>()); } productItemMap.get(pi.Product__c).add(pi); System.debug(productItemMap); } for (OpportunityLineItem a : (List<OpportunityLineItem>)lineItems) { for(Product_Item__c pi : productItemMap.get(a.Product2Id)) { System.debug(productItemMap); Opportunity_Item__c oppItem = new Opportunity_Item__c(); //oppItem.Opportunity_Product_Id__c = a.Product2Id; oppItem.Opportunity_Product_Id__c = a.Id; oppItem.Item_Id__c = pi.Item__c; oppItem.Quantity__c = a.Quantity * pi.Quantity__c; System.debug(pi); OppItems.add(oppItem); System.debug(oppItems); } } Insert OppItems; System.debug(OppItems); } public static void deleteOppItems(List<OpportunityLineItem> oldlineitems) { System.debug('delete'); Set<Id> DeletedOppLineItemIds = new Set<Id>(); for (OpportunityLineItem a : oldlineitems) { DeletedOppLineItemIds.add(a.Id); } Delete [SELECT Id FROM Opportunity_Item__c WHERE Opportunity_Item__c.Opportunity_Product_Id__c IN : DeletedOppLineItemIds]; } }
hi Jack,
Modify code like below and let us know if it helps !!
Thanks
Manoj
All Answers
hi Jack,
Modify code like below and let us know if it helps !!
Thanks
Manoj