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
SpothSpoth 

Trigger to Show the list of the Activities in all the Lead Records.

Hello,

 

Could you help me out in generating a trigger.  I want to see the Count of all the completed activities in the lead record. It should be in Apex and a Field. So that i could also use it in the Reports. 

Best Answer chosen by Admin (Salesforce Developers) 
LuckkyLuckky

Hi Spoth 

 

Here is a class .You can Schedule that class To Run  Every night(Day) .

 

public class Activitycount{

 

public void ActMethod(){

 

list<lead> lstLead;

 

list<lead> leadRecs;

 

map<id.integer> mapSample;

 

if(System.now().day()==1){

 

lstLead=new list<Lead>();

 

for(Lead objLead:[select customfield__c from Lead ]){

 

objlead.customfield__c =0;

 

lstLead.add(objLead);

 

}//for

 

update lstLead;

 

}//if

 

else{

 

mapSample =map<id.integer>;

 

leadRecs=new list<lead>();

 

for(Lead oLead:[select id,(select id from Tasks where status='completed') from Lead]){

 

mapSample .put(oLead.id,olead.tasks.size());

 

leadRecs.add(oLead);

 

}//for

 

for(Lead obj: leadRecs);

 

obj.customfield__c =mapSample.get(obj.id);

 

update leadRecs;

}//else

 

}//method

 

}//class

 

thanks

 

All Answers

LuckkyLuckky

Hi Spoth,

 

First  you have to create custom field on Lead for count of Completed Activities.

Create a Trigger on Task .it fires only when it 's whoid is lead id.

Now you  can query (Count) all the Tasks which has status completed.

Now based on whoid of task ,get the Lead Record and You can Update Custom filed with that count value.

 

I hope  this what you asked?

 

Thanks

 

SpothSpoth

Hello Luckky!!

 

Thank you for your concern in replying. There is one more thing i wanted to add to the scenario.

 

Every month this Custom field should update to Zero! That means, I only need the List of the completed Activities by month.

 

Eg : If there are 25 completed activities in July (From 1st to 31st) then on Aug 1st the count should go back to Zero & start counting till Aug 31st.

 

One more thing i dint understand is, How will you get to know that the Who id is the lead id?? Because i want this same scenario on the Account and on the Contact object as well.

Could you please elaborate the approach or send any test code/sample code.

 

Thanks a lot.

Raj.

LuckkyLuckky

Hi Spoth 

 

Here is a class .You can Schedule that class To Run  Every night(Day) .

 

public class Activitycount{

 

public void ActMethod(){

 

list<lead> lstLead;

 

list<lead> leadRecs;

 

map<id.integer> mapSample;

 

if(System.now().day()==1){

 

lstLead=new list<Lead>();

 

for(Lead objLead:[select customfield__c from Lead ]){

 

objlead.customfield__c =0;

 

lstLead.add(objLead);

 

}//for

 

update lstLead;

 

}//if

 

else{

 

mapSample =map<id.integer>;

 

leadRecs=new list<lead>();

 

for(Lead oLead:[select id,(select id from Tasks where status='completed') from Lead]){

 

mapSample .put(oLead.id,olead.tasks.size());

 

leadRecs.add(oLead);

 

}//for

 

for(Lead obj: leadRecs);

 

obj.customfield__c =mapSample.get(obj.id);

 

update leadRecs;

}//else

 

}//method

 

}//class

 

thanks

 

This was selected as the best answer