+ Start a Discussion
ApolloSApolloS 

Need help! Apex code to count Activities for Lead record

Please forgive me for absolute newbishness. Only have done couple of Visualforce pages for e-mail templates and some validation rules. Now I need to get this trigger done.

 

I need to have a custom field in Leads that has a count of Activities in Activity History. Usually I would do that with roll-up field, but it is not available in leads.

 

I found a trigger in these forums:

trigger Activity_count on Task (after insert, after update) {
List<Lead> uplist=new List<Lead>();
List<Lead> lelist=[select id,Activity_count__c from Lead];
List<AggregateResult> st=new List<AggregateResult>();
st=[select count(Id),whoid from task where id=:Trigger.new and status='Completed' group by whoid];
for(Integer i=0;i<st.size();i++)
{
for(Lead l:lelist)
{
if(l.id==st[i].get('whoid'))
{
l.Activity_count__c=(Integer)st[i].get('expr0')+l.Activity_count__c;
uplist.add(l);
}
}
}
update uplist;
}

 But problem is that I get following error:

Average test coverage across all Apex Classes and Triggers is 0%, at least 75% test coverage is required.

 

Could you help me out?

Best Answer chosen by Admin (Salesforce Developers) 
ElcalvoMikeElcalvoMike

You have to write a test class to test your code before you can deploy it.

 

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

All Answers

ElcalvoMikeElcalvoMike

You have to write a test class to test your code before you can deploy it.

 

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

This was selected as the best answer
ApolloSApolloS

Shoot... ok this basicly puts a nail in the coffin. I don't know how to do it and I have no money to spend on this problem.