function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Josh Anderson 30Josh Anderson 30 

NullPointerException on Ifelse statement

System.DmlException: Update failed. First exception on row 0 with id 0010m00000Hh4g5AAB; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Account: execution of BeforeUpdate

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.AccountTriggerHandler.updateCustomerJourneyStage: line 102, column 1
Class.AccountTriggerHandler.OnBeforeUpdate: line 12, column 1
Trigger.Account: line 15, column 1: []I am getting an NPE for an if-else statement in my class and I am getting a little confused..

 Does anyone see what I might be missing? 

Trigger: 
trigger Account on Account (after delete, after insert, after undelete, after update, before delete, before insert, before update) {

    AccountTriggerHandler handler = new AccountTriggerHandler();

    /* Before Insert */
    if(Trigger.isInsert && Trigger.isBefore){
        AccountTriggerHandler.OnBeforeInsert(Trigger.new);
    }
    /* After Insert */
    else if(Trigger.isInsert && Trigger.isAfter){
        AccountTriggerHandler.OnAfterInsert(Trigger.new);
    }
    /* Before Update */
    else if(Trigger.isUpdate && Trigger.isBefore){
        AccountTriggerHandler.OnBeforeUpdate(Trigger.old, Trigger.new, Trigger.newMap);
    }
    /* After Update */
    else if(Trigger.isUpdate && Trigger.isAfter){
        AccountTriggerHandler.OnAfterUpdate(Trigger.old, Trigger.new, Trigger.newMap);
    }
    /* Before Delete 
    else if(Trigger.isDelete && Trigger.isBefore){
        AccountTriggerHandler.OnBeforeDelete(Trigger.old, Trigger.oldMap);
    }*/
    
    /* After Delete 
    else if(Trigger.isDelete && Trigger.isAfter){
        AccountTriggerHandler.OnAfterDelete(Trigger.old, Trigger.oldMap);
    }*/

    /* After Undelete 
    else if(Trigger.isUnDelete){
        AccountTriggerHandler.OnUndelete(Trigger.new);
    }*/

}

Apex:
public with sharing class AccountTriggerHandler {
    
    public static void OnBeforeInsert(List<Account> accounts) {
		updateCustomerJourneyStage(accounts);
    }
    
    public static void OnAfterInsert(List<Account> accounts) {
		
    }
    
    public static void OnBeforeUpdate(List<Account> oldAccounts, List<Account> newAccounts, Map<Id, Account> oldMap) {
		updateCustomerJourneyStage(newAccounts);
    }
    
    public static void OnAfterUpdate(List<Account> oldAccounts, List<Account> newAccounts, Map<Id, Account> oldMap) {

    }
    
    //Creation of the task if customer journey is changed
    public static Task createTaskCJS(ID accountId, String stageName, String flightName){
        return new Task(RecordTypeId = Schema.SObjectType.Task.getRecordTypeInfosByName().get('Customer Journey').getRecordTypeId(),
            			WhatId=accountId, 
                        subject=(UserInfo.getName() + ' changed Customer Journey Stage' + stageName),
                        Activity_Type__c = 'Stage Change', 
                        type_detail__c = flightName,
                        status = 'Completed',
                        ActivityDate = date.today(),
                        Description = stageName,
                        Priority = 'Normal',
                        Customer_Journey_Stage_Snapshot__c = StageName);
    }
    
    
    
    //method used to run if we need to update the customer journey
    public static void updateCustomerJourneyStage(List<Account> updatedCJ) {
        Map<Id, Account> oldMap = new Map<Id,Account>();
        
        for (Account a :updatedCJ){
            oldMap.put(a.id, a);
        }
        List<Task> newTasks = new List<Task>();
        
        //Loop to check if roll ups have been updated
        	for (Account acc :updatedCJ){
    	
                if (acc.CJ_Priority_Opportunity_Roll__c == 17 
                    && acc.Active__c
                    && acc.Customer_Journey_Stage_Text__c != '(17) A - Sales Won') {
                        acc.Customer_Journey_Stage_Text__c = '(17) A - Sales Won';
                        if(acc.Customer_Journey_Flight_Text__c != '(D) Customer Renewal' ){
                            acc.Customer_Journey_Flight_Text__c = '(D) Customer Renewal';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(17) A - Sales Won', '(D) Customer Renewal'));
                } else if (
                    acc.CJ_Priority_Opportunity_Roll__c == 16 
                    && acc.Active__c
                    && acc.Customer_Journey_Stage_Text__c != '(16) B - Commitment') {
                        acc.Customer_Journey_Stage_Text__c = '(16) B - Commitment';
                        if(acc.Customer_Journey_Flight_Text__c != '(D) Customer Renewal' ){
                            acc.Customer_Journey_Flight_Text__c = '(D) Customer Renewal';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(16) B - Commitment', '(D) Customer Renewal'));
                } else if (
                    acc.CJ_Priority_Opportunity_Roll__c == 15 
                    && acc.Active__c
                    && acc.Customer_Journey_Stage_Text__c != '(15) C - Decision') {
                        acc.Customer_Journey_Stage_Text__c = '(15) C - Decision';
                        if(acc.Customer_Journey_Flight_Text__c != '(D) Customer Renewal' ){
                            acc.Customer_Journey_Flight_Text__c = '(D) Customer Renewal';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(15) C - Decision', '(D) Customer Renewal'));
                } else if (
                    acc.CJ_Priority_Opportunity_Roll__c == 14 
                    && acc.Active__c
                    && acc.Customer_Journey_Stage_Text__c != '(14) D - Evaluation') {
                        acc.Customer_Journey_Stage_Text__c = '(14) D - Evaluation';
                        if(acc.Customer_Journey_Flight_Text__c != '(D) Customer Renewal' ){
                            acc.Customer_Journey_Flight_Text__c = '(D) Customer Renewal';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(14) D - Evaluation', '(D) Customer Renewal'));
                } else if (
                    acc.CJ_Priority_Opportunity_Roll__c == 13 
                    && acc.Active__c
                    && acc.Customer_Journey_Stage_Text__c != '(13) E - Consideration') {
                        acc.Customer_Journey_Stage_Text__c = '(13) E - Consideration';
                        if(acc.Customer_Journey_Flight_Text__c != '(D) Customer Renewal' ){
                            acc.Customer_Journey_Flight_Text__c = '(D) Customer Renewal';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(13) E - Consideration', '(D) Customer Renewal'));
                } else if (
                    acc.Active_Marketer__c
                    && date.TODAY().daysBetween(acc.Earliest_Subscription_Start_Date__c) > 90
                    && acc.Customer_Journey_Stage_Text__c != '(12) Maturity') {
                        acc.Customer_Journey_Stage_Text__c = '(12) Maturity';
                        if(acc.Customer_Journey_Flight_Text__c != '(C) Active Customer' ){
                            acc.Customer_Journey_Flight_Text__c = '(C) Active Customer';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(12) Maturity', '(C) Active Customer'));
                } else if (
                    acc.Active_Marketer__c 
                    && acc.Active_Subscription_Charge_Names__c.contains('Jumpstart') 
                    && date.TODAY().daysBetween(acc.Earliest_Subscription_Start_Date__c) < 91 
                    && date.TODAY().daysBetween(acc.Earliest_Subscription_Start_Date__c) >29
                    && acc.Customer_Journey_Stage_Text__c != '(11) Adoption') {
                        acc.Customer_Journey_Stage_Text__c = '(11) Adoption';
                        if(acc.Customer_Journey_Flight_Text__c != '(C) Active Customer' ){
                            acc.Customer_Journey_Flight_Text__c = '(C) Active Customer';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(11) Adoption', '(C) Active Customer'));
                } else if (
                    acc.Active_Marketer__c 
                    && date.TODAY().daysBetween(acc.Earliest_Subscription_Start_Date__c) > 30
                    && acc.Customer_Journey_Stage_Text__c != '(C) Active Customer') {
                        acc.Customer_Journey_Stage_Text__c = '(C) Active Customer';
                        if(acc.Customer_Journey_Flight_Text__c != '(C) Active Customer' ){
                            acc.Customer_Journey_Flight_Text__c = '(C) Active Customer';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(C) Active Customer', '(C) Active Customer'));
                } else if (
                    acc.Active_Marketer__c
                    && date.TODAY().daysBetween(acc.Earliest_Subscription_Start_Date__c) < 31
                    && acc.Customer_Journey_Stage_Text__c != '(10) Launch') {
                        acc.Customer_Journey_Stage_Text__c = '(10) Launch';
                        if(acc.Customer_Journey_Flight_Text__c != '(C) Active Customer' ){
                            acc.Customer_Journey_Flight_Text__c = '(C) Active Customer';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(10) Launch', '(C) Active Customer'));
                } else if (
                    acc.CJ_Priority_Opportunity_Roll__c == 9
                    && acc.Customer_Journey_Stage_Text__c != '(9) A - Sales Won') {
                        acc.Customer_Journey_Stage_Text__c = '(9) A - Sales Won';
                        if(acc.Customer_Journey_Flight_Text__c != '(B) Opportunity Pipeline' ){
                            acc.Customer_Journey_Flight_Text__c = '(B) Opportunity Pipeline';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(9) A - Sales Won', '(B) Opportunity Pipeline'));
                } else if (
                    acc.CJ_Priority_Opportunity_Roll__c == 8
                    && acc.Customer_Journey_Stage_Text__c != '(8) B - Commitment') {
                        acc.Customer_Journey_Stage_Text__c = '(8) B - Commitment';
                        if(acc.Customer_Journey_Flight_Text__c != '(B) Opportunity Pipeline' ){
                            acc.Customer_Journey_Flight_Text__c = '(B) Opportunity Pipeline';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(8) B - Commitment', '(B) Opportunity Pipeline'));
                } else if (
                    acc.CJ_Priority_Opportunity_Roll__c == 7
                    && acc.Customer_Journey_Stage_Text__c != '(7) C - Decision') {
                        acc.Customer_Journey_Stage_Text__c = '(7) C - Decision';
                        if(acc.Customer_Journey_Flight_Text__c != '(B) Opportunity Pipeline' ){
                            acc.Customer_Journey_Flight_Text__c = '(B) Opportunity Pipeline';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(7) C - Decision', '(B) Opportunity Pipeline'));
                } else if (
                    acc.CJ_Priority_Opportunity_Roll__c == 6
                    && acc.Customer_Journey_Stage_Text__c != '(6) D - Evaluation') {
                        acc.Customer_Journey_Stage_Text__c = '(6) D - Evaluation';
                        if(acc.Customer_Journey_Flight_Text__c != '(B) Opportunity Pipeline' ){
                            acc.Customer_Journey_Flight_Text__c = '(B) Opportunity Pipeline';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(6) D - Evaluation', '(B) Opportunity Pipeline'));
                } else if (
                    acc.CJ_Priority_Opportunity_Roll__c == 5
                    && acc.Customer_Journey_Stage_Text__c != '(5) E - Consideration') {
                        acc.Customer_Journey_Stage_Text__c = '(5) E - Consideration';
                        if(acc.Customer_Journey_Flight_Text__c != '(B) Opportunity Pipeline' ){
                            acc.Customer_Journey_Flight_Text__c = '(B) Opportunity Pipeline';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(5) E - Consideration', '(B) Opportunity Pipeline'));
                } else if (
                    acc.CJ_Priority_Opportunity_Roll__c == 4
                    && acc.Customer_Journey_Stage_Text__c != '(4) F - Awareness') {
                        acc.Customer_Journey_Stage_Text__c = '(4) F - Awareness';
                        if(acc.Customer_Journey_Flight_Text__c != '(B) Opportunity Pipeline' ){
                            acc.Customer_Journey_Flight_Text__c = '(B) Opportunity Pipeline';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(4) F - Awareness', '(B) Opportunity Pipeline'));
                } else if (
                    acc.Active_Marketplace__c
                    && acc.Customer_Journey_Stage_Text__c != 'Marketplace Client') {
                        acc.Customer_Journey_Stage_Text__c = 'Marketplace Client';
                        if(acc.Customer_Journey_Flight_Text__c != 'Marketplace Client' ){
                            acc.Customer_Journey_Flight_Text__c = 'Marketplace Client';
                        }
                        newTasks.add(createTaskCJS(acc.id, 'Marketplace Client', 'Marketplace Client'));
                } else if (
                    acc.Active_SDP__c 
                    && acc.Customer_Journey_Stage_Text__c != 'Media Partner') {
                        acc.Customer_Journey_Stage_Text__c = 'Media Partner';
                        if(acc.Customer_Journey_Flight_Text__c != 'Media Partner' ){
                            acc.Customer_Journey_Flight_Text__c = 'Media Partner';
                        }
                        newTasks.add(createTaskCJS(acc.id, 'Media Partner', 'Media Partner'));
                } else if (
                    acc.CJ_Priority_Contact_Roll__c == 3 
                    && acc.Customer_Journey_Stage_Text__c != '(3) Qualified') {
                        acc.Customer_Journey_Stage_Text__c = '(3) Qualified';
                        if(acc.Customer_Journey_Flight_Text__c != '(A) Prospect Development' ){
                            acc.Customer_Journey_Flight_Text__c = '(A) Prospect Development';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(3) Qualified', '(A) Prospect Development'));
                } else if (
                    acc.Churned__c 
                    && acc.Customer_Journey_Stage_Text__c != '(F) Churned') {
                        acc.Customer_Journey_Stage_Text__c = '(F) Churned';
                        if(acc.Customer_Journey_Flight_Text__c != '(F) Churned' ){
                            acc.Customer_Journey_Flight_Text__c = '(F) Churned';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(F) Churned', '(F) Churned'));
                } else if (
                    acc.CJ_Priority_Contact_Roll__c == 2
                    && acc.Customer_Journey_Stage_Text__c != '(2) Engaged') {
                        acc.Customer_Journey_Stage_Text__c = '(2) Engaged';
                        if(acc.Customer_Journey_Flight_Text__c != '(A) Prospect Development' ){
                            acc.Customer_Journey_Flight_Text__c = '(A) Prospect Development';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(2) Engaged', '(A) Prospect Development'));
                } else if (
                    acc.CJ_Priority_Contact_Roll__c == 1
                    && acc.Customer_Journey_Stage_Text__c != '(1) Known') {
                        acc.Customer_Journey_Stage_Text__c = '(1) Known';
                        if(acc.Customer_Journey_Flight_Text__c != '(A) Prospect Development' ){
                            acc.Customer_Journey_Flight_Text__c = '(A) Prospect Development';
                        }
                        newTasks.add(createTaskCJS(acc.id, '(1) Known', '(A) Prospect Development' ));
                 } else if (
                    acc.Customer_Journey_Stage_Text__c != 'Not ICP') {
                        acc.Customer_Journey_Stage_Text__c = 'Not ICP';
                        if(acc.Customer_Journey_Flight_Text__c != 'Not ICP' ){
                            acc.Customer_Journey_Flight_Text__c = 'Not ICP';
                        }
                        newTasks.add(createTaskCJS(acc.id, 'Not ICP', 'Not ICP'));
                    } else {
                        acc.Customer_Journey_Flight_Text__c = null; 
                        acc.Customer_Journey_Stage_Text__c = null; 
                    } 
            }
            if(newTasks.size() > 0) {
            insert newTasks;
            }
        }
        
    }

Test: 
@isTest
public class AccountTriggerHandlerTest {
    
    public static testmethod void testCustomerJourney() {
        //Insert Account & Check Journey
        Account acc = new Account(Name = 'Test1', 
                                  Type = 'Direct Marketer', 
                                  Website = 'www.test.com');
        insert acc;
        Account acc1 = new Account(Name = 'Test1', 
                                  Type = 'Direct Marketer', 
                                  Website = 'www.test.com');
        insert acc1;
        test.startTest();
        //Test for (1) Known
        acc.CJ_Priority_Contact_Roll__c = 1;
        update acc;
        
        //Test for (2) Engaged
        acc.CJ_Priority_Contact_Roll__c = 2;
        update acc;
                
        //Test for (3) Qualified
        acc.CJ_Priority_Contact_Roll__c = 3;
        update acc;
        
        //Test for (4) F - Awareness
        acc.CJ_Priority_Opportunity_Roll__c = 4;
        update acc;
        
        //Test for (5) E - Consideration
        acc.CJ_Priority_Opportunity_Roll__c = 5;
        update acc;
        
        //Test for (6) D - Evaluation
        acc.CJ_Priority_Opportunity_Roll__c = 6;
        update acc;
        
        //Test for (7) C - Decision
        acc.CJ_Priority_Opportunity_Roll__c = 7;
        update acc;
        
        //Test for (8) B - Commitment
        acc.CJ_Priority_Opportunity_Roll__c = 8;
        update acc;
        
        //Test for (9)A - Sales Won
        acc.CJ_Priority_Opportunity_Roll__c = 9;
        update acc;
        
        //Test for (10) Launch 
        acc.Active_Product_Names__c = 'Software - Marketer';
        acc.Earliest_Subscription_Start_Date__c = date.today().addDays(25);
        acc.Max_Mktplace_Won_Contract_End_Date__c = date.today().addDays(365);
        update acc;
        
        //Test for (C)Active Customer ***
        acc.Earliest_Subscription_Start_Date__c = date.today().addDays(45);
        acc.Active_Subscription_Charge_Names__c = 'Jumpstart';
        update acc;
        
        //Test for (11)Adoption
        
        
        //Test for (12)Maturity
        
        
        //Test for (13)E - Consideration
        acc.CJ_Priority_Opportunity_Roll__c = 13;
        update acc;
        
        //Test for (14)D - Evaluation
        acc.CJ_Priority_Opportunity_Roll__c = 14;
        update acc;
        
        //Test for (15)C - Decision
        acc.CJ_Priority_Opportunity_Roll__c = 15;
        update acc;
        
        //Test for (16)B - Commitment
        acc.CJ_Priority_Opportunity_Roll__c = 16;
        update acc;
        
        //Test for (17)A - Sales Won
        acc.CJ_Priority_Opportunity_Roll__c = 17;
        update acc;
        
        //Test for Marketplace Client
        
        update acc;
        
        //Media Partner
        acc.active_product_Names__c = 'Software - Strategic Demand Partner';
        update acc;
        system.debug('Product Name: ' + acc.Active_Product_Names__c);
        system.debug('Active SDP: ' + acc.Active_SDP__c);
        
        //Test for (F) Churned
        
        update acc;
        test.stopTest();
    }
}

 
Raj VakatiRaj Vakati
Looks like its issue iwth data setup 


you need to set Customer_Journey_Flight_Text__c and other fields etc 


try like this
 
@isTest
public class AccountTriggerHandlerTest {

public static testmethod void testCustomerJourney() {
	//Insert Account & Check Journey
	Account acc = new Account(Name = 'Test1', 
							  Type = 'Direct Marketer', 
							  Website = 'www.test.com');
	
	acc.CJ_Priority_Opportunity_Roll__c =17 ;
	acc.Active__c = true ; 
	acc.Customer_Journey_Stage_Text__c = '(17) A - Sales Won';
	acc.Customer_Journey_Flight_Text__c = '(D) Customer Renewal';
					
		
insert acc ; 

		
	Account acc1 = new Account(Name = 'Test1', 
							  Type = 'Direct Marketer', 
							  Website = 'www.test.com');


							  	acc.CJ_Priority_Opportunity_Roll__c =17 ;
	acc1.Active__c = true ; 
	acc1.Customer_Journey_Stage_Text__c = '(17) A - Sales Won';
	acc1.Customer_Journey_Flight_Text__c = '(D) Customer Renewal';
					
		
insert acc1 ; 


							  test.startTest();
	//Test for (1) Known
	acc.CJ_Priority_Contact_Roll__c = 1;
	update acc;
	
	//Test for (2) Engaged
	acc.CJ_Priority_Contact_Roll__c = 2;
	update acc;
			
	//Test for (3) Qualified
	acc.CJ_Priority_Contact_Roll__c = 3;
	update acc;
	
	//Test for (4) F - Awareness
	acc.CJ_Priority_Opportunity_Roll__c = 4;
	update acc;
	
	//Test for (5) E - Consideration
	acc.CJ_Priority_Opportunity_Roll__c = 5;
	update acc;
	
	//Test for (6) D - Evaluation
	acc.CJ_Priority_Opportunity_Roll__c = 6;
	update acc;
	
	//Test for (7) C - Decision
	acc.CJ_Priority_Opportunity_Roll__c = 7;
	update acc;
	
	//Test for (8) B - Commitment
	acc.CJ_Priority_Opportunity_Roll__c = 8;
	update acc;
	
	//Test for (9)A - Sales Won
	acc.CJ_Priority_Opportunity_Roll__c = 9;
	update acc;
	
	//Test for (10) Launch 
	acc.Active_Product_Names__c = 'Software - Marketer';
	acc.Earliest_Subscription_Start_Date__c = date.today().addDays(25);
	acc.Max_Mktplace_Won_Contract_End_Date__c = date.today().addDays(365);
	update acc;
	
	//Test for (C)Active Customer ***
	acc.Earliest_Subscription_Start_Date__c = date.today().addDays(45);
	acc.Active_Subscription_Charge_Names__c = 'Jumpstart';
	update acc;
	
	//Test for (11)Adoption
	
	
	//Test for (12)Maturity
	
	
	//Test for (13)E - Consideration
	acc.CJ_Priority_Opportunity_Roll__c = 13;
	update acc;
	
	//Test for (14)D - Evaluation
	acc.CJ_Priority_Opportunity_Roll__c = 14;
	update acc;
	
	//Test for (15)C - Decision
	acc.CJ_Priority_Opportunity_Roll__c = 15;
	update acc;
	
	//Test for (16)B - Commitment
	acc.CJ_Priority_Opportunity_Roll__c = 16;
	update acc;
	
	//Test for (17)A - Sales Won
	acc.CJ_Priority_Opportunity_Roll__c = 17;
	update acc;
	
	//Test for Marketplace Client
	
	update acc;
	
	//Media Partner
	acc.active_product_Names__c = 'Software - Strategic Demand Partner';
	update acc;
	system.debug('Product Name: ' + acc.Active_Product_Names__c);
	system.debug('Active SDP: ' + acc.Active_SDP__c);
	
	//Test for (F) Churned
	
	update acc;
	test.stopTest();
}
}