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
Nick MiramsNick 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;

}
Best Answer chosen by Nick Mirams
Mohammed HaseebMohammed Haseeb
Mohammed Haseeb
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

List<task> tasklist = [select whoId,status from task where  Status != 'completed' AND whoId in : leadids];



in the first for loop at the condition so that we dont select the task whcih we are not intending to work on,

for(lead Lead : trigger.new){
 if(lead.status=='Closed Lost' || lead.status=='Qualified'){
   leadids.add(lead.id);
 }
}


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.


for(lead lead : trigger.new){

    if((lead.status=='Closed - Not Converted' || lead.status=='Qualified') && leadtotaskmap.containsKey(lead.id)){
        for(task t : leadtotaskmap.get(lead.id)){
            t.status='completed';
            taskstoupdate.add(t);
        }
    }
}



Cheers

All Answers

Mohammed HaseebMohammed Haseeb
Mohammed Haseeb
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

List<task> tasklist = [select whoId,status from task where  Status != 'completed' AND whoId in : leadids];



in the first for loop at the condition so that we dont select the task whcih we are not intending to work on,

for(lead Lead : trigger.new){
 if(lead.status=='Closed Lost' || lead.status=='Qualified'){
   leadids.add(lead.id);
 }
}


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.


for(lead lead : trigger.new){

    if((lead.status=='Closed - Not Converted' || lead.status=='Qualified') && leadtotaskmap.containsKey(lead.id)){
        for(task t : leadtotaskmap.get(lead.id)){
            t.status='completed';
            taskstoupdate.add(t);
        }
    }
}



Cheers
This was selected as the best answer
Arunkumar RArunkumar R
Hi Nick,

I have altered your code and the below code is working fine.

trigger NickMiramsTaskLeadUpdate on Lead (after update)
{

    Map<Id, Lead> leadMap = new Map<Id, Lead>([select id,Status from Lead where id in:Trigger.newMap.keyset()]);
    
    Map<Id, Task> taskMap= new Map<Id, Task>([select whoid,status from task where whoid in : Trigger.newMap.keyset()]);
    
    if(taskMap.size() > 0)
    {
        for(Task currentTask : taskMap.values())
        {
            if(leadMap.get(currentTask.whoid).Status=='Closed Lost' || leadMap.get(currentTask.whoid).Status =='Qualification')
            {
                currentTask.status = 'Completed';
            }
        }
    
    update taskMap.values();
    
    }
}


Sumitkumar_ShingaviSumitkumar_Shingavi
You code should be as simple as below:
trigger NickMiramsTaskLeadUpdate on Lead (after update){

	Set<Id> leadids = new Set<Id>();
	List<Task> taskstoupdate = new list<Task>();
	
	//Fetch only Leads which follow criteria
	for(Lead ld : trigger.new){
		if((ld.Status == 'Closed Lost' || ld.Status == 'Qualified')
		&& ld.Status != Trigger.oldMap.get(ld.Id)) {
			leadids.add(lead.id);
		}
	}
	
	//Iterate over all Leads and modify Tasks for them + Add to a list variable	
	for(Lead ld : [SELECT Id, (SELECT Id, Whatid, Status FROM Tasks) Where Id in : leadids)]) {
		for(Task t : ld.Tasks) {
			t.Status = 'Completed';
			taskstoupdate.add(t);
		}
	}
	
	//Update Tasks
	if(taskstoupdate.size()>0) {
		update taskstoupdate;
	}
}


PS: if this answers your question then hit Like and mark it as solution!