+ Start a Discussion
atidevatidev 

How to show activity details in Lead

Hi,

  I have to create custom fields to show the Last activity date and Number of activities for a lead in the lead detail section.

How can I do that? Is it possible to show this information in Lead?

Thanks and looking forward for an answer.

Alex_ConfigeroAlex_Configero

Last Activity Date can be done through a formula filed, you will see a field for last activity date to use in the formula (Insert Field -> Lead -> Last Activity Date) that is not available to add directly on the page layout.

 

For the activity count you will need a trigger and use a query similar to this one to update the activity count field

 

select count(Id) from Task where whoid=:LEADID

 

 

kiranmutturukiranmutturu

just try with this code to get the count of tasks on the respective lead..

 

 

 

trigger leadtaskcount on Task (after insert,after delete) {
    
        list<id> lstids = new list<id>();
        if(trigger.isInsert){
            for(task objtask : trigger.new){
                if(string.valueOf(objtask.whoid).contains('00Q'))
                    lstids.add(objtask.whoid);
            }
        }
        if(trigger.isDelete){
            for(task objtask : trigger.old){
                if(string.valueOf(objtask.whoid).contains('00Q'))
                    lstids.add(objtask.whoid);  
            }
        }
        list<lead> lstleads = new list<lead>();
        list<lead> lstupdatelead = new list<lead>();
        if(lstids.size() > 0)
            lstleads = [select id,(select id from tasks) from lead where id in :lstids];        
        if(lstleads.size() > 0){
            for(lead obj : lstleads){
                integer i = obj.tasks.size();
                if(i > 0)
                    obj.Total_Activites__c = i;
                else
                    obj.Total_Activites__c = 0;
                lstupdatelead.add(obj);
                    
            }//eof for loop
            if(lstupdatelead.size() > 0){
                system.debug('lstupdatelead'+lstupdatelead);    
                update lstupdatelead;
            }
        }//eof if size
    
 
}//eof trigger

 

 

 

 

Regards,

 

Kiran

atidevatidev

I didn't see the last activity date in the drop down on formula editor. Could please help me more with this?

Thanks.

Alex_ConfigeroAlex_Configero

Use the advanced formula, and it's called "Last Activity". Make sure the field type is date or date/time

atidevatidev

I don't have the Advanced formulae tab in Lead. What is it that i am missing?

kiranmutturukiranmutturu

I think in all editions this feature is available. but to cross check which edition u are using.

In developer edition the navigation is like this go to lead objects custom field section and create a custom formula field  of type date and click on insert field button there u have to select Last Activity field.

 

 

Regards,

 

Kiran

mms16mms16

Thank you for posting this trigger. Worked perfectly and is very helpful...