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
AnniAnni 

trigger to update the Task Status on Lead conversion

Hi All,

 

 

I need to write a trigger to update the Task Status to 'Completed' . when the Lead is converted to Opportunity.

Please help me to sort out this issue

 

regards,

Anni

 

 

logontokartiklogontokartik

Please try the below. If you have any custom statuses when doing conversion, add them to if condition

 

trigger LeadTaskStatus on Lead (before update) {

    Set<Id> OppIds = new Set<Id>();
    
    for(Lead l : Trigger.new){
    
        if(l.status == 'Closed - Converted')
            oppIds.add(l.ConvertedOpportunityId);
    
    }
    
    List<Task> leadTasks = [Select Id, Status from Task where WhatId in :OppIds];
    for(Task t : leadTasks)
        t.Status = 'Completed';
    
    update leadTasks;
    
}

 

s_k_as_k_a

Try This code

Set<Id> OppIds = new Set<Id>();
	   
    for(Lead l : [SELECT  IsConverted, ConvertedContactId, ConvertedOpportunityId, Id, Status FROM Lead where isConverted =true and convertedOpportunityId !=null limit 1]){
    
        if(l.isConverted == true && l.ConvertedOpportunityId!=null )
        {
            oppIds.add(l.ConvertedOpportunityId);
		    
        }
    }
    
    List<Task> TasksToUpdate = new List<Task>();
   	for(Task t : [Select Id, Status from Task where WhatId in :OppIds])
	{
        t.Status = 'Completed';
		TasksToUpdate(t);
	}
	
	update TasksToUpdate;

 

AnniAnni

Hi Karthik,

 

The trigger you sent is not working.

Even i try to write the trigger to change the status to 'completed' on lead conversion as follows

 

trigger updatestatusoftask on Task (before insert) {
List<Lead> leads = [SELECT Id,IsConverted from Lead];
for(Task task : Trigger.new){
for(Lead l: leads){


if((l.IsConverted == true)&&(l.Id==task.WhoId)){

task.Status='Completed';
insert task;

}
}

}

}

 

even this trigger isn't working.

 

Regards,

Arun

s_k_as_k_a

Try this code.This code is working for me.

 

trigger taskstatusUpdate on Task (before Insert, before Update) {

if((trigger.isUpdate || trigger.isInsert)&& trigger.isBefore)
{
   Set<Id> WhatIds = new Set<Id>();
   Set<Id> whoIDs =new Set<ID>();
   for(task t : trigger.new)
   { 
    whatIds.add(t.WhatId);
    whoIds.add(t.whoId);
   }
   Map<id, Id> oppToLeadMap = new Map<id, Id>();
   for(lead l : [SELECT  IsConverted,ConvertedContactId, ConvertedOpportunityId, Id FROM Lead where isConverted =true and convertedOpportunityId in: whatIds and ConvertedContactId in: whoIds])
   {
      oppToLeadMap.put(l.ConvertedOpportunityId, l.ConvertedContactId);
   }
   for(task t : trigger.new)
   {
       if(OppToleadMap.containsKey(t.whatId)&& t.whoId == OppToleadMap.get(t.whatId))
       {
           t.status ='Completed';
       }
   }
}
}

 

logontokartiklogontokartik

I feel its not a best practice to write a trigger on a Task when you have business logic you need to implement on a Lead, because Tasks are common for all objects and if you write a trigger on a Task, it gets executed every single time regardless of it being a lead, or some other object. 

 

Can you elaborate or tell me what you meant by, its not working? Can you set a Debug log and see if the trigger on Lead is executed when you are converting a Lead? and paste it here?

s_k_as_k_a

Hi Karthik,

 

In this logic I am changing status of the Tasks  that are inserted or updated only when a lead is converted to opportunity.

 

I compiled and tested that code. i didn't get any errors.

 

Here I will explain:

 

First i am getting any task before insert or update operation. And then i am collecting what Id and whoId

by using Sets.

 

throug  this query  i am creating a OppTo leadMap. 

 

This Map can contains  convertedOpportunityId (which equals to whatID on task) and convertedcontactId (equals to WhoId  in task only when IsConverted field on lead is set to true. 

 

SELECT  IsConverted,ConvertedContactId, ConvertedOpportunityId, Id FROM Lead where isConverted =true and convertedOpportunityId in: whatIds and ConvertedContactId in: whoIds]

 

After that i am checking whether the task whaId and WhoId in the map 

 

OppToleadMap.containsKey(t.whatId)&& t.whoId == OppToleadMap.get(t.whatId

 

So that the status feild of the task does not updated to 'completed'. for other tasks, unless it is created by converting a lead.

 

 

In this BeforeUpdate event on trigger is not necessary.

 

 

 

 

 

 

AnniAnni

Hi Karthik,

 

I applied the trigger you gave . As soon as lead is converted to the opportunity the Task status remains 'Not-started' .

I have shown the Debug log below 

 

 

 

Log

20.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO

02:12:37.300 (300922000)|CODE_UNIT_STARTED|[EXTERNAL]|01q90000000KpOF|accountpriority on Account trigger event BeforeInsert for [new]

02:12:37.301 (301561000)|SYSTEM_METHOD_ENTRY|[3]|LIST<Account>.iterator()

02:12:37.301 (301867000)|SYSTEM_METHOD_EXIT|[3]|LIST<Account>.iterator()

02:12:37.301 (301900000)|SYSTEM_METHOD_ENTRY|[3]|system.ListIterator.hasNext()

02:12:37.301 (301920000)|SYSTEM_METHOD_EXIT|[3]|system.ListIterator.hasNext()

02:12:37.301 (301989000)|SYSTEM_METHOD_ENTRY|[3]|system.ListIterator.hasNext()

02:12:37.302 (302004000)|SYSTEM_METHOD_EXIT|[3]|system.ListIterator.hasNext()

02:12:38.204 (302027000)|CUMULATIVE_LIMIT_USAGE

02:12:38.204|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: 0 out of 150

  Number of DML rows: 0 out of 10000

  Number of script statements: 1 out of 200000

  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

 

02:12:38.204|CUMULATIVE_LIMIT_USAGE_END

 

02:12:37.302 (302092000)|CODE_UNIT_FINISHED|accountpriority on Account trigger event BeforeInsert for [new]

02:12:37.775 (775759000)|CODE_UNIT_STARTED|[EXTERNAL]|01q90000000KpbJ|updateAccount on Contact trigger event AfterInsert for [0039000000J0MVk]

02:12:37.776 (776073000)|SYSTEM_CONSTRUCTOR_ENTRY|[2]|<init>()

02:12:37.776 (776118000)|SYSTEM_CONSTRUCTOR_EXIT|[2]|<init>()

02:12:37.776 (776178000)|SYSTEM_METHOD_ENTRY|[3]|LIST<Contact>.iterator()

02:12:37.776 (776493000)|SYSTEM_METHOD_EXIT|[3]|LIST<Contact>.iterator()

02:12:37.776 (776528000)|SYSTEM_METHOD_ENTRY|[3]|system.ListIterator.hasNext()

02:12:37.776 (776549000)|SYSTEM_METHOD_EXIT|[3]|system.ListIterator.hasNext()

02:12:37.776 (776652000)|SYSTEM_METHOD_ENTRY|[4]|String.valueOf(Object)

02:12:37.776 (776685000)|SYSTEM_METHOD_EXIT|[4]|String.valueOf(Object)

02:12:37.776 (776708000)|SYSTEM_METHOD_ENTRY|[4]|System.debug(ANY)

02:12:37.776 (776722000)|USER_DEBUG|[4]|DEBUG|Account id is 0019000000KG8FQAA1

02:12:37.776 (776728000)|SYSTEM_METHOD_EXIT|[4]|System.debug(ANY)

02:12:37.776 (776884000)|SYSTEM_METHOD_ENTRY|[5]|LIST<Account>.add(Object)

02:12:37.776 (776922000)|SYSTEM_METHOD_EXIT|[5]|LIST<Account>.add(Object)

02:12:37.776 (776987000)|DML_BEGIN|[9]|Op:Update|Type:Account|Rows:1

02:12:37.783 (783551000)|CODE_UNIT_STARTED|[EXTERNAL]|01q90000000KpOF|accountpriority on Account trigger event BeforeUpdate for [0019000000KG8FQ]

02:12:37.783 (783714000)|SYSTEM_METHOD_ENTRY|[3]|LIST<Account>.iterator()

02:12:37.783 (783731000)|SYSTEM_METHOD_EXIT|[3]|LIST<Account>.iterator()

02:12:37.783 (783740000)|SYSTEM_METHOD_ENTRY|[3]|system.ListIterator.hasNext()

02:12:37.783 (783753000)|SYSTEM_METHOD_EXIT|[3]|system.ListIterator.hasNext()

02:12:37.783 (783800000)|SYSTEM_METHOD_ENTRY|[3]|system.ListIterator.hasNext()

02:12:37.783 (783814000)|SYSTEM_METHOD_EXIT|[3]|system.ListIterator.hasNext()

02:12:38.685 (783839000)|CUMULATIVE_LIMIT_USAGE

02:12:38.685|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: 1 out of 150

  Number of DML rows: 1 out of 10000

  Number of script statements: 6 out of 200000

  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

 

02:12:38.685|CUMULATIVE_LIMIT_USAGE_END

 

02:12:37.783 (783903000)|CODE_UNIT_FINISHED|accountpriority on Account trigger event BeforeUpdate for [0019000000KG8FQ]

02:12:37.798 (798016000)|DML_END|[9]

02:12:37.798 (798057000)|SYSTEM_METHOD_ENTRY|[3]|system.ListIterator.hasNext()

02:12:37.798 (798073000)|SYSTEM_METHOD_EXIT|[3]|system.ListIterator.hasNext()

02:12:38.700 (798093000)|CUMULATIVE_LIMIT_USAGE

02:12:38.700|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: 1 out of 150

  Number of DML rows: 1 out of 10000

  Number of script statements: 6 out of 200000

  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

 

02:12:38.700|CUMULATIVE_LIMIT_USAGE_END

 

02:12:37.798 (798141000)|CODE_UNIT_FINISHED|updateAccount on Contact trigger event AfterInsert for [0039000000J0MVk]

02:12:37.944 (944034000)|CODE_UNIT_STARTED|[EXTERNAL]|01q90000000KAZn|opportunitytrigger on Opportunity trigger event BeforeInsert for [new]

02:12:38.846 (944368000)|CUMULATIVE_LIMIT_USAGE

02:12:38.846|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: 1 out of 150

  Number of DML rows: 1 out of 10000

  Number of script statements: 8 out of 200000

  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

 

02:12:38.846|CUMULATIVE_LIMIT_USAGE_END

 

02:12:37.944 (944441000)|CODE_UNIT_FINISHED|opportunitytrigger on Opportunity trigger event BeforeInsert for [new]

02:12:38.270 (1270765000)|CODE_UNIT_STARTED|[EXTERNAL]|01q90000000KpOK|duplicatename on Opportunity trigger event AfterInsert for [006900000090loz]

02:12:38.271 (1271122000)|SYSTEM_METHOD_ENTRY|[3]|LIST<Opportunity>.iterator()

02:12:38.271 (1271443000)|SYSTEM_METHOD_EXIT|[3]|LIST<Opportunity>.iterator()

02:12:38.271 (1271477000)|SYSTEM_METHOD_ENTRY|[3]|system.ListIterator.hasNext()

02:12:38.271 (1271499000)|SYSTEM_METHOD_EXIT|[3]|system.ListIterator.hasNext()

02:12:38.271 (1271554000)|SYSTEM_METHOD_ENTRY|[3]|system.ListIterator.hasNext()

02:12:38.271 (1271569000)|SYSTEM_METHOD_EXIT|[3]|system.ListIterator.hasNext()

02:12:39.173 (1271593000)|CUMULATIVE_LIMIT_USAGE

02:12:39.173|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: 1 out of 150

  Number of DML rows: 1 out of 10000

  Number of script statements: 9 out of 200000

  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

 

02:12:39.173|CUMULATIVE_LIMIT_USAGE_END

 

02:12:38.271 (1271654000)|CODE_UNIT_FINISHED|duplicatename on Opportunity trigger event AfterInsert for [006900000090loz]

02:12:38.804 (1804013000)|CODE_UNIT_STARTED|[EXTERNAL]|01q90000000KqWb|LeadTaskStatus on Lead trigger event BeforeUpdate for [00Q9000000DU9M9]

02:12:38.804 (1804329000)|SYSTEM_CONSTRUCTOR_ENTRY|[3]|<init>(Integer)

02:12:38.804 (1804399000)|SYSTEM_CONSTRUCTOR_EXIT|[3]|<init>(Integer)

02:12:38.804 (1804471000)|SYSTEM_METHOD_ENTRY|[5]|LIST<Lead>.iterator()

02:12:38.804 (1804531000)|SYSTEM_METHOD_EXIT|[5]|LIST<Lead>.iterator()

02:12:38.804 (1804562000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()

02:12:38.804 (1804593000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()

02:12:38.804 (1804838000)|SYSTEM_METHOD_ENTRY|[8]|SET<Id>.add(Object)

02:12:38.804 (1804901000)|SYSTEM_METHOD_EXIT|[8]|SET<Id>.add(Object)

02:12:38.804 (1804920000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()

02:12:38.804 (1804943000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()

02:12:38.805 (1805574000)|SOQL_EXECUTE_BEGIN|[12]|Aggregations:0|select Id, Status from Task where WhatId = :tmpVar1

02:12:38.846 (1846269000)|SOQL_EXECUTE_END|[12]|Rows:0

02:12:38.846 (1846400000)|SYSTEM_METHOD_ENTRY|[13]|LIST<Task>.iterator()

02:12:38.846 (1846459000)|SYSTEM_METHOD_EXIT|[13]|LIST<Task>.iterator()

02:12:38.846 (1846480000)|SYSTEM_METHOD_ENTRY|[13]|system.ListIterator.hasNext()

02:12:38.846 (1846501000)|SYSTEM_METHOD_EXIT|[13]|system.ListIterator.hasNext()

02:12:39.748 (1846582000)|CUMULATIVE_LIMIT_USAGE

02:12:39.748|LIMIT_USAGE_FOR_NS|(default)|

  Number of SOQL queries: 1 out of 100

  Number of query rows: 0 out of 50000

  Number of SOSL queries: 0 out of 20

  Number of DML statements: 1 out of 150

  Number of DML rows: 1 out of 10000

  Number of script statements: 14 out of 200000

  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

 

02:12:39.748|CUMULATIVE_LIMIT_USAGE_END

 

02:12:38.846 (1846658000)|CODE_UNIT_FINISHED|LeadTaskStatus on Lead trigger event BeforeUpdate for [00Q9000000DU9M9]

 

Regards,

Arun

 

AnniAnni

Thanks Anusha.

Your  trigger worked like charm.

 

 

Arun

 

s_k_as_k_a

Hi,

 

Can you make my answer as accept as solution. It would be helpful to others.

 

thanks,

Nihar SharmaNihar Sharma
Hello s_k_a,

I know it's too old post but i am facing one issue regarding update status of task.

i have two task, one is already in related list of lead and one is from lead conversation process which is standard

i used your trigger and it's working fine for the one which is from lead conversation process but it's not working for the one which is already exist on the lead and that task move to account, contact and opportunity

can you please me out ?

Thanks
-Nihar
Afroz Ahmed 9Afroz Ahmed 9
Strangely, if the task details are entered on the lead conversion page, then the lead before and after triggers are called at the last (Lead after event).
Even adding an error on the task trigger (task.addError) will not show any error on screen and will not stop the account, contact, opportunity records being created, but only the task record will not be created for opportunity.

So the final flow is
1.0 Account Custom Field Mappings
1.1 Account Before (Fires based on lead settings)
1.2 Account After
2.0 Contact Custom Field Mappings
2.1 Contact Before (Fires based on lead settings)
2.2 Contact After
3.0 Opportunity Field Mappings
3.1 Opportunity Before (Fires based on lead settings)
3.2 Opportunity After (OCR's not available)
4.0 Lead Before
4.1 Lead After (OCR's available)
5.0 Commit to database
6.0 Task Before
6.1 Task After
7.0 Commit to database (task)

-Afroz