Below is my trigger and the test class that goes with it, but I keep getting errors.
EXCEPTION_THROWN [10]|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, scUpdate: execution of BeforeInsert
and
EXCEPTION_THROWN caused by: System.QueryException: List has no rows for assignment to SObject
trigger AfterHoursCheckTrigger on Service_Call__c (before insert) {
For(Service_Call__c wc:trigger.new){
Datetime CreatedTime = System.Now();
BusinessHours bh = [Select
SundayStartTime, MondayStartTime, TuesdayStartTime,
WednesdayStartTime, ThursdayStartTime, FridayStartTime,
SaturdayStartTime, SundayEndTime, MondayEndTime,TuesdayEndTime,
WednesdayEndTime, ThursdayEndTime, FridayEndTime,SaturdayEndTime
From BusinessHours
Where IsDefault=true];
Boolean isWithin = BusinessHours.isWithin(bh.id, CreatedTime);
system.debug('#######'+isWithin);
if (!isWithin)
wc.After_Hours__c = True;
}
}
Test
@isTest (SeeAllData = true)
public class BusinesshrsTestclass {
public static testMethod void BusinesshrsTestmethod()
{
List<BusinessHours> bhs=[select id from BusinessHours where
IsDefault=true];
System.assert(bhs.size()==1);
Service_Call__c sc =new Service_Call__c();
sc.After_Hours__c=False;
//add other required field if needed
insert sc;
sc.After_Hours__c=true;
update sc;
System.assert(sc.After_Hours__c,true);
}
}
seeAllData = true
is considered extremely bad practice in unit testing. You should be synthesizing all test data.