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'){
setIds.add(childObj.AccountId);
}
}
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);
updateOppties.add(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
Kindly help me.
Any help will be greatly appreciated.
Many thanks​