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
Elliot2642Elliot2642 

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

 

 

souvik9086souvik9086

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

 

@isTest 
private class TestClassName{
    
public static testMethod void testCountTask() {
        //Setup
        Account a = new Account(name='Test');
        insert a;

        Opportunity opp = new Opportunity(accountId = a.id, name='Test Opp', StageName='New', CloseDate=System.today());
        insert opp;

        //Insert our first task
        Task t = new Task(subject='Test Activity', whatId = opp.id);
        insert t;

        //Verify count
        opp = [SELECT ID, activity_count__c FROM Opportunity WHERE ID = :opp.id];
        System.assertEquals(1,opp.activity_count__c);

        //Disconnect task from the opportunity
        didRun = false; //Reset
        t.whatId = null;
        update t;
        //Verify count = 0
        opp = [SELECT ID, activity_count__c FROM Opportunity WHERE ID = :opp.id];
        System.assertEquals(0,opp.activity_count__c);

        didRun = false; //Reset
        //Add an event
        Event e = new Event(subject='Test Event', whatId = opp.id, startDateTime = System.Now(), endDateTime = System.now());
        insert e;

        //Verify count = 1
        opp = [SELECT ID, activity_count__c FROM Opportunity WHERE ID = :opp.id];
        System.assertEquals(1,opp.activity_count__c);

        //Relink the task to the opportunity
        didRun = false; //Reset
        t.whatId = opp.id;
        update t;

        //Verify count = 2
        opp = [SELECT ID, activity_count__c FROM Opportunity WHERE ID = :opp.id];
        System.assertEquals(2,opp.activity_count__c);

        //Disconnect the event from the opportunity
        didRun = false; //Reset
        e.whatId = null;
        update e;

        //Verify count is back down to 1
        opp = [SELECT ID, activity_count__c FROM Opportunity WHERE ID = :opp.id];
        System.assertEquals(1,opp.activity_count__c);

        //Delete the task
        didRun = false; //reset
        delete t;
        //Verify count is back down to 0
        opp = [SELECT ID, activity_count__c FROM Opportunity WHERE ID = :opp.id];
        System.assertEquals(0,opp.activity_count__c);

    }

}

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

sushant sussushant sus
write test method in different class
Elliot2642Elliot2642

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);

}
}

souvik9086souvik9086

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

Megan BondMegan Bond
Elliot,

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