• Nicolai Nonnenbroich
  • NEWBIE
  • 30 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 7
    Replies

Hi everyone,

With a lot of help from this forum, I got my first invocable method to work but now I am stuck on writing the test class for it.

Here's my invocable method and the attempted test class:

global class CreatedDateWithinBusinessHours {
        @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;
    }
}


@isTest
private class CDwithinBusinessHoursTest {
    static testMethod void validateCDBusinessHours() {
       
        Case  c = new Case ();
         
        Datetime recordCD = c.CreatedDate;
       
          insert c; 
        
        List<Datetime> requestList = new List<Datetime>();
        requestList.add(recordCD);
       
        Test.startTest();
         
        //Getting this error here: Method does not exist or incorrect signature: void execute(List<Datetime>) from the type CreatedDateWithinBusinessHours
        CreatedDateWithinBusinessHours.execute(requestList);
        
           Test.stopTest();
        
        //Variable does not exist: outputMember             
           System.assert(outputMember);
       
   }
}    

Hi everyone,

I'm completely new to Apex and invocable methods so any help would be greatly appreciated.

I'm trying to create an invocable method that uses the Business Hours class. I'd like to pass the Created Date of a record in a record-triggered flow into Apex and check if the Created Date is within Business Hours. I would then like to return a boolean as the output variable that I can then use in a decision element to trigger a record creation.

Here's what I have pasted together from what I could find via help articles and a previous post:

global class CreatedDateWithinBusinessHours {
        @InvocableMethod(label = 'Check if within BH' description = 'Check if record Created Date is within business hours')
            public static List<Results> execute(List<Requests> requestList) {
            List<SObject> inputCollection = requestList[0].inputCollection;
            // Get the default business hours
            BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault = true];
            
            // Is this where I would use my Created Date input variable I'm passing from the flow?
            Datetime targetTime = Datetime();

            // Find whether the time is within the default business hours. Is this the boolean I would use as the output variable to use in my flow?
            Boolean isWithin = BusinessHours.isWithin(bh.id, targetTime);

            //Create a Results object to hold 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 
            //(an extra step added to allow this interface to also support bulkification)
            List<Results> responseWrapper = new List<Results> ();
            responseWrapper.add(response);
            return responseWrapper;
    }

    //
    global class Requests {
        @InvocableVariable(label = '' description = 'Test Description' required = false)
        public List<SObject> inputCollection;
        
    }
    //
    global class Results {
        @InvocableVariable(label = 'Within Business Hours' description = '' required = true)
        public Boolean outputMember;
    }
}

 

Hi,

 

This is my first question and I'm completely new to Apex.

I'm essentially trying to write am invocable class that I can use in a flow. It should check now() against the default business hours and return a boolean as an output variable that I can then use in a decision element. 

I think I found the piece on writing the query for business hours but I'm not sure how to turn that into an invocable action that I can use in a flow.

Any help would be greatly appreciated!!

Hi everyone,

With a lot of help from this forum, I got my first invocable method to work but now I am stuck on writing the test class for it.

Here's my invocable method and the attempted test class:

global class CreatedDateWithinBusinessHours {
        @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;
    }
}


@isTest
private class CDwithinBusinessHoursTest {
    static testMethod void validateCDBusinessHours() {
       
        Case  c = new Case ();
         
        Datetime recordCD = c.CreatedDate;
       
          insert c; 
        
        List<Datetime> requestList = new List<Datetime>();
        requestList.add(recordCD);
       
        Test.startTest();
         
        //Getting this error here: Method does not exist or incorrect signature: void execute(List<Datetime>) from the type CreatedDateWithinBusinessHours
        CreatedDateWithinBusinessHours.execute(requestList);
        
           Test.stopTest();
        
        //Variable does not exist: outputMember             
           System.assert(outputMember);
       
   }
}    

Hi everyone,

I'm completely new to Apex and invocable methods so any help would be greatly appreciated.

I'm trying to create an invocable method that uses the Business Hours class. I'd like to pass the Created Date of a record in a record-triggered flow into Apex and check if the Created Date is within Business Hours. I would then like to return a boolean as the output variable that I can then use in a decision element to trigger a record creation.

Here's what I have pasted together from what I could find via help articles and a previous post:

global class CreatedDateWithinBusinessHours {
        @InvocableMethod(label = 'Check if within BH' description = 'Check if record Created Date is within business hours')
            public static List<Results> execute(List<Requests> requestList) {
            List<SObject> inputCollection = requestList[0].inputCollection;
            // Get the default business hours
            BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault = true];
            
            // Is this where I would use my Created Date input variable I'm passing from the flow?
            Datetime targetTime = Datetime();

            // Find whether the time is within the default business hours. Is this the boolean I would use as the output variable to use in my flow?
            Boolean isWithin = BusinessHours.isWithin(bh.id, targetTime);

            //Create a Results object to hold 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 
            //(an extra step added to allow this interface to also support bulkification)
            List<Results> responseWrapper = new List<Results> ();
            responseWrapper.add(response);
            return responseWrapper;
    }

    //
    global class Requests {
        @InvocableVariable(label = '' description = 'Test Description' required = false)
        public List<SObject> inputCollection;
        
    }
    //
    global class Results {
        @InvocableVariable(label = 'Within Business Hours' description = '' required = true)
        public Boolean outputMember;
    }
}

 

Hi,

 

This is my first question and I'm completely new to Apex.

I'm essentially trying to write am invocable class that I can use in a flow. It should check now() against the default business hours and return a boolean as an output variable that I can then use in a decision element. 

I think I found the piece on writing the query for business hours but I'm not sure how to turn that into an invocable action that I can use in a flow.

Any help would be greatly appreciated!!