I got stuck at one place on one of my requirement on which I am working on.

My use case is as follows:

SCENARIO 1: Close Opportunity when Upgrade Order is cancelled 

GIVEN a a person account record in Salesforce AND the has an open upgrade opportunity record in Salesforce AND the opportunity record type equals "Upgrade Opportunity" AND the opportunity stage is not "Closed - Won" WHEN places an upgrade order AND the status of the upgrade order has been changed to Cancelled THEN the open upgrade opportunity Stage field is set to "Closed - Lost" AND all other related opportunities that also meet this scenario criteria (Upgrade Opportunity, not Closed-Won) are set to "Closed - Lost"

I have done the scenario and code is also getting saved but getting System.NullPointerException:Attempt to de-reference a null Object when try to change the status of an upgrade order from a value say"Order-Placed" to "Cancelled"

Below is my Apex Class which runs in before update from Apex Trigger:

    public with sharing class OrderTriggerService extends TriggerService {
public void updateOpportunityStage(){
        Set<Id> setIds = new Set<Id>();
        Map<Id,Order> orderOldMap =(Map<Id,Order>)oldMap;
        Map<Id,Order> orderNewMap=(Map<Id,Order>)newMap;
        Set<Opportunity>  updateOppties=new Set<Opportunity>();
        DateTime statusChange= System.now()-90;

        for (Order childObj : (List<Order>) newList) {
            if(childObj.Account_Record_Type_Name__c == 'Recipient' && childObj.Record_Type_Dev_Name__c == 'Upgrade_Order'){ 

        if (setIds.isEmpty()) { return; }

        Map<ID, Account> parentAccts = new Map<Id, Account>(
                                          [select id, Upgrade_Order_Booked_Date__c, Upgrade_Order_Status__c, Order_Cancellation_Reason__c,Upgrade_Order_Number__c,Order_Cancellation_Date__c,
                                          (select id, Name, Booked_Date__c, Status ,Order_Cancellation_Reason__c,Oracle_Order_Number__c,Order_Cancellation_Date__c
                                            from Orders ) 
                                           from Account where id in :setIds]);
        System.debug('updateOrderUpgrade parentAccts: ' + parentAccts);

        Map<Account, Order> accountToLatestOrder = new Map<Account, Order>();
        for (Account acc : parentAccts.values()) {
            if (acc.orders.isEmpty() == false) {
                accountToLatestOrder.put(acc, acc.orders[0]);

      for(Account acc : [select id, Upgrade_Order_Booked_Date__c, Upgrade_Order_Status__c, Order_Cancellation_Reason__c,Upgrade_Order_Number__c,Order_Cancellation_Date__c,
                                    (select Id,Name,closedate,amount,StageName,Oracle_Order_Number__c,Status_Last_Changed_On__c
                                     from opportunities where Record_Type_Dev_Name__c='Upgrade_Opportunity') 
                                    from Account where id in : setIds]){

                             for(Order orders : accountToLatestOrder.values()){            
                                    for(Opportunity opps : acc.opportunities){

                                        if(opps.StageName<>'Upgrade-Closed Won' && orders.Status!=orderOldMap.get(orders.Id).Status && orders.Status=='Cancelled'){

                                                 opps.StageName='Upgrade - Closed Lost'; 


                                 System.debug('updateOpportunityStage Opportunity: ' + opps);  

                        List<Opportunity> updateopps=new List<Opportunity>(updateOppties);
                        update updateopps;

I am receiving error in below line of code

if(opps.StageName<>'Upgrade-Closed Won' && orders.Status!=orderOldMap.get(orders.Id).Status && orders.Status=='Cancelled')

Also,attached is the screenshots

screenshots from UI-Error when changing status from one value to "Cancelled" for an upgrade order

Kindly help me.

Any help will be greatly appreciated.

Many thanks​

1 Answer 1


In accountToLatestOrder.values() you are queryiong Account andOrder and storing first child. Which may/may not part of current trigger execution flow.

When in next for loop your code execute this orderOldMap.get(orders.Id).Status so there are very good chances that trigger.old doesn't contain this order and it will return you null and you will get this exception. So you first need to check if your map contains this.

if(opps.StageName<>'Upgrade-Closed Won' && orderOldMap.containsKey(orders.Id) && orders.Status!=orderOldMap.get(orders.Id).Status && orders.Status=='Cancelled') // You can modify this condition as per your need
  • Thanks @Tushar for your answer.But still opportunity are not getting updated. I mean if any order of record type=upgrade opportunity changes its status to "CANCELLED" then all the opportunities(of record type=upgrade opportunity) linked with the account to which order is linked whose stage is not equals to Closed Won will becomes Closed-Lost. After applying your code the records are getting saved but nothing is happening on oppties.I checked in debug logs and found orderOldMap is returning null.Please revist my code and suggests me.Kindly help.Many thanks in advance Oct 7, 2018 at 15:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .