I'm working on after update trigger. And it's throwing an exception after I'm checking null values too. i don't have any idea why it's
public void AfterUpdate(Map<Id, SObject> newItems, Map<Id, SObject> oldItems)
{
/**
* This method is to calclate average of all ARPU amounts with case have customer order recordtype
* as well as case details with either ADD-On (OR) New Subscription and update in related account
* field called ARPU.
* This method will fire only when any case with record type called customer order is created as well updated.
*/
Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'Customer_Order' Limit 1].Id;
Id recordTypeId1 = [Select Id From RecordType Where DeveloperName = 'UBI_Fulfillment' Limit 1].Id;
Id TrainingrecordTypeId = [Select Id from RecordType Where DeveloperName = 'Customer_Training' LIMIT 1].Id;
List<Account> Accountstoupdate =new List<Account>();
Map<Id, AggregateResult> results =new Map<Id, AggregateResult>();
Map<Id,Account> accWithCaseSalesDiv = new Map<Id,Account>();
set<Id> CaseAccountIds = new set<Id>();
Set<Id> CaseAccountIdsELD = new set<Id>();
List<Case> Onboarding= new List<Case>();
List<Case> CaseIDs = new List<Case>();
List<Case> TrainingCases = new List<Case>();
System.debug('Details in olditems keys'+oldItems.keyset());
System.debug('Details in olditems values'+oldItems.values());
System.debug('Details in full valuess'+oldItems);
Map<Id, Case> OldRecords = new Map<Id, Case>();
Map<Id,Id> AccountDetails = new Map<Id,Id>();
Map<Id,Account> FullAccountDetails = new Map<Id,Account>();
for(Id key : oldItems.keySet())
{
OldRecords.put(key, (Case)oldItems.get(key));
}
for(case css:OldRecords.values())
{
if(css.Case_Details__c == 'New Subscription' && css.Ship_Date__c == null )
{
AccountDetails.put(css.accountid,css.id);
}
}
If(AccountDetails <> null && !AccountDetails.isEmpty())
{
for(account a:[SELECT Id,Name,OwnerId,Team__c,Account_Status__c,Type FROM Account where id IN:AccountDetails.keyset()])
{
accWithCaseSalesDiv.put(AccountDetails.get(a.id),a);
FullAccountDetails.put(a.id,a);
}
}
System.debug('Accounts Data values'+accWithCaseSalesDiv);
for(Case i:(List<case>)newItems.values())
{
if(i.ARPU__c != OldRecords.get(i.id).ARPU__c && RecordTypeId == recordTypeId || i.RecordTypeId == recordTypeId1 && (i.Case_Details__c =='New Subscription' || i.Case_Details__c =='Add On') )
{
//Loop through and add caseId's to the list.
CaseIDs.add(i);
CaseAccountIds.add(i.accountId);
}
if(((i.P_SM_T377AZKAATT__c != OldRecords.get(i.id).P_SM_T377AZKAATT__c) || (i.P_GPS_TABE_8_KIT_P__c != OldRecords.get(i.id).P_GPS_TABE_8_KIT_P__c)) && i.RecordTypeId == recordTypeId && i.Status =='Shipped' && i.Total_ELD_Shipped__c > 0)
{
CaseAccountIdsELD.add(i.AccountId);
}
if(accWithCaseSalesDiv != null && !accWithCaseSalesDiv.isEmpty())
{
System.debug('Details ifor account'+accWithCaseSalesDiv);
System.debug('Current account id'+i.AccountId);
System.debug('RecordTypeID:'+i.RecordTypeId);
//System.debug('accWithCaseSalesDiv Details'+accWithCaseSalesDiv.get(i.Id).id);
//System.debug('accWithCaseSalesDiv details Account Status'+accWithCaseSalesDiv.get(i.Id).Account_Status__c);
//System.debug('accWithCaseSalesDiv Type'+accWithCaseSalesDiv.get(i.Id).Type);
System.debug('Case Details'+i.Case_Details__c);
System.debug('Account Details directly team'+i.account.team__c);
System.debug('old ship date'+OldRecords.get(i.id).Ship_Date__c);
System.debug('current ship details'+i.Ship_Date__c);
If(accWithCaseSalesDiv.get(i.Id).id != null && accWithCaseSalesDiv.get(i.Id).Account_Status__c != null && accWithCaseSalesDiv.get(i.Id).Type != null)
{
if(i.RecordTypeId == recordTypeId && i.AccountId == accWithCaseSalesDiv.get(i.Id).id && accWithCaseSalesDiv.get(i.id).team__c != '' && accWithCaseSalesDiv.get(i.id).team__c == 'Bangalore Team' && accWithCaseSalesDiv.get(i.id).Account_Status__c != '' && accWithCaseSalesDiv.get(i.Id).Account_Status__c == 'Active / Green' &&
accWithCaseSalesDiv.get(i.Id).Type != '' && (accWithCaseSalesDiv.get(i.Id).Type == 'Revenue' || accWithCaseSalesDiv.get(i.Id).Type == 'Transient') && i.Case_Details__c == 'New Subscription' && OldRecords.get(i.id).Ship_Date__c == null && i.Ship_Date__c != null)
{
OnBoarding.add(i);
}
}
}
}
}
When I check debug logs it's showing error on these lines:
If(accWithCaseSalesDiv.get(i.Id).id != null &&
accWithCaseSalesDiv.get(i.Id).Account_Status__c != null
&&accWithCaseSalesDiv.get(i.Id).Type != null)
{
}
Debug Logs:
11:22:27.50 (1837155126)|EXCEPTION_THROWN|[28]|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ForARPU: execution of AfterUpdate
caused by: System.NullPointerException: Attempt to de-reference a null object
Class.CaseTriggerHandler.AfterUpdate: line 720, column 1 Class.TriggerDispatcher.Run: line 36, column 1 Trigger.ForARPU: line 4, column 1: [] 11:22:27.50 (1837827557)|HEAP_ALLOCATE|[28]|Bytes:351 11:22:27.50 (1848750570)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ForARPU: execution of AfterUpdate
caused by: System.NullPointerException: Attempt to de-reference a null object
Class.CaseTriggerHandler.AfterUpdate: line 720, column 1 Class.TriggerDispatcher.Run: line 36, column 1 Trigger.ForARPU: line 4, column 1: []