Skip to content

Instantly share code, notes, and snippets.

@tamarachance
Last active May 20, 2025 20:11
Show Gist options
  • Save tamarachance/d81f1272ac1ba185e80c81108c90a783 to your computer and use it in GitHub Desktop.
Save tamarachance/d81f1272ac1ba185e80c81108c90a783 to your computer and use it in GitHub Desktop.
Working with BusinessHours methods in Salesforce Flows
public class BusinessHoursInvocable {
public class Input {
@InvocableVariable(required=true)
public Id businessHoursId;
@InvocableVariable
public Datetime startDate;
@InvocableVariable
public Datetime endDate;
@InvocableVariable
public Long intervalMilliseconds;
@InvocableVariable
public Datetime targetDate;
}
public class Output{
@InvocableVariable
public Long diffMs;
@InvocableVariable
public Datetime addedDate;
@InvocableVariable
public Datetime addedGmtDate;
@InvocableVariable
public Boolean isWithinBusinessHours;
@InvocableVariable
public Datetime nextBusinessStartDate;
}
@InvocableMethod(label='Run Business Hours Calculations')
public static List<Output> runCalculations(List<Input> inputList) {
List<Output> results = new List<Output>();
for (Input ip : inputList) {
Output op = new Output();
// Run BusinessHours.diff() if start and end dates are present
if (ip.startDate != null && ip.endDate != null) {
op.diffMs = BusinessHours.diff(ip.businessHoursId, ip.startDate, ip.endDate);
}
// Run BusinessHours.add() & .addGmt() if startDate and interval are present
if (ip.startDate != null && ip.intervalMilliseconds != null) {
op.addedDate = BusinessHours.add(ip.businessHoursId, ip.startDate, ip.intervalMilliseconds);
op.addedGmtDate = BusinessHours.addGmt(ip.businessHoursId, ip.startDate, ip.intervalMilliseconds);
}
// Run BusinessHours.isWithin() & .nextStartDate() if targetDate is present
if (ip.targetDate != null) {
op.isWithinBusinessHours = BusinessHours.isWithin(ip.businessHoursId, ip.targetDate);
op.nextBusinessStartDate = BusinessHours.nextStartDate(ip.businessHoursId, ip.targetDate);
}
results.add(op);
}
return results;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment