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
Admin User 10568Admin User 10568 

Next Start Date Using Business Hours

Hi, I would like to create a trigger that prints the next business date/time available to a custom field on a custom object. I would like the trigger to run after each new instance of the object type. I'm getting an error "a value cannot be stored to Task_Start_Time__c in type Repro_Lead__c". It's a datetime field and I was wondering how I could get this to work?
 
trigger NextAvailableBusinessMoment on REPRO__Lead__c (after insert) {

    
// Get the default business hours

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

// Starting from the System.now, find the next date when business hours reopens.
//  This returned next day start time will be in the local time zone

Datetime nextStart = BusinessHours.nextStartDate(bh.id,  System.now());

// print nextStart date/time in custom field on trigger object

REPRO__Lead__c.Task_Start_Time__c = nextStart;
    

}

 
Best Answer chosen by Admin User 10568
AnkaiahAnkaiah (Salesforce Developers) 
Hi,

try with below code.
trigger NextAvailableBusinessMoment on REPRO__Lead__c (before insert) {

    
// Get the default business hours

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

// Starting from the System.now, find the next date when business hours reopens.
//  This returned next day start time will be in the local time zone

Datetime nextStart = BusinessHours.nextStartDate(bh.id,  System.now());

// print nextStart date/time in custom field on trigger object

for(REPRO__Lead__c rl:trigger.new){

rl.Task_Start_Time__c = nextStart;
}
    

}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi,

try with below code.
trigger NextAvailableBusinessMoment on REPRO__Lead__c (before insert) {

    
// Get the default business hours

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

// Starting from the System.now, find the next date when business hours reopens.
//  This returned next day start time will be in the local time zone

Datetime nextStart = BusinessHours.nextStartDate(bh.id,  System.now());

// print nextStart date/time in custom field on trigger object

for(REPRO__Lead__c rl:trigger.new){

rl.Task_Start_Time__c = nextStart;
}
    

}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
 
This was selected as the best answer
mukesh guptamukesh gupta
Hi Admin,

A little mistake from your side:

As per salesforce best practice we need to use bulkify so what we need to do just below:-

for(REPRO__Lead__c repo_lead : trigger.new)
{
    repo_lead.Task_Start_Time__c = nextStart;
}

So please use below code:-
 
trigger NextAvailableBusinessMoment on REPRO__Lead__c (before insert) {

    
// Get the default business hours

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

// Starting from the System.now, find the next date when business hours reopens.
//  This returned next day start time will be in the local time zone

Datetime nextStart = BusinessHours.nextStartDate(bh.id,  System.now());

// print nextStart date/time in custom field on trigger object

for(REPRO__Lead__c repo_lead:trigger.new){  // this is bulkify step
    repo_lead.Task_Start_Time__c = nextStart;
}
    

}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
Admin User 10568Admin User 10568
Equal thanks to both @Ankaiah and @Mukesh for working triggers. You've both saved me a lot of research to work this out. Legends!