You need to sign in to do that
Don't have an account?
Nick Mirams
Trigger to update the task status after Lead Status is 'Closed Lost' or 'Qualified'
HI,
I am getting this error, can anyone help?: -
System.NullPointerException: Attempt to de-reference a null object: Trigger.NickMiramsTaskLeadUpdate: line 30, column 1
heres the code...I am trying to write a trigger to close a task as 'Completed' after a lead status becomes Closed Lost or Qualified...
trigger NickMiramsTaskLeadUpdate on Lead (after update){
set<id> leadids = new set<id>();
map<id,list<task>> leadtotaskmap = new map<id,list<task>>();
list<task> taskstoupdate = new list<task>();
for(lead Lead : trigger.new){
leadids.add(lead.id);
}
list<task> tasklist = [select whatid,status from task where whatid in : leadids];
for(task t : tasklist){
if(leadtotaskmap.get(t.whatid)!=null){
list<task> temp = leadtotaskmap.get(t.whatid);
temp.add(t);
leadtotaskmap.put(t.whatid, temp);
}
else
{
leadtotaskmap.put(t.whatid, new list<task>{t});
}
}
for(lead lead : trigger.new){
if(lead.status=='Closed Lost' || lead.status=='Qualified'){
list<Task> tasklisttemp = leadtotaskmap.get(lead.id);
for(task t : tasklisttemp){
t.status='completed';
taskstoupdate.add(t);
}
}
}
if(taskstoupdate.size()>0)
update taskstoupdate;
}
I am getting this error, can anyone help?: -
System.NullPointerException: Attempt to de-reference a null object: Trigger.NickMiramsTaskLeadUpdate: line 30, column 1
heres the code...I am trying to write a trigger to close a task as 'Completed' after a lead status becomes Closed Lost or Qualified...
trigger NickMiramsTaskLeadUpdate on Lead (after update){
set<id> leadids = new set<id>();
map<id,list<task>> leadtotaskmap = new map<id,list<task>>();
list<task> taskstoupdate = new list<task>();
for(lead Lead : trigger.new){
leadids.add(lead.id);
}
list<task> tasklist = [select whatid,status from task where whatid in : leadids];
for(task t : tasklist){
if(leadtotaskmap.get(t.whatid)!=null){
list<task> temp = leadtotaskmap.get(t.whatid);
temp.add(t);
leadtotaskmap.put(t.whatid, temp);
}
else
{
leadtotaskmap.put(t.whatid, new list<task>{t});
}
}
for(lead lead : trigger.new){
if(lead.status=='Closed Lost' || lead.status=='Qualified'){
list<Task> tasklisttemp = leadtotaskmap.get(lead.id);
for(task t : tasklisttemp){
t.status='completed';
taskstoupdate.add(t);
}
}
}
if(taskstoupdate.size()>0)
update taskstoupdate;
}
HI Nick - the error you get is because in task the realted field to Leads is "whoId" field and there fore your query never picks the tasks records and you get a null pointer exception. if you change all the whatId to whoId it will work.. but few more suggestions to make your code more better you can do this...
change the query, to add one more condition so that you dont select compelted task
in the first for loop at the condition so that we dont select the task whcih we are not intending to work on,
and then in your last for loop make these changes to avoid the NULL pointer exception is future if your Lead does not have any Task associated to it.
Cheers
All Answers
HI Nick - the error you get is because in task the realted field to Leads is "whoId" field and there fore your query never picks the tasks records and you get a null pointer exception. if you change all the whatId to whoId it will work.. but few more suggestions to make your code more better you can do this...
change the query, to add one more condition so that you dont select compelted task
in the first for loop at the condition so that we dont select the task whcih we are not intending to work on,
and then in your last for loop make these changes to avoid the NULL pointer exception is future if your Lead does not have any Task associated to it.
Cheers
I have altered your code and the below code is working fine.
PS: if this answers your question then hit Like and mark it as solution!