Skip to main content
Tweeted twitter.com/StackSalesforce/status/1280381051949334532
added 34 characters in body
Source Link
Chris Duncombe
  • 24.3k
  • 13
  • 77
  • 116

I have written a class to calculate business hours for Case age in business hours. The class works great my issue is when trying to write decent test code for the class.

In following testing best practices, I want to create all my own test data and not rely on existing data (seeAllDate=true). I would assume that in order to test my different scenarios I would need to create and insert new business hours. Something Normally, I would use something like the snippet below

BusinessHours newHours = new BusinessHours(
    MondayStartTime = Time.newInstance(9, 0, 0, 0),
    MondayEndTime = Time.newInstance(17, 0, 0, 0),
    TuesdayStartTime = Time.newInstance(9, 0, 0, 0),
    TuesdayEndTime = Time.newInstance(17, 0, 0, 0),
    WednesdayStartTime = Time.newInstance(9, 0, 0, 0),
    WednesdayEndTime = Time.newInstance(17, 0, 0, 0),
    ThursdayStartTime = Time.newInstance(9, 0, 0, 0),
    ThursdayEndTime = Time.newInstance(17, 0, 0, 0),
    FridayStartTime = Time.newInstance(9, 0, 0, 0),
    FridayEndTime = Time.newInstance(17, 0, 0, 0),
    IsActive = true,
    IsDefault = true
);
insert newHours;

However, you cannot perform DML on business hours. Is there a way to do this without having to use seeAllData=true?

I have searched and searched but can;t find much info on using the business hours class in test code. Any help is much appreciated.

I have written a class to calculate business hours for Case age in business hours. The class works great my issue is when trying to write decent test code for the class.

In following testing best practices, I want to create all my own test data and not rely on existing data (seeAllDate=true). I would assume that in order to test my different scenarios I would need to create and insert new business hours. Something like below

BusinessHours newHours = new BusinessHours(
    MondayStartTime = Time.newInstance(9, 0, 0, 0),
    MondayEndTime = Time.newInstance(17, 0, 0, 0),
    TuesdayStartTime = Time.newInstance(9, 0, 0, 0),
    TuesdayEndTime = Time.newInstance(17, 0, 0, 0),
    WednesdayStartTime = Time.newInstance(9, 0, 0, 0),
    WednesdayEndTime = Time.newInstance(17, 0, 0, 0),
    ThursdayStartTime = Time.newInstance(9, 0, 0, 0),
    ThursdayEndTime = Time.newInstance(17, 0, 0, 0),
    FridayStartTime = Time.newInstance(9, 0, 0, 0),
    FridayEndTime = Time.newInstance(17, 0, 0, 0),
    IsActive = true,
    IsDefault = true
);
insert newHours;

However, you cannot perform DML on business hours. Is there a way to do this without having to use seeAllData=true?

I have searched and searched but can;t find much info on using the business hours class in test code. Any help is much appreciated.

I have written a class to calculate business hours for Case age in business hours. The class works great my issue is when trying to write decent test code for the class.

In following testing best practices, I want to create all my own test data and not rely on existing data (seeAllDate=true). I would assume that in order to test my different scenarios I would need to create and insert new business hours. Normally, I would use something like the snippet below

BusinessHours newHours = new BusinessHours(
    MondayStartTime = Time.newInstance(9, 0, 0, 0),
    MondayEndTime = Time.newInstance(17, 0, 0, 0),
    TuesdayStartTime = Time.newInstance(9, 0, 0, 0),
    TuesdayEndTime = Time.newInstance(17, 0, 0, 0),
    WednesdayStartTime = Time.newInstance(9, 0, 0, 0),
    WednesdayEndTime = Time.newInstance(17, 0, 0, 0),
    ThursdayStartTime = Time.newInstance(9, 0, 0, 0),
    ThursdayEndTime = Time.newInstance(17, 0, 0, 0),
    FridayStartTime = Time.newInstance(9, 0, 0, 0),
    FridayEndTime = Time.newInstance(17, 0, 0, 0),
    IsActive = true,
    IsDefault = true
);
insert newHours;

However, you cannot perform DML on business hours. Is there a way to do this without having to use seeAllData=true?

I have searched and searched but can;t find much info on using the business hours class in test code. Any help is much appreciated.

deleted 1 character in body
Source Link
Chris Duncombe
  • 24.3k
  • 13
  • 77
  • 116

I have written a class to calculate business hours for Case agewage in business hours. The class works great my issue is when trying to wirtewrite decent test code for the class.

In following testing best practices, I want to create all my own test data and not rely on existing data (seeAllDate=true). I would assume that in order to test my different scenarios I would need to create and insert new business hours. Something like below

BusinessHours newHours = new BusinessHours(
    MondayStartTime = Time.newInstance(9, 0, 0, 0),
    MondayEndTime = Time.newInstance(17, 0, 0, 0),
    TuesdayStartTime = Time.newInstance(9, 0, 0, 0),
    TuesdayEndTime = Time.newInstance(17, 0, 0, 0),
    WednesdayStartTime = Time.newInstance(9, 0, 0, 0),
    WednesdayEndTime = Time.newInstance(17, 0, 0, 0),
    ThursdayStartTime = Time.newInstance(9, 0, 0, 0),
    ThursdayEndTime = Time.newInstance(17, 0, 0, 0),
    FridayStartTime = Time.newInstance(9, 0, 0, 0),
    FridayEndTime = Time.newInstance(17, 0, 0, 0),
    IsActive = true,
    IsDefault = true
);
insert newHours;

However, you cannot perform DML on business hours. Is there a way to do this without having to use seeAllData=true?

I have searched and searched but can;t find much info on using the business hours class in test code. Any help is much appreciated.

I have written a class to calculate business hours for Case agew in business hours. The class works great my issue is when trying to wirte decent test code for the class.

In following testing best practices, I want to create all my own test data and not rely on existing data (seeAllDate=true). I would assume that in order to test my different scenarios I would need to create and insert new business hours. Something like below

BusinessHours newHours = new BusinessHours(
    MondayStartTime = Time.newInstance(9, 0, 0, 0),
    MondayEndTime = Time.newInstance(17, 0, 0, 0),
    TuesdayStartTime = Time.newInstance(9, 0, 0, 0),
    TuesdayEndTime = Time.newInstance(17, 0, 0, 0),
    WednesdayStartTime = Time.newInstance(9, 0, 0, 0),
    WednesdayEndTime = Time.newInstance(17, 0, 0, 0),
    ThursdayStartTime = Time.newInstance(9, 0, 0, 0),
    ThursdayEndTime = Time.newInstance(17, 0, 0, 0),
    FridayStartTime = Time.newInstance(9, 0, 0, 0),
    FridayEndTime = Time.newInstance(17, 0, 0, 0),
    IsActive = true,
    IsDefault = true
);
insert newHours;

However, you cannot perform DML on business hours. Is there a way to do this without having to use seeAllData=true?

I have searched and searched but can;t find much info on using the business hours class in test code. Any help is much appreciated.

I have written a class to calculate business hours for Case age in business hours. The class works great my issue is when trying to write decent test code for the class.

In following testing best practices, I want to create all my own test data and not rely on existing data (seeAllDate=true). I would assume that in order to test my different scenarios I would need to create and insert new business hours. Something like below

BusinessHours newHours = new BusinessHours(
    MondayStartTime = Time.newInstance(9, 0, 0, 0),
    MondayEndTime = Time.newInstance(17, 0, 0, 0),
    TuesdayStartTime = Time.newInstance(9, 0, 0, 0),
    TuesdayEndTime = Time.newInstance(17, 0, 0, 0),
    WednesdayStartTime = Time.newInstance(9, 0, 0, 0),
    WednesdayEndTime = Time.newInstance(17, 0, 0, 0),
    ThursdayStartTime = Time.newInstance(9, 0, 0, 0),
    ThursdayEndTime = Time.newInstance(17, 0, 0, 0),
    FridayStartTime = Time.newInstance(9, 0, 0, 0),
    FridayEndTime = Time.newInstance(17, 0, 0, 0),
    IsActive = true,
    IsDefault = true
);
insert newHours;

However, you cannot perform DML on business hours. Is there a way to do this without having to use seeAllData=true?

I have searched and searched but can;t find much info on using the business hours class in test code. Any help is much appreciated.

Source Link
Chris Duncombe
  • 24.3k
  • 13
  • 77
  • 116

Set Business Hours in Apex Test Code

I have written a class to calculate business hours for Case agew in business hours. The class works great my issue is when trying to wirte decent test code for the class.

In following testing best practices, I want to create all my own test data and not rely on existing data (seeAllDate=true). I would assume that in order to test my different scenarios I would need to create and insert new business hours. Something like below

BusinessHours newHours = new BusinessHours(
    MondayStartTime = Time.newInstance(9, 0, 0, 0),
    MondayEndTime = Time.newInstance(17, 0, 0, 0),
    TuesdayStartTime = Time.newInstance(9, 0, 0, 0),
    TuesdayEndTime = Time.newInstance(17, 0, 0, 0),
    WednesdayStartTime = Time.newInstance(9, 0, 0, 0),
    WednesdayEndTime = Time.newInstance(17, 0, 0, 0),
    ThursdayStartTime = Time.newInstance(9, 0, 0, 0),
    ThursdayEndTime = Time.newInstance(17, 0, 0, 0),
    FridayStartTime = Time.newInstance(9, 0, 0, 0),
    FridayEndTime = Time.newInstance(17, 0, 0, 0),
    IsActive = true,
    IsDefault = true
);
insert newHours;

However, you cannot perform DML on business hours. Is there a way to do this without having to use seeAllData=true?

I have searched and searched but can;t find much info on using the business hours class in test code. Any help is much appreciated.