You need to sign in to do that
Don't have an account?
DannyTK
Attempting to clean up trigger to avoid a System.NullPointerException: Attempt to de-reference a null object error message
Good Morning,
I'm not a developer but I put together a trigger to update a checkbox field (Trigger_Email_on_Outstanding_Task__c) on the related Lead Object if the corresponding field on a Task (Trigger_email_on_Outstanding_Task__c) is checked (by a workflow):
____________________________________________________________________________________
trigger TriggerTaskNotification on Task (after insert, after update) {
Set<Id> leadIds=new Set<Id>();
for(Task t:trigger.new){
if(t.Trigger_Email_on_Outstanding_Task__c==true){
if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
if( !leadIds.contains(t.whoid)){ //Dont forget '!'
leadIds.add(t.whoId);
}//if3
}//if 2
}//if 1
}//for
/* List<Lead> leadsToUpdate=[SELECT Id, Trigger_Email_on_Outstanding_Task__c FROM Lead WHERE Id IN :leadIds AND Trigger_Email_on_Outstanding_Task__c= True]; */
Map<Id,Lead> relatedleads = new Map<Id,Lead>([SELECT Id, Trigger_Email_on_Outstanding_Task__c FROM Lead WHERE Id IN :leadIds]);
For(Task t2 : trigger.new){
if(t2.Trigger_Email_on_Outstanding_Task__c==true){
if(relatedleads.get(t2.whoid) != null){ //if task is related to lead
//make lead field to 'checked'
relatedleads.get(t2.whoid).Trigger_Email_on_Outstanding_Task__c = true;
}//if2
}//if1
}//for
try{
update relatedleads.values();
}catch(DMLException e){
system.debug('Leads were not all properly updated. Error: '+e);
}
}
______________________________________________________________________________
Looks like i'm receiving a System.NullPointerException: Attempt to de-reference a null object error, on execution of AfterUpdate
from line 8, column 1
if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
I'm assuming that it may be due to a task that doesn't have a related lead record but where the workflow has checked the checkbox on the task. Is there a line I can omit or add that can account for these instances, where if there's a task with the checkbox checked but with no associated whoid then ignore....i'm assuming my trigger isn't very cleanly written since it's written by me.
thanks everyone
I'm not a developer but I put together a trigger to update a checkbox field (Trigger_Email_on_Outstanding_Task__c) on the related Lead Object if the corresponding field on a Task (Trigger_email_on_Outstanding_Task__c) is checked (by a workflow):
____________________________________________________________________________________
trigger TriggerTaskNotification on Task (after insert, after update) {
Set<Id> leadIds=new Set<Id>();
for(Task t:trigger.new){
if(t.Trigger_Email_on_Outstanding_Task__c==true){
if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
if( !leadIds.contains(t.whoid)){ //Dont forget '!'
leadIds.add(t.whoId);
}//if3
}//if 2
}//if 1
}//for
/* List<Lead> leadsToUpdate=[SELECT Id, Trigger_Email_on_Outstanding_Task__c FROM Lead WHERE Id IN :leadIds AND Trigger_Email_on_Outstanding_Task__c= True]; */
Map<Id,Lead> relatedleads = new Map<Id,Lead>([SELECT Id, Trigger_Email_on_Outstanding_Task__c FROM Lead WHERE Id IN :leadIds]);
For(Task t2 : trigger.new){
if(t2.Trigger_Email_on_Outstanding_Task__c==true){
if(relatedleads.get(t2.whoid) != null){ //if task is related to lead
//make lead field to 'checked'
relatedleads.get(t2.whoid).Trigger_Email_on_Outstanding_Task__c = true;
}//if2
}//if1
}//for
try{
update relatedleads.values();
}catch(DMLException e){
system.debug('Leads were not all properly updated. Error: '+e);
}
}
______________________________________________________________________________
Looks like i'm receiving a System.NullPointerException: Attempt to de-reference a null object error, on execution of AfterUpdate
from line 8, column 1
if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
I'm assuming that it may be due to a task that doesn't have a related lead record but where the workflow has checked the checkbox on the task. Is there a line I can omit or add that can account for these instances, where if there's a task with the checkbox checked but with no associated whoid then ignore....i'm assuming my trigger isn't very cleanly written since it's written by me.
thanks everyone
BakT
Hi Danny, please add a statement that will make sure that the whoId is not null.