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
Tom FrayTom Fray 

Count of activities on lead conversion

I have a business requirement to show a count of the number of completed tasks that were on a lead before it was converted. The report will be run on the account object, so I have created a field to hold this value, and am writing a trigger to count the number of completed tasks against the lead at the point of conversion, and am planning to update the field on the account with this value.
 

So far I have the following trigger, but on converting a lead, both system debugs are showing 0, and I can't work out why. I'm fine with populating the field on the account once i have the value. For now, I'm just trying to work out why the count of tasks is always 0. Both queries work when i run them in an anon block before and after the conversion respectively

trigger LeadConvert on Lead (after update, before update) {

for(Lead convertingLead:trigger.new){
  if(convertingLead.IsConverted == TRUE){
  
   id thisLeadID = convertingLead.id;
   integer i = [SELECT Count() From Task a WHERE IsClosed = True AND Type = 'Call' AND whoid = :thisLeadID]; 
    system.debug('Number of completed calls against lead: ' + i);
  
   id thisAccountID = convertingLead.ConvertedAccountId;
   integer j = [SELECT Count() From Task a WHERE IsClosed = True AND Type = 'Call' AND accountid = :thisAccountID];
   system.debug('Number of completed calls against account: ' +j);
   
  }
 
}
}

Has anyone got any ideas?

Thanks

Best Answer chosen by Tom Fray
Sonam_SFDCSonam_SFDC
Hi, I used the same code on my test ORG  - the only  changes I made was in the query where I've used the following one to get completed Tasks:

[SELECT Count() From Task a WHERE  Status = 'Completed' AND accountid = :thisAccountID];

The debug logs show me the correct count for the second query - pls try updating the code and let me know if this helps..

All Answers

Sonam_SFDCSonam_SFDC
Hi, I used the same code on my test ORG  - the only  changes I made was in the query where I've used the following one to get completed Tasks:

[SELECT Count() From Task a WHERE  Status = 'Completed' AND accountid = :thisAccountID];

The debug logs show me the correct count for the second query - pls try updating the code and let me know if this helps..
This was selected as the best answer
Tom FrayTom Fray
Thanks,

This seems to have worked. I didn't think that the problem would be there, as it was fine when running the query in an anonymous block.

In case anyone was wondering, the leads are against the account at this stage, rather than the lead.

Tom