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
ethan liethan li 

assertEquals contradicts code coverage

I have a trigger on task which basically checks the Task's call type and update fields on the corresponding account accordingly. (I omitted some parts since the test is only on the insert part)

 

trigger callType on Task (after insert, after update, after delete) {
    Account[] oa; 
    Account a;
    if(Trigger.IsDelete) { ... } 
else if(Trigger.IsUpdate) { ... } else if(Trigger.IsInsert) { for (Task t : Trigger.new) { oa = [SELECT Id, KYC_Completed__c, Check_in_Scheduled__c FROM Account where Id =:t.WhatId LIMIT 1]; //finds the account task is related to if(oa.size()>0){ a = oa[0]; }else return; //oa.size zero, the task is not related to account. abort mission. //if new trigger is check in or kyc call then respective fields should be true. if (t.Call_Type_aD__c == 'KYC Call') a.KYC_Completed__c = true; else if(t.Call_Type_aD__c == 'Check in') a.Check_in_Scheduled__c = true; else return; } } }

This is the (unfinished) test 

 

@isTest
private class callTypetest{
    static testMethod void testcallType() {
        callTypetest ctt = new callTypetest();
        //Make a new account
        Account a= new Account(
            Name = 'Test Account'
        );
        insert a;
        Task t = new Task(Call_Type_aD__c = 'KYC Call', WhatId = a.Id);
        insert t;
        update a;
        a = [SELECT Id, KYC_Completed__c FROM Account WHERE Id =: a.Id LIMIT 1];
        System.assertEquals(true, a.KYC_Completed__c);

    }

}

 Now my test is simple: when I make the task, the insert part of the trigger is activated (code coverage shows this when I check it). It should tick the KYC_Completed__c to true since the Call_Type_aD__c is KYC Call, and in fact that part of the code ran as shown by the code coverage. I've also updated the account as well as querying it again before the assertEquals. However the assertEquals says expect true, got false.

 

Any help is greatly appreciated!

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
Andrew WilkinsonAndrew Wilkinson

Is there a part in your trigger that you update the affected accounts? Also you need to take the SOQL query out of your for loop and use a map to keep track of the accounts.

 

trigger callType on Task (after insert, after update, after delete) {
    Account[] oa; 
    Account a;
//set to hold the whatids of the tasks
    Set<Id> whatIds = new Set<Id>();
//populate the set to use in query
    for(Task t : Trigger.New){
        whatIds.add(t.whatId);
    }
//map to hold the accounts
    Map<Id,Account> taskAccountMap = new Map<Id,Account>();
//query the accounts outside of for loop
    for(Account a : [SELECT Id, KYC_Completed__c, Check_in_Scheduled__c FROM Account where Id IN :whatIds]){
        taskAccountMap.put(a.Id,a);
    }

    if(Trigger.IsDelete) { ... } 
    else if(Trigger.IsUpdate) { ... }
    else if(Trigger.IsInsert) {
        for (Task t : Trigger.new) {
           if(taskAccountMap.containsKey(t.WhatId){
            a = taskAccountMap.get(t.WhatId);
           }
           else
               return;
            //if new trigger is check in or kyc call then respective fields should be true.
            if (t.Call_Type_aD__c == 'KYC Call') 
                a.KYC_Completed__c = true;
            else if(t.Call_Type_aD__c == 'Check in') 
                a.Check_in_Scheduled__c = true;
            else return;
        }
    }
    //then update the accounts from the map
    update taskAccountMap.values();
}

 I bulkified the portion you showed as well as added the update.

All Answers

Andrew WilkinsonAndrew Wilkinson

Is there a part in your trigger that you update the affected accounts? Also you need to take the SOQL query out of your for loop and use a map to keep track of the accounts.

 

trigger callType on Task (after insert, after update, after delete) {
    Account[] oa; 
    Account a;
//set to hold the whatids of the tasks
    Set<Id> whatIds = new Set<Id>();
//populate the set to use in query
    for(Task t : Trigger.New){
        whatIds.add(t.whatId);
    }
//map to hold the accounts
    Map<Id,Account> taskAccountMap = new Map<Id,Account>();
//query the accounts outside of for loop
    for(Account a : [SELECT Id, KYC_Completed__c, Check_in_Scheduled__c FROM Account where Id IN :whatIds]){
        taskAccountMap.put(a.Id,a);
    }

    if(Trigger.IsDelete) { ... } 
    else if(Trigger.IsUpdate) { ... }
    else if(Trigger.IsInsert) {
        for (Task t : Trigger.new) {
           if(taskAccountMap.containsKey(t.WhatId){
            a = taskAccountMap.get(t.WhatId);
           }
           else
               return;
            //if new trigger is check in or kyc call then respective fields should be true.
            if (t.Call_Type_aD__c == 'KYC Call') 
                a.KYC_Completed__c = true;
            else if(t.Call_Type_aD__c == 'Check in') 
                a.Check_in_Scheduled__c = true;
            else return;
        }
    }
    //then update the accounts from the map
    update taskAccountMap.values();
}

 I bulkified the portion you showed as well as added the update.

This was selected as the best answer
ethan liethan li

Thank you so much this is just it. I forgot the update the accounts in the trigger.