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
KevSnellKevSnell 

Trigger Test Help, 1 Line of Code stopping from getting to 100% test coverage

Hi All,

 

I have written the below trigger to create a Task on the Account record when the field (OneSource__OSKeyID__c) is Null.

 

This works great (please be gentle as I'm still learning) and I have created a Test Trigger, however, there is one line of code that is stopping me from getting 100% coverage on this trigger.

 

The one line is this: 

 

    setAccount.add(objTask.WhatId);


If someone could help I would be very grateful.

 

Kev

 

Trigger

trigger AssignOneSourceTask on Account (after insert) 
{
     Set<Id> setAccount = new Set<Id>();
        for(Task objTask: [Select Id, WhatId from Task where WhatId In: trigger.new and Subject = 'OSKey ID Missing on Account'])
        setAccount.add(objTask.WhatId);
        List<Task> taskList = new List<Task>();
        for(integer i=0; i<trigger.new.size(); i++)
        {
            if(!setAccount.contains(trigger.new[i].Id))
                if (trigger.new[i].OneSource__OSKeyID__c == NULL) 
     {
     Task t = new Task(
     Subject = 'OSKey ID Missing on Account',
     WhatId = trigger.new[i].id,
     Description = 'Please update OSKey ID on Account Record',
     Priority = 'Normal',
     Status = 'Not Started',
     IsReminderSet = True,
     ReminderDateTime = System.now()+1,
     ActivityDate = Date.today()+2,
     OwnerId = '005E0000000YhNn');
     
     taskList.add(t);
     setAccount.add(trigger.new[i].Id);
     }
     }
 if(!taskList.isEmpty())
    insert taskList;
}

 

Test Trigger 

@isTest
public with sharing class TriggerTest_Account_OneSourceID {
	public static testmethod void testAccount_NoOneSourceID() {
		System.debug('VistageDebug: entering TriggerTest_Account_OneSourceID');
		//Create Test Account
		Account testAccounts = new Account(Name='ACCOUNT_NAME');
		Insert testAccounts;
		
		List<Task> taskList = [SELECT id, ActivityDate, subject from Task WHERE whatId = :testAccounts.id];
        System.assertEquals(1,taskList.size());     
		System.assertEquals('OSKey ID Missing on Account',taskList[0].Subject);
		
	}
	
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_

 

It is impossible for that line to be hit because it is ONLY an insert trigger.  This means you haven't had the chance yet to add one to the db.  If this is correct then you should simply remove that check.  Otherwise make it an update trigger too.

 

trigger AssignOneSourceTask on Account (after insert, after update) 

 

Than also, at the end of your test method add

 

update testAccounts;

 

All Answers

Damien_Damien_

 

It is impossible for that line to be hit because it is ONLY an insert trigger.  This means you haven't had the chance yet to add one to the db.  If this is correct then you should simply remove that check.  Otherwise make it an update trigger too.

 

trigger AssignOneSourceTask on Account (after insert, after update) 

 

Than also, at the end of your test method add

 

update testAccounts;

 

This was selected as the best answer
KevSnellKevSnell

Thank you - it makes sense now looking at it - I removed the check as I only wanted it to work on "after insert"

 

Now back to figuring out some other test methods, which hopefully I can do on my own.


Kev