You need to sign in to do that
Don't have an account?
Maria22
FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object Apex Triggers
Hi All,
I have written a trigger on Order object which will take the status of Order Start Date field from Order and copy the same on "Upgrade Order Date" field in accounts.
I am receiving System.NullPointerException and I am not able to find out where I am making mistakes or what I have done wrong.
Below is my trigger:
Below is my trigger Handler:
I ran the query in workbench and getting few records as well but in debug logs "parentAccts" doesn'r returning any row and line
retuen null and myParentAcct.Upgrade_Order_Date__c= order.EffectiveDate shows the attempt to de-reference a null object.
Kindly help/guide/suggests.
Any help will be greatly apprecaited
Many thanks in advance
Thanks & Regards,
Harjeet
I have written a trigger on Order object which will take the status of Order Start Date field from Order and copy the same on "Upgrade Order Date" field in accounts.
I am receiving System.NullPointerException and I am not able to find out where I am making mistakes or what I have done wrong.
Below is my trigger:
trigger OrderTrigger on Order (after insert) { OrderTriggerHandler orderhandler= new OrderTriggerHandler(trigger.new); if(trigger.isAfter && trigger.isInsert) { orderhandler.HandleAfterInsert(); } }
Below is my trigger Handler:
public class OrderTriggerHandler { List<Order>newOrders; public OrderTriggerHandler(List<Order>newOrders){ this.newOrders=newOrders; } public void HandleAfterInsert(){ updateOrderUpgradeStatus(); } public void updateOrderUpgradeStatus(){ Map<ID, Account> parentAccts = new Map<ID, Account>(); //Making it a map instead of list for easier lookup //List<Id> listIds = new List<Id>(); Set<Id> setIds=new Set<Id>(); for (Order childObj : newOrders) { setIds.add(childObj.Name); } system.debug('The value is: ' + parentAccts); //parentAccts = new Map<Id, Account>([Select id,Name,Upgrade_Order_Date__c,(Select id,EffectiveDate,Name,Header_Id__c from Orders where RecordType.name = 'Upgrade Order' ORDER BY Header_Id__c DESC) from Account WHERE ID IN :setIds AND RecordType.name = 'Recipient']); parentAccts = new Map<Id, Account>([Select id,Name,Upgrade_Order_Date__c,(Select id,Name,EffectiveDate,Header_Id__c,Status from Orders where RecordType.name = 'Upgrade Order' ORDER BY Header_Id__c DESC NULLS LAST) from Account WHERE ID IN :setIds AND RecordType.name = 'Recipient' ]); for (Order order: newOrders){ // if(Order.Type=='Upgrade Order'){ Account myParentAcct = parentAccts.get(order.Name); system.debug('The value is: ' + myParentAcct); myParentAcct.Upgrade_Order_Date__c= order.EffectiveDate; //order.EffectiveDate=myParentAcct.Upgrade_Order_Date__c; //} } system.debug('The value is: ' + parentAccts); update parentAccts.values(); } }
I ran the query in workbench and getting few records as well but in debug logs "parentAccts" doesn'r returning any row and line
system.debug('The value is: ' + myParentAcct);
retuen null and myParentAcct.Upgrade_Order_Date__c= order.EffectiveDate shows the attempt to de-reference a null object.
Kindly help/guide/suggests.
Any help will be greatly apprecaited
Many thanks in advance
Thanks & Regards,
Harjeet
Try this
Put following code into containsKey check.
Account myParentAcct = parentAccts.get(order.Name);
system.debug('The value is: ' + myParentAcct);
myParentAcct.Upgrade_Order_Date__c= order.EffectiveDate;
Also initialize the List<Order>newOrders. i.e List<Order> newOrders = new List<Order>();
Thanks!