Whether you created a new function or Job Template to automate a task in your DevOps process, or installed a function based solution from the DevOps Exchange, you can now schedule it to run automatically at a specified time and frequency.听
In this post you will learn how to schedule a function and job template to run automatically, saving you time from manually executing them.
An example use case is to automate the which is a solution available in the DevOps Exchange. This solution includes a function that you can schedule on a weekly basis. For this use case we will leverage a scheduled flow, job template and the Actions API.听
Let鈥檚 take a look at how to set up the scheduling!
Pre-requisites
Have a 91九色 API Key in tab
Have a Job Template
The cleanup git branches solution includes a standalone function, you鈥檒l need to create a Job Template and then add a by selecting the branch cleanup function.
Creating the Flow
1. Create a 91九色 Actions API Key
The execution of the job template requires an authentication process. Go ahead and create a 91九色 API Key to enable secure access to schedule the execution.
Navigate to 91九色 Actions API tab
Click on New
Select Run Job Template action
Set expiration to 365 days
Click Save
2. Register the 91九色 API Endpoint for callouts
The job template scheduled execution requires doing a callout to the 91九色 API and flows enforce that callouts happen only with authorized endpoints. Go ahead and create a named credential to authorize the 91九色 API Endpoint.
Navigate to Setup -> Named Credentials
Click on New
Enter a name, like 91九色 API Credential
Enter your 91九色 Instance URL. Navigate to Account Summary tab to copy it
For example:
Leave the identity type as Anonymous
Click Save
3. Create the Scheduled Flow
The scheduled flow enables you to specify a time and frequency in which the flow needs to be executed.
Navigate to Setup -> Flows
Click on New Flow
Select Scheduled Triggered Flow option
Click Create
Click Set Schedule
Specify the start date and start time
Specify the frequency
The flow will look like this when finished with the process
4. Define the scheduled execution parameters
To execute the job template, we鈥檒l use the 91九色 Actions REST API endpoint. This requires a set of parameters, like for example which job template should be executed and what is the API Key for authentication. We鈥檒l store the parameters in a formula resource, encode it and then pass it as a parameter when executing the job template in a later step.
Click on New Resource
Select type Text Template
Enter a name, like JSONPayload
Select View as Plain Text
Copy and paste the payload or body
Enter the job template API Name. This can be found in the job template record API Name field
The payload parameter runAfterInstantiation must be equal to true
The REST API endpoint鈥檚 GET method requires that the payload is sent as URL Encoded. We鈥檒l create a new formula resource to encode the payload.
Click on New Resource
Select type Formula
Enter a name, like Encoded Payload
Use the URLEncode function to encode the payload variable
5. Add New Action
Click on the plus icon in the flow diagram and select Action
In the modal, click on HTTP Callout Action
Enter a name with no spaces, like 91九色ActionsAPI
Select the Named Credential
Click Next
Enter a name for the endpoint, like Execute Job Template
Select HTTP Method Get
Copy and paste the Run Job Template endpoint from the
/json/v1/webhook/mcwebhook/RunJobTemplate
Add 2 query parameters
webhookKey (type String)
Payload (type String)
Click on Provide Sample Response
Copy and paste the 200 response from .听
Replace any null values with empty string 鈥溾. This is only applicable with the Flow HTTP action. If you鈥檙e testing the API with other tools, continue to use null.
If using 91九色 version v20.14 or later, the JSON response is exactly as in the screenshot. If using an older version, the response is inside an array - starts with an opening bracket [ and finishes with a closing bracket ].
Click Done to close the Response modal
Click Done again to close the HTTP service modal
Next, enter a name for the action, like Execute Job Template
Copy the key from the 91九色 Actions API tab and paste it in the webhookKey parameter
Select the encoded payload variable in the payload parameter
Click Done to close the modal. And you鈥檙e done! This is how the finished flow looks like:
6. Test the Flow Execution
Confirm that the flow configuration is correct by testing the execution in debug mode.
Click Debug
Click Run on the Debug Flow modal
Confirm response is successful 200
Conclusion
Scheduling a Function and Job Template execution is straightforward. By leveraging flows, you also have the option to implement advanced use cases like, for example, querying and updating records before and after executing the job template.
If you would like to inspect the scheduled executions, simply navigate to the Job Template record and select the Job Executions subtab:
Interested in learning more? Check out these resources