0

The code is used in a flow to check if a case was created during business hours by case created date field. It works fine for a single record, but I need to be able to also use it to bulk update previous records, and that is where it's failing. Here is the code I'm using:

global class CheckforBH { 
    @InvocableMethod(label = 'Created Date is within BH' description = 'Check if flow record Created Date is within business hours') 
    public static List<Results> execute(List<Requests> requestList) {

        // Get the default business hours
        BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault = true];
    
        // Pass the Created Date Time of the flow Record
        Datetime targetTime = requestList[0].inputCreatedDateTime;

        // Boolean that returns true if date time is within Business hours
        Boolean isWithin = BusinessHours.isWithin(bh.id, targetTime);

        // Results object that holds the return values
        Results response = new Results();

        //Add the return values to the Results object
        response.outputMember = isWithin;

        //Wrap the Results object in a List container 
        List<Results> responseWrapper = new List<Results>();
        responseWrapper.add(response);
        return responseWrapper;
    }

    global class Requests {
        @InvocableVariable(label = 'Created Date Time' description = 'Created Date of the Flow record' required = false)
        global Datetime inputCreatedDateTime;
    }

    global class Results {
        @InvocableVariable(label = 'Within Business Hours' description = 'Boolean that returns true if the Created Date is within Business Hours' required = true)
        public Boolean outputMember;
    }
}
3
  • Are you working on a managed package? If not, it is highly recommended to change global access modifier to public.
    – Adrian Larson
    Feb 8, 2023 at 18:25
  • It's not a managed package. Just copied code someone else wrote. I can certainly edit global to public .
    – Emilie
    Feb 8, 2023 at 18:40
  • Just be aware that this is a "best practice" only. I would not expect changing to access modifier to public to fix anything (there is one thing that comes to mind that this could fix, name shadowing/collision, but it's extremely unlikely). This is more of a "avoid setting yourself up for failure" thing.
    – Derek F
    Feb 8, 2023 at 18:57

1 Answer 1

1

You must return one output for each input. This means using a for loop.

  @InvocableMethod(label = 'Created Date is within BH' description = 'Check if flow record Created Date is within business hours')
   public static List<Results> execute(List<Requests> requestList) {
    //Results object in a List container, one for each request
    List<Results> responseWrapper = new List<Results> ();
    // Get the default business hours
    BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault = true];
    for(Requests request:requestList) {
      // Pass the Created Date Time of the flow Record
      Datetime targetTime = request.inputCreatedDateTime;
      // Boolean that returns true if date time is within Business hours
      Boolean isWithin = BusinessHours.isWithin(bh.id, targetTime);
      // Results object that holds the return values
      Results response = new Results();
      response.outputMember = isWithin;
      //Add the return values to the Results object
      responseWrapper.add(response);
    }
    return responseWrapper;
}
4
  • Using the code you provided causes the following errors: Invalid type: Results (2:29) Invalid type: Requests (2:52) Invalid type: Results (4:2) Invalid type: Requests (7:6) Invalid type: Results (13:4) Variable does not exist: response (14:4) Variable does not exist: responseWrapper (16:4) Variable does not exist: request (9:26)
    – Emilie
    Feb 8, 2023 at 18:36
  • @Emilie Don't forget to keep your Requests and Results classes from your code.
    – sfdcfox
    Feb 8, 2023 at 18:47
  • Just saw that. Testing now.
    – Emilie
    Feb 8, 2023 at 18:52
  • Thank you so much for your assistance. It works!
    – Emilie
    Feb 8, 2023 at 19:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .