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
Mary CMary C 

Apex Trigger Error

We are receiving the following error message from one of the Apex Triggers set up by a consultant:

Apex script unhandled trigger exception
TaskBeforeInsertUpdate: execution of BeforeInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.TaskBeforeInsertUpdate: line 61, column 1

I have also attached line 61 of the code in the system. Can someone tell me what is causing this error?

                    t.Subject = t.Subject + ' - ' + IdContactMap.get(t.WhoId).Name;
sreenivasAppssreenivasApps
Hi Mary,

Before assigning the value, check the map whether it contains the key, value or not.
like: IdContactMap.containsKey(t.WhoId) && IdContactMap.get(t.WhoId) != Null 
Mary CMary C
I believe that is in the map but I am not 100% possitive so I have inlcuded everything below. 

9     trigger TaskBeforeInsertUpdate on Task (before insert, before update) {
10    
11    Set<Id> LeadIdSet = new Set<Id>();
12    Set<Id> ContactIdSet = new Set<Id>();
13    for(Task t : trigger.new){
14        if(t.WhoId != null){
15            if(t.WhoId.getSObjectType().getDescribe().getName() == 'Lead')
16                LeadIdSet.add(t.WhoId);
18            if(t.WhoId.getSObjectType().getDescribe().getName() == 'Contact')
19                ContactIdSet.add(t.WhoId); }} 
20    
21    Map<Id,Lead> IdLeadMap = new Map<Id,Lead>([Select id, Name From Lead Where Id IN :LeadIdSet]);
22    Map<Id,Contact> IdContactMap = new Map<Id,Contact>([Select id, Name From Contact Where Id IN :ContactIdSet]);
23    
24    for(Task t : trigger.new){
25        
26        if(Trigger.isUpdate){
27            String TaskSuffix;
28            
29            if(t.WhoId != null){
30                if(t.WhoId.getSObjectType().getDescribe().getName() == 'Lead'){
31                    TaskSuffix = ' - ' + IdLeadMap.get(t.WhoId).Name;
32                    if(t.Subject != trigger.oldMap.get(t.Id).Subject){
33                        if(!(t.Subject.contains(TaskSuffix)) && !(trigger.oldMap.get(t.Id).Subject.contains(TaskSuffix))){
34                            t.Subject = t.Subject + TaskSuffix; }}}
35                    
36                if(t.WhoId.getSObjectType().getDescribe().getName() == 'Contact' ){
37                    TaskSuffix = ' - ' + IdContactMap.get(t.WhoId).Name;
38                    
39                    if(!(t.Subject.contains(TaskSuffix)) && trigger.oldMap.get(t.Id).Subject.contains(TaskSuffix)){
40                        t.Subject = t.Subject + TaskSuffix;}}}}
41        
42        else{
43            if(t.WhoId != null){
44                if(t.WhoId.getSObjectType().getDescribe().getName() == 'Lead')
45                    t.Subject = t.Subject + ' - ' + IdLeadMap.get(t.WhoId).Name;
46                    
47                if(t.WhoId.getSObjectType().getDescribe().getName() == 'Contact' )
48                    t.Subject = t.Subject + ' - ' + IdContactMap.get(t.WhoId).Name;}}}