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
fiona gentryfiona gentry 

Apex class shows only 48% whereas wrote tests for all lines

Hi Folks,

Apex class shows only 48% whereas wrote tests for all lines,any idea why shows only 48%

apex code
 
public class ETRR_TaskCreationValidation {
    public static void isairtel account contactResearchcompleted(List<Task> ETRRTasks,Set<Id> caseIdset)
    {
        map<id, Task> mapTaskDetails ;
        map<Id, Case> mapCaseWithTaskList = new  map<Id, Case>([select id, (Select id, Action__c from Tasks )  from Case where Id in: caseIdset]); 
        for(Task t : ETRRTasks)
        {
            Boolean validationFlag = true;
            
            for(Task t1:  mapCaseWithTaskList.get(t.WhatId).tasks)
            {
                if(t1.Action__c == 'airtel account contact Research')
                {
                    validationFlag = false;
                }
            }
            
            if(validationFlag)
            {
                t.addError('Please Create a task for airtel account contact Research before creating any other Task');
            }
            
        }
    }
    public static void is1stairtel contactcompleted(List<Task> ETRRTasks,Set<Id> caseIdset)
    {
        map<id, Task> mapTaskDetails ;
        map<Id, Case> mapCaseWithTaskList = new  map<Id, Case>([select id, (Select id, Action__c from Tasks )  from Case where Id in: caseIdset]); 
        for(Task t : ETRRTasks)
        {
            Boolean validationFlag = true;
            if(mapCaseWithTaskList.get(t.WhatId).tasks.size()>1){
                for(Task t1:  mapCaseWithTaskList.get(t.WhatId).tasks)
                {
                    
                    if( t1.Action__c == '1st airtel contact')
                    {
                        validationFlag = false;
                    }
                    
                }
                
            }
            if(validationFlag)
            {
                t.addError('Please Create a task for 1st airtel contact before creating any other Task');
            }
            
        }
    }
}

Here is test class
 
@isTest
public class ETRR_TaskCreationValidation_Test {
    
    @isTest
    private static void testETRRValidation()
    {
        Id caseRecordTypeId = [select id from Recordtype where DeveloperName  = 'Executive_Response' and sobjecttype = 'Case' ].Id ;
        Id taskrecordTypeId_ETRR = [select id, DeveloperName from RecordType where sobjecttype = 'Task' AND developername = 'ETRR_Task_Record_Type' ].Id; 
        
        Case c = new case(recordtypeId = caseRecordTypeId);
        insETRR c;
        
        try
        {
            task t = new task(recordtypeId = taskrecordTypeId_ETRR, WhatId = c.Id );
            insert t;
            task t2 = new task(recordtypeId = taskrecordTypeId_ETRR, WhatId = c.Id );
            insert t2;
        }
        Catch(System.DmlException dmlex)
        {
            system.assETRREquals('FIELD_CUSTOM_VALIDATION_EXCEPTION', dmlex.getDmlStatusCode(0));     
            task t = new task(recordtypeId = taskrecordTypeId_ETRR, WhatId = c.Id, Action__c = 'airtel account airtel contact Research' );
            insert t; 
            task t1 = new task(recordtypeId = taskrecordTypeId_ETRR, WhatId = c.Id );
            insert t1; 
             task t2 = new task(recordtypeId = taskrecordTypeId_ETRR, WhatId = c.Id, Action__c = '1st airtel contact' );
            insert t2; 
            task t3 = new task(recordtypeId = taskrecordTypeId_ETRR, WhatId = c.Id );
            insert t3; 
            
        }
        
        
    }
}

Your response is appreciated

Regards
Fiona
Best Answer chosen by fiona gentry
Antoninus AugustusAntoninus Augustus
Hi Fiona,

I’m assuming you’re using a trigger class with the before insert context to call the ETRR_TaskCreationValidation class. I’m also assuming you’re performing an extra SOQL query within that trigger class to get the related Case ids and storing them in the caseIdset argument.
If I understand correctly the goal here is to prevent a Task from being created if the parent Case doesn’t already have a Task with the Action__c field being ‘airtel account contact Research’.

If so, there’s a much cleaner and better way of accomplishing this but first lets go over your code. For one, you’re capturing an exception in the test class that’s not being thrown by the ETRR_TaskCreationValidation class. This is perfectly fine if you’re throwing the exception in the calling trigger class, otherwise addError wouldn’t be throwing any exceptions.

You also have a few typos such as ‘insETRR c’ and ‘system. assETRREquals’. You’re also testing your code in a one-by-one fashion, I recommend bulkifying your code. ‘mapTaskDetails’ is also not being used anywhere.

Here’s my approach to your problem, you’d preferably put this logic in the trigger handler class:
Map<Id, Task> mapTaskWithRelatedTask = new Map<Id, Task>();
Set<Id> setWhatIds = new Set<Id>();

for (Task objTask : Trigger.New) {
    mapTaskWithRelatedTask.put(objTask.whatId, objTask);
}
for (AggregateResult ar : [SELECT whatId FROM Task WHERE whatId IN: mapTaskWithRelatedTask.keySet() AND Action__c = 'airtel account contact Research' GROUP BY whatId]) {
    setWhatIds.add(ar.get('whatId'));
}
for (Task objTask : mapTaskWithRelatedTask.values()) {
    if (!setWhatIds.contains(objTask.whatId)) {
        objTask.addError('Please Create a task for airtel account contact Research before creating any other Task');
    }
}

I believe this accomplishes the same without the extra SOQL call and embedded for loop. As for your 48% coverage problem, it might be coming from the lack of exception being thrown. I tested my coverage with this new approach, simply inserting a Task gives you approximately 80% coverage. Hope this answers your question.

All Answers

Antoninus AugustusAntoninus Augustus
Hi Fiona,

I’m assuming you’re using a trigger class with the before insert context to call the ETRR_TaskCreationValidation class. I’m also assuming you’re performing an extra SOQL query within that trigger class to get the related Case ids and storing them in the caseIdset argument.
If I understand correctly the goal here is to prevent a Task from being created if the parent Case doesn’t already have a Task with the Action__c field being ‘airtel account contact Research’.

If so, there’s a much cleaner and better way of accomplishing this but first lets go over your code. For one, you’re capturing an exception in the test class that’s not being thrown by the ETRR_TaskCreationValidation class. This is perfectly fine if you’re throwing the exception in the calling trigger class, otherwise addError wouldn’t be throwing any exceptions.

You also have a few typos such as ‘insETRR c’ and ‘system. assETRREquals’. You’re also testing your code in a one-by-one fashion, I recommend bulkifying your code. ‘mapTaskDetails’ is also not being used anywhere.

Here’s my approach to your problem, you’d preferably put this logic in the trigger handler class:
Map<Id, Task> mapTaskWithRelatedTask = new Map<Id, Task>();
Set<Id> setWhatIds = new Set<Id>();

for (Task objTask : Trigger.New) {
    mapTaskWithRelatedTask.put(objTask.whatId, objTask);
}
for (AggregateResult ar : [SELECT whatId FROM Task WHERE whatId IN: mapTaskWithRelatedTask.keySet() AND Action__c = 'airtel account contact Research' GROUP BY whatId]) {
    setWhatIds.add(ar.get('whatId'));
}
for (Task objTask : mapTaskWithRelatedTask.values()) {
    if (!setWhatIds.contains(objTask.whatId)) {
        objTask.addError('Please Create a task for airtel account contact Research before creating any other Task');
    }
}

I believe this accomplishes the same without the extra SOQL call and embedded for loop. As for your 48% coverage problem, it might be coming from the lack of exception being thrown. I tested my coverage with this new approach, simply inserting a Task gives you approximately 80% coverage. Hope this answers your question.
This was selected as the best answer
Antoninus AugustusAntoninus Augustus
Hi quick correction, addError would actually throw the DmlException, my mistake!
fiona gentryfiona gentry
No its not a trigger ,its actually an apex class to create tasks on Case record
fiona gentryfiona gentry
No its not a trigger ,its actually an apex class to create tasks on Case record and the goal here is to prevent a Task from being created if the parent Case record doesn’t already have a Task with the Action__c field being ‘airtel account contact Research’ and once user creates a Task with Action__c as "‘airtel account contact Research’" then they should be able to create only Task with Action__c as '1st airtel contact'',after these 2 tasks creation they can create any task with any picklist value in Action__c field