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
MBarnardMBarnard 

Attempt to De-Reference a Null Object - New Apex Trigger on Task

Hey Guys,

 

I have an urgent fix needed, I deployed a new Trigger (and subsequently had to comment it out).

 

I have no idea, why when modifying an Account, is causing a Task trigger to error out. 

 

There are obviously two triggers fighting back and forth and I am not sure why.

 

The Trigger is this: 

trigger AccountUpdateonLogCall on Task (after insert, after update) {
	
	//To do - If the Purpose of a completed task contains "Low Redemption" or "Proactive", put the description of the completed task in the 
	//the "Low_Redemption_Notes__c" and/or "Proactive_Communication_Notes__c" field on the account object

//Create a set of related account ID's
Set <ID> acctIDs = new Set <ID> ();


//For every task, add it's related to account ID to the set
	for (Task t: Trigger.new){
	  acctIDs.add(t.accountID);
//Create a map to match the task related to ID's with their corresponding account ID's
	  Map<ID, Account> acctMap = new Map<ID, Account> ([Select ID, Proactive_Communication_Notes__c, Low_Redemption_Notes__c  from Account where ID in :acctIDs]);
//Create the account object
      Account acctRec = acctMap.get(t.whatID);

//If the account ID isn't null, the Purpose is Pro or Low, and the task has been marked as completed    
//Check to see if the which fields to update based upon the Purpose__c field.
  	  if (t.accountID != null && t.Purpose__c.Equals('Proactive Communication') && t.Status == 'Completed'){
//Update the Last_SW_Activity__c field on the account object with the task's end date  
  		  acctrec.Proactive_Communication_Notes__c = t.Description;
  		  update acctRec;
  	  }
  	  else if (t.accountID != null &&  t.Purpose__c == ('Low-Redemption Communication') && t.Status == ('Completed')){
  	  	  acctrec.Proactive_Communication_Notes__c = t.Description;
  	  	  acctrec.Low_Redemption_Notes__c = t.Description;
  		  update acctRec;
  	}
  }

 

 The end of the error is here: 

09:28:50.461|CUMULATIVE_LIMIT_USAGE_END

09:28:58.640 (15640685095)|CODE_UNIT_FINISHED|TestClass.myUnitTest
09:28:58.640 (15640691443)|EXECUTION_FINISHED
09:28:58.672 (15672545565)|EXECUTION_STARTED
09:28:58.672 (15672557111)|CODE_UNIT_STARTED|[EXTERNAL]|01pU0000000uhEc|unsubscribe.testUnsubscribe
09:28:58.672 (15672823603)|METHOD_ENTRY|[1]|01pU0000000uhEc|unsubscribe.unsubscribe()
09:28:58.672 (15672831894)|METHOD_EXIT|[1]|unsubscribe
09:28:58.672 (15672929931)|SYSTEM_CONSTRUCTOR_ENTRY|[94]|<init>()
09:28:58.672 (15672964293)|SYSTEM_CONSTRUCTOR_EXIT|[94]|<init>()
09:28:58.673 (15673000365)|SYSTEM_CONSTRUCTOR_ENTRY|[95]|<init>()
09:28:58.673 (15673018059)|SYSTEM_CONSTRUCTOR_EXIT|[95]|<init>()
09:28:58.673 (15673179045)|DML_BEGIN|[103]|Op:Insert|Type:Lead|Rows:1
09:28:59.261 (16261741837)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Lead
09:28:59.268 (16268756720)|CODE_UNIT_FINISHED|Workflow:Lead
09:28:59.270 (16270842834)|DML_END|[103]
09:28:59.271 (16271021000)|DML_BEGIN|[110]|Op:Insert|Type:Contact|Rows:1
09:28:59.392 (16392831876)|DML_END|[110]
09:28:59.392 (16392927559)|CONSTRUCTOR_ENTRY|[117]|01pU0000000uhEc|<init>()
09:28:59.392 (16392984711)|CONSTRUCTOR_EXIT|[117]|01pU0000000uhEc|<init>()
09:28:59.393 (16393010727)|METHOD_ENTRY|[118]|01pU0000000uhEc|unsubscribe.handleInboundEmail(Messaging.InboundEmail, Messaging.InboundEnvelope)
09:28:59.393 (16393088911)|SYSTEM_CONSTRUCTOR_ENTRY|[8]|<init>()
09:28:59.393 (16393117648)|SYSTEM_CONSTRUCTOR_EXIT|[8]|<init>()
09:28:59.393 (16393137238)|SYSTEM_CONSTRUCTOR_ENTRY|[11]|<init>()
09:28:59.393 (16393158715)|SYSTEM_CONSTRUCTOR_EXIT|[11]|<init>()
09:28:59.393 (16393204778)|SYSTEM_CONSTRUCTOR_ENTRY|[12]|<init>()
09:28:59.393 (16393227885)|SYSTEM_CONSTRUCTOR_EXIT|[12]|<init>()
09:28:59.393 (16393527015)|SOQL_EXECUTE_BEGIN|[33]|Aggregations:0|select Id, Name, Email, HasOptedOutOfEmail from Contact 
09:28:59.418 (16418074120)|SOQL_EXECUTE_END|[33]|Rows:1
09:28:59.418 (16418167403)|SYSTEM_METHOD_ENTRY|[33]|Database.QueryLocator.iterator()
09:28:59.418 (16418328865)|SYSTEM_METHOD_ENTRY|[7]|QueryLocatorIterator.QueryLocatorIterator()
09:28:59.418 (16418343945)|SYSTEM_METHOD_EXIT|[7]|QueryLocatorIterator
09:28:59.418 (16418429734)|SYSTEM_METHOD_EXIT|[33]|Database.QueryLocator.iterator()
09:28:59.418 (16418447665)|SYSTEM_METHOD_ENTRY|[33]|Database.QueryLocatorIterator.hasNext()
09:28:59.418 (16418485833)|SYSTEM_METHOD_ENTRY|[33]|LIST<Contact>.size()
09:28:59.418 (16418516395)|SYSTEM_METHOD_EXIT|[33]|LIST<Contact>.size()
09:28:59.418 (16418532213)|SYSTEM_METHOD_EXIT|[33]|Database.QueryLocatorIterator.hasNext()
09:28:59.418 (16418543964)|SYSTEM_METHOD_ENTRY|[33]|Database.QueryLocatorIterator.next()
09:28:59.418 (16418562952)|SYSTEM_METHOD_ENTRY|[48]|Database.QueryLocatorIterator.hasNext()
09:28:59.418 (16418583322)|SYSTEM_METHOD_ENTRY|[33]|LIST<Contact>.size()
09:28:59.418 (16418594968)|SYSTEM_METHOD_EXIT|[33]|LIST<Contact>.size()
09:28:59.418 (16418605734)|SYSTEM_METHOD_EXIT|[48]|Database.QueryLocatorIterator.hasNext()
09:28:59.418 (16418645056)|SYSTEM_METHOD_EXIT|[33]|Database.QueryLocatorIterator.next()
09:28:59.418 (16418723679)|SYSTEM_METHOD_ENTRY|[41]|LIST<Contact>.add(Object)
09:28:59.418 (16418755646)|SYSTEM_METHOD_EXIT|[41]|LIST<Contact>.add(Object)
09:28:59.418 (16418766937)|SYSTEM_METHOD_ENTRY|[33]|Database.QueryLocatorIterator.hasNext()
09:28:59.418 (16418781516)|SYSTEM_METHOD_EXIT|[33]|Database.QueryLocatorIterator.hasNext()
09:28:59.418 (16418853617)|DML_BEGIN|[45]|Op:Update|Type:Contact|Rows:1
09:28:59.445 (16445474217)|DML_END|[45]
09:28:59.445 (16445960322)|SOQL_EXECUTE_BEGIN|[53]|Aggregations:0|select Id, Name, Email, HasOptedOutOfEmail from Lead 
09:28:59.477 (16477011223)|SOQL_EXECUTE_END|[53]|Rows:1
09:28:59.477 (16477077421)|SYSTEM_METHOD_ENTRY|[53]|Database.QueryLocator.iterator()
09:28:59.477 (16477308106)|SYSTEM_METHOD_EXIT|[53]|Database.QueryLocator.iterator()
09:28:59.477 (16477327336)|SYSTEM_METHOD_ENTRY|[53]|Database.QueryLocatorIterator.hasNext()
09:28:59.477 (16477368820)|SYSTEM_METHOD_ENTRY|[33]|LIST<Lead>.size()
09:28:59.477 (16477381191)|SYSTEM_METHOD_EXIT|[33]|LIST<Lead>.size()
09:28:59.477 (16477401035)|SYSTEM_METHOD_EXIT|[53]|Database.QueryLocatorIterator.hasNext()
09:28:59.477 (16477411846)|SYSTEM_METHOD_ENTRY|[53]|Database.QueryLocatorIterator.next()
09:28:59.477 (16477425170)|SYSTEM_METHOD_ENTRY|[48]|Database.QueryLocatorIterator.hasNext()
09:28:59.477 (16477439702)|SYSTEM_METHOD_ENTRY|[33]|LIST<Lead>.size()
09:28:59.477 (16477449821)|SYSTEM_METHOD_EXIT|[33]|LIST<Lead>.size()
09:28:59.477 (16477460138)|SYSTEM_METHOD_EXIT|[48]|Database.QueryLocatorIterator.hasNext()
09:28:59.477 (16477511767)|SYSTEM_METHOD_EXIT|[53]|Database.QueryLocatorIterator.next()
09:28:59.477 (16477588069)|SYSTEM_METHOD_ENTRY|[61]|LIST<Lead>.add(Object)
09:28:59.477 (16477623318)|SYSTEM_METHOD_EXIT|[61]|LIST<Lead>.add(Object)
09:28:59.477 (16477648698)|SYSTEM_METHOD_ENTRY|[63]|String.valueOf(Object)
09:28:59.477 (16477711423)|SYSTEM_METHOD_EXIT|[63]|String.valueOf(Object)
09:28:59.477 (16477729129)|SYSTEM_METHOD_ENTRY|[63]|System.debug(ANY)
09:28:59.477 (16477737311)|USER_DEBUG|[63]|DEBUG|Lead Object: Lead:{Name=Rasmus Mencke, Email=rmencke@salesforce.com, Id=00QU000000D4GPdMAN, HasOptedOutOfEmail=true}
09:28:59.477 (16477743969)|SYSTEM_METHOD_EXIT|[63]|System.debug(ANY)
09:28:59.477 (16477760011)|SYSTEM_METHOD_ENTRY|[53]|Database.QueryLocatorIterator.hasNext()
09:28:59.477 (16477772573)|SYSTEM_METHOD_EXIT|[53]|Database.QueryLocatorIterator.hasNext()
09:28:59.477 (16477815825)|DML_BEGIN|[66]|Op:Update|Type:Lead|Rows:1
09:28:59.535 (16535285783)|ENTERING_MANAGED_PKG|RT1
09:28:59.537 (16537085362)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Lead
09:28:59.537 (16537109734)|CODE_UNIT_FINISHED|Workflow:Lead
09:28:59.537 (16537182920)|DML_END|[66]
09:28:59.537 (16537249306)|SYSTEM_METHOD_ENTRY|[73]|System.debug(ANY)
09:28:59.537 (16537272785)|USER_DEBUG|[73]|DEBUG|Found the unsubscribe word in the subject line.
09:28:59.537 (16537281200)|SYSTEM_METHOD_EXIT|[73]|System.debug(ANY)
09:28:59.537 (16537302671)|METHOD_EXIT|[118]|01pU0000000uhEc|unsubscribe.handleInboundEmail(Messaging.InboundEmail, Messaging.InboundEnvelope)
09:28:51.358 (16537327168)|CUMULATIVE_LIMIT_USAGE
09:28:51.358|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 2 out of 100
  Number of query rows: 2 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 4 out of 150
  Number of DML rows: 4 out of 10000
  Number of code statements: 27 out of 200000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

09:28:51.358|LIMIT_USAGE_FOR_NS|RT1|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of code statements: 4 out of 200000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

09:28:51.358|CUMULATIVE_LIMIT_USAGE_END

09:28:59.537 (16537378369)|CODE_UNIT_FINISHED|unsubscribe.testUnsubscribe
09:28:59.537 (16537386450)|EXECUTION_FINISHED
09:28:59.597 (16597443514)|EXECUTION_STARTED
09:28:59.597 (16597456990)|CODE_UNIT_STARTED|[EXTERNAL]|01pU0000000uhEc|unsubscribe.testUnsubscribe2
09:28:59.597 (16597725101)|METHOD_ENTRY|[1]|01pU0000000uhEc|unsubscribe.unsubscribe()
09:28:59.597 (16597734223)|METHOD_EXIT|[1]|unsubscribe
09:28:59.597 (16597847377)|SYSTEM_CONSTRUCTOR_ENTRY|[125]|<init>()
09:28:59.597 (16597884686)|SYSTEM_CONSTRUCTOR_EXIT|[125]|<init>()
09:28:59.597 (16597903204)|SYSTEM_CONSTRUCTOR_ENTRY|[126]|<init>()
09:28:59.597 (16597917993)|SYSTEM_CONSTRUCTOR_EXIT|[126]|<init>()
09:28:59.598 (16598028373)|DML_BEGIN|[134]|Op:Insert|Type:Lead|Rows:1
09:28:59.689 (16689902323)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Lead
09:28:59.689 (16689934241)|CODE_UNIT_FINISHED|Workflow:Lead
09:28:59.692 (16692004222)|DML_END|[134]
09:28:59.692 (16692166896)|DML_BEGIN|[141]|Op:Insert|Type:Contact|Rows:1
09:28:59.724 (16724815820)|DML_END|[141]
09:28:59.724 (16724868518)|CONSTRUCTOR_ENTRY|[148]|01pU0000000uhEc|<init>()
09:28:59.724 (16724907249)|CONSTRUCTOR_EXIT|[148]|01pU0000000uhEc|<init>()
09:28:59.724 (16724922296)|METHOD_ENTRY|[149]|01pU0000000uhEc|unsubscribe.handleInboundEmail(Messaging.InboundEmail, Messaging.InboundEnvelope)
09:28:59.724 (16724975920)|SYSTEM_CONSTRUCTOR_ENTRY|[8]|<init>()
09:28:59.724 (16724998785)|SYSTEM_CONSTRUCTOR_EXIT|[8]|<init>()
09:28:59.725 (16725021927)|SYSTEM_CONSTRUCTOR_ENTRY|[11]|<init>()
09:28:59.725 (16725027792)|SYSTEM_CONSTRUCTOR_EXIT|[11]|<init>()
09:28:59.725 (16725036228)|SYSTEM_CONSTRUCTOR_ENTRY|[12]|<init>()
09:28:59.725 (16725039402)|SYSTEM_CONSTRUCTOR_EXIT|[12]|<init>()
09:28:59.725 (16725076379)|SYSTEM_METHOD_ENTRY|[76]|System.debug(ANY)
09:28:59.725 (16725093675)|USER_DEBUG|[76]|DEBUG|No Unsuscribe word found in the subject line.
09:28:59.725 (16725099499)|SYSTEM_METHOD_EXIT|[76]|System.debug(ANY)
09:28:59.725 (16725108978)|METHOD_EXIT|[149]|01pU0000000uhEc|unsubscribe.handleInboundEmail(Messaging.InboundEmail, Messaging.InboundEnvelope)
09:28:51.546 (16725133358)|CUMULATIVE_LIMIT_USAGE
09:28:51.546|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 2 out of 150
  Number of DML rows: 2 out of 10000
  Number of code statements: 20 out of 200000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

09:28:51.546|CUMULATIVE_LIMIT_USAGE_END

09:28:59.725 (16725166488)|CODE_UNIT_FINISHED|unsubscribe.testUnsubscribe2
09:28:59.725 (16725171193)|EXECUTION_FINISHED

 

cmoylecmoyle

I wasn't quite able to figure out what would be causing your null pointer exception. However, there were several things wrong with your trigger code that may contribute.

 

When looping over your records in Trigger.new, you shouldn't be doing DML or SOQL statements inside that loop. Also, since you just want to reference tasks related to accounts, its best to use just the AccountId field rather than the WhatId field. The code below has been bulkified and you may have better results with it:

 

trigger AccountUpdateonLogCall on Task (after insert, after update) {
	Set <ID> acctIDs = new Set <ID> ();

	for (Task t: Trigger.new){
		if(t.AccountId != null)
			acctIDs.add(t.accountID);
	}
	Map<ID, Account> acctMap = new Map<ID, Account> ([Select ID, Proactive_Communication_Notes__c, Low_Redemption_Notes__c  from Account where ID in :acctIDs]);
    
	for(Task t : Trigger.new){
		Account acctRec = acctMap.get(t.AccountId);
		if(acctRec != null){
			if (t.accountID != null && t.Purpose__c.Equals('Proactive Communication') && t.Status == 'Completed'){
				acctRec.Proactive_Communication_Notes__c = t.Description;
			}else if (t.accountID != null &&  t.Purpose__c == ('Low-Redemption Communication') && t.Status == ('Completed')){
				  acctRec.Proactive_Communication_Notes__c = t.Description;
				  acctRec.Low_Redemption_Notes__c = t.Description;
			}
		}
	}
	
	update acctMap.values();
  	  
}
  

 

MBarnardMBarnard

This is my second ever trigger so I am sure I made a bunch of mistakes...

 

Testing it now.

MBarnardMBarnard

So, my Test Class is erroring out on your Trigger, just like it was on mine before I changed AcctID to WhatID.

 

This is my test class:

private class TestClass {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        test.startTest();
        Account a = new Account(name='Test');
        insert a;
        Account b = new Account(name='Test2');
        insert b;
        
 		       
        Task t = new Task(subject='Test Activity', Status = 'Completed', Purpose__c = 'Low-Redemption Communication', Description = 'I have Called This Dealership', Type = 'Call', whatId = a.ID);
        insert t;
        Task t2 = new Task(subject='Test Activity', Status = 'Completed', Purpose__c = 'Proactive Communication', Description = 'I have Called This Dealership2', Type = 'Call', whatId = b.ID);
        insert t2;
        
        
        a = [SELECT ID, Low_Redemption_Notes__c, Proactive_Communication_Notes__c  FROM Account WHERE ID = :a.ID];
        b = [SELECT ID, Low_Redemption_Notes__c, Proactive_Communication_Notes__c  FROM Account WHERE ID = :b.ID];
        System.assertEquals(t.Description, a.Low_Redemption_Notes__c);
        System.assertEquals(t.Description, a.Proactive_Communication_Notes__c);
        System.assertEquals(t2.Description, b.Proactive_Communication_Notes__c);
 		test.stopTest();
    }
}

 The Error:

Description	Resource	Path	Location	Type
System.AssertException: Assertion Failed: Expected: I have Called This Dealership, Actual: null	TestClass.cls	/ActivitytoAccountUpdate/src/classes	line 42	Force.com run test failure

 

 

 

KodiKodi

HI,
In Task object not have accountID field. so you have to use "WhatId".

change ur code

acctIDs.add(t.accountID); change into acctIDs.add(t.whatid);

like that will be change your all codes.

MBarnardMBarnard

So i switched back to 'WhatID' and everything works out.

 

However, I am not getting 'attempt to de-reference a null object' when i go to Send an Email from an account.

 

There is no 'purpose' on 'send an email' and it is only available on logged calls/tasks.

 

any input?