+ Start a Discussion
Keaton KleinKeaton Klein 

Trying to increase my test coverage

I have the trigger & test below which is giving me 38% coverage.  I am very new to APEX and the testing still has me confused.  Any help on creating a test class for this trigger would be greatly appreciated.
 
trigger AcceptanceSigned on Task (after insert, after update) {
    Set<Id> LogIds = new Set<Id>();
for(Task t: [SELECT Subject,WhatId, Status FROM TASK WHERE ID IN:trigger.new]){
String wId = t.WhatId;
if(wId!=null && wId.startsWith('a01') && !LogIds.contains(t.WhatId) && t.Status == 'Completed' && t.Subject == 'Sign Acceptance Form'){
LogIds.add(t.WhatId);
    for(Account_Log__c a:[SELECT Stage__c, status_list__c FROM Account_Log__c WHERE ID IN:LogIds]){
                 a.Stage__c = 'Acceptance Form Signed';
        update a; 
}
}  
    if(wId!=null && wId.startsWith('a01') && !LogIds.contains(t.WhatId) && t.Status == 'Completed'){
LogIds.add(t.WhatId);
    for(Account_Log__c a:[SELECT Stage__c, Status_List__c FROM Account_Log__c WHERE ID IN:LogIds]){
        		 a.Status_List__c = a.Status_List__c + ';'+ t.subject;
        update a; 
}
}  
}  
}
 
@isTest
public class AcceptanceSignedTest {
         private static testMethod void testAcceptanceSigned(){
             test.startTest(); 
             Account_Log__c testLog = new Account_Log__c(Grantor_and_or_Family_Trustee_etc_1__c='Test');
             testLog.Grantor_and_or_Family_Trustee_etc_2__c = 'Test';
             insert(testLog);
             LIST<Account_Log__c> a = [SELECT ID, Grantor_and_or_Family_Trustee_etc_1__c FROM Account_Log__c WHERE Grantor_and_or_Family_Trustee_etc_1__c = 'Test'];
             Task testTask = new Task(Subject = 'Test', whatId = a[0].ID); 
             insert(testTask); 
             Account_Log__c updatedLog = [SELECT Grantor_and_or_Family_Trustee_etc_1__c FROM Account_Log__c WHERE Grantor_and_or_Family_Trustee_etc_1__c = 'Test'];
             system.AssertEquals('Test',updatedLog.Grantor_and_or_Family_Trustee_etc_1__c);
             test.stopTest(); 
        }
}

 
Best Answer chosen by Keaton Klein
MithunPMithunP
Hi Keaton,

Here is the updated test class, you can try this.
 
@isTest(SeeAllData = true)
public class AcceptanceSignedTest {
         public static testMethod void testAcceptanceSigned(){
             test.startTest();
             Account_Log__c testLog = new Account_Log__c(Grantor_and_or_Family_Trustee_etc_1__c='Test', Grantor_and_or_Family_Trustee_etc_2__c = 'Test');
             insert testLog;
             
             Task testTask = new Task(Subject = 'Sign Acceptance Form', whatId = testLog.ID, Status = 'Completed');
             insert testTask;
             
             Account_Log__c testLog2 = new Account_Log__c(Grantor_and_or_Family_Trustee_etc_1__c='Test2', Grantor_and_or_Family_Trustee_etc_2__c = 'Test2');
             insert testLog2;
             
             Task testTask2 = new Task(Subject = 'Test', whatId = testLog2.ID, Status = 'Completed');
             insert testTask2;
             test.stopTest();
        }
}
Best Regards,
Mithun.
 

All Answers

MithunPMithunP
Hi Keaton,

Here is the updated test class, you can try this.
 
@isTest(SeeAllData = true)
public class AcceptanceSignedTest {
         public static testMethod void testAcceptanceSigned(){
             test.startTest();
             Account_Log__c testLog = new Account_Log__c(Grantor_and_or_Family_Trustee_etc_1__c='Test', Grantor_and_or_Family_Trustee_etc_2__c = 'Test');
             insert testLog;
             
             Task testTask = new Task(Subject = 'Sign Acceptance Form', whatId = testLog.ID, Status = 'Completed');
             insert testTask;
             
             Account_Log__c testLog2 = new Account_Log__c(Grantor_and_or_Family_Trustee_etc_1__c='Test2', Grantor_and_or_Family_Trustee_etc_2__c = 'Test2');
             insert testLog2;
             
             Task testTask2 = new Task(Subject = 'Test', whatId = testLog2.ID, Status = 'Completed');
             insert testTask2;
             test.stopTest();
        }
}
Best Regards,
Mithun.
 
This was selected as the best answer
Bhanu MaheshBhanu Mahesh
Hi Keaton,

No need to query the record again. You will get the entire record in Trigger.new
As per best practices, never perform SOQL or DML operations inside a for loop.
Try to use collections instead of repeated SOQL.


trigger AcceptanceSigned on Task (after insert, after update) {
    Set<Id> LogIds = new Set<Id>();
    Map<Id,Account_Log__c> mapacctLogWithId = new Map<Id,Account_Log__c>();
    Map<Id,Account_Log__c> mapAccLogToUpdate = new Map<Id,Account_Log__c>;
    
    for(Task t: trigger.new){
        if(t.WhatId != null && t.WhatId.startsWith('a01') && t.Status == 'Completed'){
            LogIds.add(t.WhatId);
        }
    }
    for(Account_Log__c a:[SELECT Stage__c, status_list__c FROM Account_Log__c WHERE ID IN:LogIds]){
        mapacctLogWithId.put(a.Id,a);
    }
    
    for(Task t: trigger.new){
        if(t.WhatId != null && t.WhatId.startsWith('a01') && t.Status == 'Completed' && mapacctLogWithId != null && mapacctLogWithId.get(t.WhatId) != null){
            Account_Log__c accLog = new Account_Log__c();
            if(mapAccLogToUpdate != null && mapAccLogToUpdate.get(t.WhatId) != null){
                accLog = mapAccLogToUpdate.get(t.WhatId);
            }
            else if(mapAccLogToUpdate != null && mapAccLogToUpdate.get(t.WhatId) == null){
                accLog = mapacctLogWithId.get(t.WhatId);
            }
            accLog.Status_List__c = accLog.Status_List__c + ';'+ t.subject;
            if(t.Subject == 'Sign Acceptance Form'){
                accLog.Stage__c = 'Acceptance Form Signed';    
            }
            mapAccLogToUpdate.put(accLog.Id,accLog);
        }
    
    }
    
    if(mapAccLogToUpdate != null && && mapAccLogToUpdate.values() != null &&mapAccLogToUpdate.values().size() > 0){
        update mapAccLogToUpdate.values();
    }




You can add conditions on update if you want to run this trigger only when whatId or Subject ot Status is changed using trigger context variables. 
Else the trigger will run even if there is a change in other fields.

For test class you can use the code suggested by Mithun.
Keaton KleinKeaton Klein
Thank you Mithun, 100% now :)
Keaton KleinKeaton Klein
Thank you Bhanu as well, i implemented your version of the trigger.