0

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);      

}
}
  • 1
    Please include the line numbers where these exceptions are being thrown. Additionally, please be aware that using seeAllData = true is considered extremely bad practice in unit testing. You should be synthesizing all test data. – David Reed Mar 8 '18 at 19:28
  • 2
    @ad_rey --> Test Apex Triggers – glls Mar 8 '18 at 19:31
  • It's line 2. Yeah I read that as well. I'm willing to scrap the whole test class I just don't really know where to start. – ad_rey Mar 8 '18 at 19:42
  • 1
    Just a style thing, but not using brackets, even for a single-line if statement, caused Apple some serious headaches. Best practice is to put in the brackets. Makes your code easier to read anyway, and easier to add to later. – DavidSchach Mar 8 '18 at 20:09
4

You should be receiving one or the other of these errors, but not both at the same time, and I am guessing that it goes back and forth depending on whether or not you perform a query in your test class, right?

You need to eliminate seeAllData = true from your test class. Instead, your class should create and insert all records that are required (here, a BusinessHours instance) to complete the processing that you are attempting to test. You can do this within your test method, or in an @testSetup method.

You are performing a SOQL query in a loop in your trigger. There is no reason to do this, since your query does not rely on the value of wc, so this isn't even a bulkification issue: just move the query outside the loop.

Consider including logic to handle missing objects. When you do var = [SELECT ...];, as opposed to List<Type> x = [SELECT ...];, and there are no matching records, you will get a DML exception.

sc.After_Hours__c=true;
update sc;
System.assert(sc.After_Hours__c,true);      

This is an a == a assertion. The value of sc is not changed (other than to populate the Id field in the case of an insert) by performing DML upon it. To check the results of your processing, you must re-query the object and all needed fields prior to writing assertions. Since you don't have an update trigger at all, you have no reason to be making this assertion.

You should, instead, be querying the object and making an assertion about the behavior of your insert trigger to show that it is working as designed.

You should also consider including a negative case - inserting an object that shouldn't be changed and validating that it is not affected. Best practice triggers also check a bulk case - inserting multiple records in one DML operation and validating the functionality across the whole list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.