1

I'm trying to create a trigger on Leads that when the Lead Status = 'Open' and a Task is created for that records, as soon as the Task is saved (it doesn't matter if it is completed or still open) it will change the Lead Status to 'Contacted'.

You can't do it with workflows because it'll take it when the task is completed and only if the task has a due date.

This is what I've done so far:

trigger UpdateLeadswithTask on Task (after insert) {
    // set up lists you will need
    List<Lead> LeadsToUpdate = new List<Lead>();
    Map<Id, Task> taskMap = new Map<Id, Task>();

    // go through the list of tasks that were inserted
    for (Task t: Trigger.New)
    {
      // if they are related to a Lead, add the Lead id (whoID) and their values to a map
      if (t.WhatId  != null)
        {
            taskMap.put(t.WhatId, t);
        }
    }
    // if the map isnt empty
    if (taskMap.size() > 0)
    {
        // get all of the Leads related to the tasks
        LeadsToUpdate = [SELECT Id, Status FROM Lead WHERE Id IN: taskMap.keySet()];
        // go through the list for each Lead
        for (Lead l: LeadsToUpdate)
        { if (l.Status =='Working - Contacted')
            // set the new Lead status
            l.Status = 'Open - Not Contacted';
         else
             System.debug('The Status of the Lead was not Working - Contacted');
        }

        // if the list of Leads isnt empty, update them
        if (LeadsToUpdate.size() > 0)
        {
            update LeadsToUpdate;
        }
    }
}

It is not doing anything..I use the debug log and I don't see like it is doing anything (it is active). I also created a Test Class and it says the list is Null.

  • 2
    This is a community where you can find help with a single problem or piece of guidance. Please post what you already have or where you have started, don't just say "I need code to do _____." As a start, you want the trigger to be on Task (since it's firing on that object), not Lead – Brian Mansfield May 15 '15 at 15:47
  • No, I think the trigger should be on Lead, because an update on Lead fires the other actions. However, as @Brian stated, you need to show your work so far, so we can help you get it right. – navD87 May 15 '15 at 16:17
  • 1
    What are you talking about? He wants the trigger to fire when a Task is saved or created.... – Brian Mansfield May 15 '15 at 16:18
  • Sorry, my bad.. for some reason, I understood as a Task should be created by the trigger when the Lead becomes open. You are right @Brian, it should be on the Task – navD87 May 15 '15 at 16:24
  • I just edited my post and now you can see the code I've done. I hope that helps you see what I've done wrong. Thanks again! – Leo May 18 '15 at 15:23
1

It's the "WhoId" not the "WhatId" that links to Leads. Change that in your code and you should be good.

Also it looks like you have your if statement backwards, changing Leads with Status of 'Contacted' to Status of 'Open'. Your question says to change from 'Open' to 'Contacted'

  • It worked!!! you are a genius! The Status was just an example, I need to do this for another Org with different statuses. I just wanted the trigger to work and it is working now. =) Would you mind explaining what's the difference between WhatId vs WhoId ? – Leo May 18 '15 at 16:23
  • @Leo They are both "Polymorphic Fields" which means they can refer to more than one sObject. WhoID refers to people things. So that would be typically a Lead ID or a Contact ID WhatID refers to object type things. That would typically be an Account ID or an Opportunity ID You can check out this link for more info. – dBeltowski May 18 '15 at 16:27
0

Okay, I've looked at your code and am going to comment and correct some things to move you in the right direction

trigger UpdateLeadswithTask on Task (after insert) {
    // set up lists you will need
    List<Lead> LeadsToUpdate = new List<Lead>();
    Map<Id, Task> taskMap = new Map<Id, Task>();

    // go through the list of tasks that were inserted
    for (Task t: Trigger.New)
    {
      // if they are related to a Lead, add the Lead id (whoID) and their values to a map
      if (t.WhatId  != null)
        {
            taskMap.put(t.WhatId, t);

Here, you've used the WhatId instead of the WhoId, also the key to a map of this kind, should be the task Id, not the WhatId or WhoId in case there happened to be more than one task for the same lead.

            taskMap.put(t.Id,t.WhoId);
        }
    }
    // if the map isnt empty
    if (taskMap.size() > 0)
    {
        // get all of the Leads related to the tasks

Some might say that this is where you need to start with your Trigger. You don't know at the beginning which Tasks are related to Leads and which Tasks are related to Opportunities, Accounts or other things.Your map above, was of little value.

One of your options would have been to just collect all of the contact Ids and run your query on leads to see which ones were returned and then build your map. But we can work with the code that you have.

        LeadsToUpdate = [SELECT Id, Status FROM Lead WHERE Id IN: taskMap.keySet()];

Note that your above query used keySet(), we need to change this now to the following:

        LeadsToUpdate = [SELECT Id, Status FROM Lead WHERE Id IN: taskMap.values()];

Now we'll continue with your code below

        // check to see if lead status == working-contacted

        list<Lead>leadsToUp = new list<Lead>();

        for(Lead l:LeadsToUpdate){

           if (l.Status =='Working - Contacted'){

               // set the new Lead status
               l.Status = 'Open - Not Contacted';

               leadsToUp.add(l);

I've commented out the part part of the code section below that isn't needed at all.

         // else
         //    System.debug('The Status of the Lead was not Working - Contacted');

           }

        }

        // if the list of Leads isnt empty, update them
        if (leadsToUp.isEmpty() == false) update LeadsToUpdate;


    }
}
  • Thank you crmprogdev! My previous code is working but I will change it to what you gave me. I will let you know if something doesn't work right! Thanks again! – Leo May 18 '15 at 21:52

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.