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
LisaSturzLisaSturz 

Code question

I am getting the following error when executing my test code...does anyone know how to fix?:

System.AssertException: Assertion Failed: Expected: 1, Actual: null
Class.TestActivityCount.testCountTask: line 18, column 1

 test code:

@istest
private class TestActivityCount{   
/*
    * Test method for this class and TaskUpdateContact and EventUpdateContact
    */
    public static testMethod void testCountTask() {
        //Setup

        Contact con = new Contact(lastname='Test Con');
        insert con;

        //Insert our first task
        Task t = new Task(subject='Test Activity', whoId = con.id, activity_Type__c='Visit', macro_service_line__c='ASC');
        insert t;

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

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

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

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

        //Relink the task to the contact
        //didRun = false; //Reset
        t.whatId = con.id;
        update t;

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

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

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

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

    }

}

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
You should be using "WhoId", not "WhatId".

All Answers

willardwillard
Looks like the activity_count__c field is not being populated.
Does it work when doing it through the UI?
Have you tried doing i through the UI with the exact parameters you do in your test class?
Test classes do not see your org's data. Is there something in your trigger that depends on some data you are not creating in this test class?
sfdcfoxsfdcfox
The test code appears correct. Perhaps you should be more interested in asking why your trigger isn't working.
LisaSturzLisaSturz

My Trigger may be the problem (sorry...new to this):

 

trigger TaskUpdateAccount on Task (after delete, after insert, after undelete, after update) {

    Set<ID> conIds = new Set<ID>();
    //We only care about tasks linked to contacts.
    String prefix =  ContactActivityCount.conPrefix;

    //Add any contact ids coming from the new data
    if (Trigger.new != null) {
        for (Task t : Trigger.new) {
            if (t.WhatId != null && String.valueOf(t.whatId).startsWith(prefix) ) {
                conIds.add(t.whatId);
            }
        }
    }

    //Also add any contact ids coming from the old data (deletes, moving an activity from one contact to another)
    if (Trigger.old != null) {
        for (Task t : Trigger.old) {
            if (t.WhatId != null && String.valueOf(t.whatId).startsWith(prefix) ) {
                conIds.add(t.whatId);
            }
        }
    }

    if (conIds.size() > 0)
        contactActivityCount.updatecontactCounts(conIds);

}

sfdcfoxsfdcfox
You should be using "WhoId", not "WhatId".
This was selected as the best answer
LisaSturzLisaSturz

Thank you! After that update, the following error message appeared atwhen saving the trigger: Error: Compile Error: Illegal variable declaration: conIds.add at line 11 column 18

LisaSturzLisaSturz

I must have had something wrong, because I was able to save now without errors!! Thank you for your help. One last question: Now my test class is saying: Compile Error: Variable does not exist: didRun at line 21 column 9

LisaSturzLisaSturz

Updated test class:

@istest
private class TestActivityCount{   
/*
    * Test method for this class and TaskUpdateContact and EventUpdateContact
    */
    public static testMethod void testCountTask() {
        //Setup

        Contact con = new Contact(lastname='Test Con');
        insert con;

        //Insert our first task
        Task t = new Task(subject='Test Activity', whoId = con.id, activity_Type__c='Visit', macro_service_line__c='ASC');
        insert t;

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

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

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

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

        //Relink the task to the contact
        didRun = false; //Reset
        t.whatId = con.id;
        update t;

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

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

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

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

    }

}

LisaSturzLisaSturz

All is working perfectly now! Thank you so much for your help!