I have written a trigger to create a new task related to a Lead with some information, this is working properly

The issue is when I convert this lead, related task will automatically move to converted account, contact and opportunity from lead and status is 'New' as it is in the lead's task and I want to change that task status to 'Qualified' but no luck.

I have created another trigger on task to set status as 'Qualified' but it's not working for the task which is moved from lead to account, contact and opportunity

Trigger of Lead for create new task :

trigger LeadandTask on Lead (after insert) {
   List<task> lstTask = new  List<task>();

   RecordType objrecordtype = [select id,Name from recordtype where name = 'TaskLead' limit 1];

   for(Lead l : Trigger.New){
       if(l.TaskCreation__C == true){
          datetime cd = l.CreatedDate;
          date d = Date.newInstance(cd.year(),cd.month(),cd.day());
          Task objtask = new Task();
          objtask.subject = 'Qual';  
          objtask.whoId = l.id;
          objtask.Status = 'New'; 
          objtask.ActivityDate = system.today();
          objtask.priority = 'Normal';
          objtask.Ownerid = l.OwnerId;
          objtask.Description = l.Description; 
          objtask.recordtypeid = objrecordtype.id;
          objtask.Need_Identified_Date__c = d;
          objtask.email__C = l.Email;
          objtask.phone__C = l.phone;

   if(lstTask.size() > 0){
      insert lstTask;

Trigger of Task for set status 'Qualified' :

trigger taskstatusUpdate on Task (before Insert, before Update, after 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){
      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 = 'Qualified';

Have you tried to change the objtask.Status = 'New'; to objtask.Status = 'Qualified'; ?

It looks like you are setting up the value 'New' on that line in your code.


If you want to set that task in when a Lead is converted you need to explore the option IsConverted=true in your SOQL and maybe change from after insert to after update. If all new Leads will have the 'New' value as status on that Task, the only time when you want to fire your trigger is after update.


I have been playing around a bit and I think you could use something like this:

trigger TaskOnConverteLeads on Lead (after update) {

    Map<Id, String> leadMap = new Map<Id, String>();
    List<Task> taskList = new List<Task>(); 

    for(Lead l: trigger.new)
        if(l.isConverted== true && l.TaskCreation__c == true)
           leadMap.put(l.ConvertedContactId, l.Status);


    List<Contact> conList = [Select Id FROM Contact WHERE Contact.Id IN:leadMap.keySet()];

    for(Contact c: conList){
        if(!conList.isEmpty() && conList.size()>0)
            Task ta = new Task(); 
            ta.Status ='In Progress'; 
            ta.WhoId= c.Id;
            ta.ActivityDate = system.today();


    if(!taskList.isEmpty() && taskList.size()>0)
        insert taskList;

It is not totally done but it works, at least it will change the task status for the Contact that is converted from the Land from where the Task is created. Please, give it a try, test if it works, and let me know how it goes.

| improve this answer | |
  • but that's for the creation of task, so we need NEW at that time, need to change it as Qualified when converted – WantToBeACoder May 3 '17 at 12:02
  • How do you convert your Leads? Normally you should be able to change that value on the lead converting page. – Carlos Naranjo May 3 '17 at 12:30
  • converting lead way is normal by clicking on convert button on lead, yes you are right but after converting lead total two task created one is from conversation page and another is lead's task – WantToBeACoder May 3 '17 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.