You need to sign in to do that
Don't have an account?
Help with Activity Count Trigger
Hi All,
I am not a developer, but due to a business request, I have been working on updating the below code to function with Leads:
http://www.radialweb.com/2010/08/summarizing_salesforce_fields_with_triggers/#comments
Basically, anywhere I saw 'Opportunity', I replaced it with 'Lead'. Anywhere I saw 'opp' or 'opps' I replaced them with 'lead' and 'leads' respectively. And anywhere I saw an 'o' in repsect to referencing a Record as an Opportunity Record, I changed the 'o' to an 'l'.
That being said, when I made the above edits and attempted to save the class, I got the below error:
Error: Compile Error: Test methods must be in test classes at line 40 column 35
Any help?
Thanks,
Elliot
Hi,
In summer13 release we cannot write test methods in classes. We have to create a separate test class and have to write the test method there.
Like this
So just cut the test method from your code and write it in a separate apex class like this.
If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.
Thanks
Hi Souvik,
Thanks for the response. However, now I am having trouble saving the test class. Please see below. I am getting the following error message: Error: Compile Error: Variable does not exist: lead at line 14 column 9. Sorry I haven't quite figured out how to make the code be formatted in a clean way.
@isTest
private class TestClassName{
public static testMethod void testCountTask() {
//Setup
Lead l = new Lead(lastname='Test');
insert l;
//Insert our first task
Task t = new Task(subject='Test Activity', whatId = lead.id);
insert t;
//Verify count
lead = [SELECT ID, activity_count__c FROM Lead WHERE ID = :lead.id];
System.assertEquals(1,lead.activity_count__c);
//Disconnect task from the lead
didRun = false; //Reset
t.whatId = null;
update t;
//Verify count = 0
lead = [SELECT ID, activity_count__c FROM Lead WHERE ID = :lead.id];
System.assertEquals(0,lead.activity_count__c);
didRun = false; //Reset
//Add an event
Event e = new Event(subject='Test Event', whatId = lead.id, startDateTime = System.Now(), endDateTime = System.now());
insert e;
//Verify count = 1
lead = [SELECT ID, activity_count__c FROM Lead WHERE ID = :lead.id];
System.assertEquals(1,lead.activity_count__c);
//Relink the task to the lead
didRun = false; //Reset
t.whatId = lead.id;
update t;
//Verify count = 2
lead = [SELECT ID, activity_count__c FROM Lead WHERE ID = :lead.id];
System.assertEquals(2,lead.activity_count__c);
//Disconnect the event from the lead
didRun = false; //Reset
e.whatId = null;
update e;
//Verify count is back down to 1
lead = [SELECT ID, activity_count__c FROM Lead WHERE ID = :lead.id];
System.assertEquals(1,lead.activity_count__c);
//Delete the task
didRun = false; //reset
delete t;
//Verify count is back down to 0
lead = [SELECT ID, activity_count__c FROM Lead WHERE ID = :lead.id];
System.assertEquals(0,lead.activity_count__c);
}
}
Hi,
Modify the blue colored text
@isTest
private class TestClassName{
public static testMethod void testCountTask() {
//Setup
Lead l = new Lead(lastname='Test');
insert l;
//Insert our first task
Task t = new Task(subject='Test Activity', whatId = lead.id);
insert t;
//Verify count
lead = [SELECT ID, activity_count__c FROM Lead WHERE ID = :lead.id];
System.assertEquals(1,lead.activity_count__c);
//Disconnect task from the lead
didRun = false; //Reset
t.whatId = null;
update t;
//Verify count = 0
Lead lead = new Lead();
lead = [SELECT ID, activity_count__c FROM Lead WHERE ID = :lead.id];
System.assertEquals(0,lead.activity_count__c);
didRun = false; //Reset
//Add an event
Event e = new Event(subject='Test Event', whatId = lead.id, startDateTime = System.Now(), endDateTime = System.now());
insert e;
//Verify count = 1
lead = [SELECT ID, activity_count__c FROM Lead WHERE ID = :lead.id];
System.assertEquals(1,lead.activity_count__c);
//Relink the task to the lead
didRun = false; //Reset
t.whatId = lead.id;
update t;
//Verify count = 2
lead = [SELECT ID, activity_count__c FROM Lead WHERE ID = :lead.id];
System.assertEquals(2,lead.activity_count__c);
//Disconnect the event from the lead
didRun = false; //Reset
e.whatId = null;
update e;
//Verify count is back down to 1
lead = [SELECT ID, activity_count__c FROM Lead WHERE ID = :lead.id];
System.assertEquals(1,lead.activity_count__c);
//Delete the task
didRun = false; //reset
delete t;
//Verify count is back down to 0
lead = [SELECT ID, activity_count__c FROM Lead WHERE ID = :lead.id];
System.assertEquals(0,lead.activity_count__c);
}
}
If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.
Thanks
were you able to make this work for leads? My apex class and triggers do not show any errors but my field will not update