can we test subflow using background script in servicenow
- User
can we test subflow using background script in servicenow
- Assistant
In ServiceNow, when you want to test a subflow, you typically do so using Flow Designer's built-in testing capabilities. However, there may be situations where you want to trigger or test a subflow programmatically using a background script. You can achieve this using the ServiceNow scripting API, specifically the
FlowAPIclass.Here is a high-level overview and an example of how you can trigger a subflow using a background script:
-
Preparation Steps:
- Identify the
sys_idof your subflow. - Identify the necessary inputs your subflow requires.
- Identify the
-
Background Script:
- Use the
FlowAPIandFlowAPIParametersclasses to trigger the subflow and pass the required inputs.
- Use the
Here’s an example of how you might do this in a background script:
// The sys_id of the subflow you want to trigger var subflowSysId = '<SUBFLOW_SYS_ID>'; // Create parameters object var flowParams = new sn_fd.FlowAPIParameters(); // Add the necessary inputs for your subflow flowParams.addInput('param1', 'value1'); flowParams.addInput('param2', 'value2'); // Add more parameters as needed // Start the subflow try { var flowAPI = new sn_fd.FlowAPI(); var result = flowAPI.startSubflow(subflowSysId, flowParams); gs.info('Subflow started with execution ID: ' + result.executionId); } catch (e) { gs.error('Error starting subflow: ' + e.getMessage()); }Notes:
- Replace
<SUBFLOW_SYS_ID>with the actualsys_idof the subflow you want to test. - Replace
param1,param2, etc., with the names of the inputs your subflow expects, and provide appropriate values.
Explanation:
sn_fd.FlowAPI: ServiceNow's Flow Designer API class used to start flows and subflows programmatically.sn_fd.FlowAPIParameters: This class is used to encapsulate the input parameters required by the subflow.
By running this background script, you can effectively test and trigger your subflow in a controlled environment.
Remember to always test scripts in a development or sandbox instance first to avoid any unintended consequences in a production environment.
-
- User
Is this working var flowApiName = 'your_flow_api_name'; // Replace 'your_query' with a query that matches the 100 RITMs you want to target var ritmQuery = 'your_query';
var ritmGR = new GlideRecord('sc_req_item'); ritmGR.addEncodedQuery(ritmQuery); ritmGR.query(); while (ritmGR.next()) { // Trigger the Flow for each RITM sn_fd.FlowAPI.startFlow(flowApiName, null, null, { 'record': ritmGR }); }
- Assistant
Your approach is mostly correct, but there are a few key adjustments and best practices you should consider ensuring the script works properly:
- Defining the Flow API Name: Make sure you have the correct
sys_idor unique name of the flow that you want to trigger. A common approach is to store this in a variable. - Handling Errors: Adding error handling can help with troubleshooting.
- Using Proper Parameters: Correct input parameters need to be specified for the flow.
- Logging for Debugging: Including logging to understand the flow progress and results.
Below is a revised version of your script:
// The sys_id or API name of the flow you want to trigger var flowApiName = 'your_flow_api_name_or_sys_id'; // Replace 'your_encoded_query' with the actual query to target the 100 RITMs var ritmQuery = 'your_encoded_query'; var ritmGR = new GlideRecord('sc_req_item'); ritmGR.addEncodedQuery(ritmQuery); ritmGR.query(); while (ritmGR.next()) { try { // Trigger the Flow for each RITM var flowParams = { 'record': ritmGR }; var result = sn_fd.FlowAPI.startFlow(flowApiName, ritmGR, {}, flowParams); gs.info('Flow started for RITM ' + ritmGR.sys_id + ', Execution ID: ' + result.executionId); } catch (e) { gs.error('Error triggering flow for RITM ' + ritmGR.sys_id + ': ' + e.message); } }Explanation:
-
Flow API Name:
- Ensure that
flowApiNameis accurate.
- Ensure that
-
RITM Query:
- Ensure
ritmQueryis an encoded query string that matches the RITMs you want to target.
- Ensure
-
Triggering the Flow:
sn_fd.FlowAPI.startFlow(): This function starts the flow and requires parameters such as the flow name and input variables.- The
flowParamsobject includes therecordparameter, which is the currentritmGR(RITM GlideRecord).
-
Error Handling:
- Use try-catch for error handling to catch and log issues during the flow start process.
-
Logging for Debugging:
- Use
gs.info()to log successful flow initiations. - Use
gs.error()to log errors.
- Use
Please ensure you replace placeholders like
'your_flow_api_name_or_sys_id','your_encoded_query', and input parameters with actual values appropriate to your use case before running the script. Also, make sure to test the script in a non-production environment to validate its behavior. - Defining the Flow API Name: Make sure you have the correct