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
Keaton KleinKeaton Klein 

Trying to increase my test coverage again

I have this trigger with the following test and i am only getting 31% coverage.  The concept of test still has me confused, any help would be greatly appreciated.
trigger AcceptanceSigned on Task (after insert, after update) {
    Set<Id> LogIds = new Set<Id>();
    Map<Id,Account> mapacctLogWithId = new Map<Id,Account>();
    Map<Id,Account> mapAccLogToUpdate = new Map<Id,Account>();
    
    for(Task t: trigger.new){
        if(t.WhatId != null && string.valueof(t.WhatId).startsWith('a01')){
            LogIds.add(t.WhatId);
        }
    }
    for(Account a:[SELECT Description, easy_workflow__c FROM Account WHERE ID IN:LogIds]){
        mapacctLogWithId.put(a.Id,a);
    }
    
    for(Task t: trigger.new){
        if(t.WhatId != null && string.valueof(t.WhatId).startsWith('a01') && t.Status == 'Completed' && mapacctLogWithId != null && mapacctLogWithId.get(t.WhatId) != null){
            Account accLog = new Account();
            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.easy_workflow__c = accLog.easy_workflow__c + ';'+ t.subject;
            //accLog.easy_workflow__c = accLog.easy_workflow__c.replace(t.subject,'');
            if(t.Subject == 'Sign Acceptance Form'){
                accLog.Description = 'Acceptance Form Signed';    
            }
            mapAccLogToUpdate.put(accLog.Id,accLog);
        }
        
                if(t.WhatId != null && string.valueof(t.WhatId).startsWith('a01') && t.Status == 'Open' && mapacctLogWithId != null && mapacctLogWithId.get(t.WhatId) != null){
            Account accLog = new Account();
            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.easy_workflow__c = accLog.easy_workflow__c + ';'+ t.subject
                    if(accLog.easy_workflow__c != null && accLog.easy_workflow__c !=''){
            accLog.easy_workflow__c = accLog.easy_workflow__c.replace(t.subject,'');
                    }
            if(t.Subject == 'Sign Acceptance Form'){
                accLog.Description = 'Acceptance Form Signed';    
            }
            mapAccLogToUpdate.put(accLog.Id,accLog);
        }
    
    if(mapAccLogToUpdate != null && mapAccLogToUpdate.values() != null &&mapAccLogToUpdate.values().size() > 0){
        update mapAccLogToUpdate.values();
    }
    } }
@isTest(SeeAllData = true)
public class AcceptanceSignedTest {
         public static testMethod void testAcceptanceSigned(){
             test.startTest();
             Account testLog = new Account(name='Test', description = 'Test');
             insert testLog;
             
             Task testTask = new Task(Subject = 'Sign Acceptance Form', whatId = testLog.ID, Status = 'Completed');
             insert testTask;
             
             Account testLog2 = new Account(name='Test2', description = 'Test2');
             insert testLog2;
             
             Task testTask2 = new Task(Subject = 'Test', whatId = testLog2.ID, Status = 'Completed');
             insert testTask2;
             
             Account testLoga = new Account(name='Test', description = 'Test');
             insert testLoga;
             
             Task testTaska = new Task(Subject = 'Sign Acceptance Form', whatId = testLog.ID, Status = 'Open');
             insert testTaska;
             
             Account testLog2a = new Account(name='Test2', description = 'Test2');
             insert testLog2a;
             
             Task testTask2a = new Task(Subject = 'Test', whatId = testLog2.ID, Status = 'Open');
             insert testTask2a;
             test.stopTest();
        }
}


 
Best Answer chosen by Keaton Klein
Sameer Tyagi SFDCSameer Tyagi SFDC
Hi Keaton,

I see that you have written this line many times in your code.
string.valueof(t.WhatId).startsWith('a01')

I would like to inform you that account id always starts with 001
Thats why, your condition does not cover in trigger. 


Regards,
Sameer Tyagi
http://mirketa.com/

All Answers

Sameer Tyagi SFDCSameer Tyagi SFDC
Hi Keaton,

I see that you have written this line many times in your code.
string.valueof(t.WhatId).startsWith('a01')

I would like to inform you that account id always starts with 001
Thats why, your condition does not cover in trigger. 


Regards,
Sameer Tyagi
http://mirketa.com/
This was selected as the best answer
Sameer Tyagi SFDCSameer Tyagi SFDC
You just need to replace all  'a01' with '001'
Like this 
string.valueof(t.WhatId).startsWith('001')

Your coverage will be 93% . :) 


Sameer

 
Keaton KleinKeaton Klein
Ahh, thank you very much, i must haev overlooked that 20 times.