1

When i am querying lead in Lead conversion trigger..My task query is returning null even there are task records associated with Lead object..I even try to hard-code query still its returning empty list.

trigger LeadonConversion on Lead (after update) {
    List<Lead> leadList=[Select id,(Select Id, WhoId, WhatId From Tasks) from Lead where id in:Trigger.newMap.Keyset()];
    for(Lead leRec:leadList)
    {
        System.debug('=========list========'+leRec.Tasks);
        for(Task tRec:leRec.Tasks)
        {
            if(leadTasksMap.containsKey(leRec.id))
            {
                System.debug('==========inside==========');
                leadTasksMap.get(leRec.id).add(tRec);
            }
            else
            {
                System.debug('==========inside 1==========');
                leadTasksMap.put(leRec.Id,new List<Task>{tRec});
            }
        }
    }

System.debug('=======leadTasksMap======'+leadTasksMap);

My map is null.

I even tried this

List<Task> taskList=[Select id,whoid from task where whoid=:'00Q11000003UPa1'];

This is also returning empty list

Can someone tell me why this happening.Is it some event issue or what..??

3

Your trigger is (correctly) firing after update, so that's good. However, after the update is finished, the Lead is no longer a Lead, so its activities are all set with WhoId = Lead.ConvertedContactId.

The solution is to do a query of the Lead itself, being sure to get ConvertedContactId, ConvertedAccountId, and ConvertedOpportunityId. Then we'll work with those values. But this is where things get fishy:

Scenario 1: Convert Lead and don't create an Opportunity

You would check if ConvertedOpportunityId == null, and then you would find all those activities. But...

Scenario 1.1: Convert Lead, no Opportunity, didn't merge with an existing Contact

Here, we have no problems taking all Tasks where WhoId = :l.ConvertedContactId and setting them all to completed.

Scenario 1.1: Convert Lead, no Opportunity, DID merge with an existing Contact

There is no way of knowing off-the-bat if you did this, unless you do a second query on Contact, searching for CreatedDate, and comparing that to LastModifiedDate on that Contact. If the two do not match (and I don't know if they could be even slightly different, so you may need to check on just date and hour being the same) then you merged the two and you should filter your Task query to only those tasks where the LastModifiedDate is the same as the Contact LastModifiedDate... BUT...

This is not foolproof, and I'm sure you can think of ways that this could fall apart, such as having converted a Lead to merge with a very active Contact record. And for THIS reason, I think that this is a dangerous thing you're attempting unless you can make some proper filters on your Task query. Even then, it sounds dangerous.

Scenario 2: Convert Lead and do create an Opportunity

This one is nice and fun: You created an Opportunity, so you can find it by checking the WhatId (the Opportunity) and the WhoId (the Contact). The Tasks you get here are only the ones that were on the Lead, so you're safe, whether or not you merged the Lead with an existing Contact.

I think those are the only options.

However, marking all open Tasks as closed when converting a lead just sounds fishy from a business process perspective. Part of the point of a CRM is to use one record for a person and to follow that person through the whole process of marketing, sales, support, retention, etc. This practice of closing "all the tasks" goes against that, so it would be good to filter your query very carefully, or you are going to have management and marketing complaining loudly.

Good luck!

1

Per the docs:

All open and closed activities from the lead are attached to the account, contact, and opportunity.

So I am assuming that the activities are no longer associated with a lead after conversion

  • Actually my requirements is I want to change the status of all the tasks to completed..when lead is converted..So what happens to those tasks after conversion..? – miku Aug 10 '14 at 16:53
  • A task is an activity so it is attached to the account, contact, and opportunity – Eric Aug 10 '14 at 17:20

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.