- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2024 11:07 PM
I have a few questions here:
1) Can I create a flow without a trigger defined? I want to be able to call it on demand from a script.
2) How can I call a flow from a script include?
3) On the beginning of the flow, I want to know who the logged in user is, how do I get the logged in user?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2024 11:53 PM
Hi @begsa
- No, Flows need a trigger. But you can use Subflows instead. They can be invoked from a script without a trigger.
- It is extensively explained at https://developer.servicenow.com/dev.do#!/learn/learning-plans/vancouver/servicenow_application_deve...
- You can use a Flow variable and assign the respective value to that Flow variable via inline script:
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2024 01:43 AM
1) Can I create a flow without a trigger defined? I want to be able to call it on demand from a script. - Yes, you can create a flow without a trigger defined. This is known as a subflow in ServiceNow. Subflows can be called from other flows or scripts. 2) How can I call a flow from a script include? - You can call a flow from a script include using the FlowAPI class. Here is a sample code: javascript var flow = new sn_fd.FlowAPI(); var inputs = {}; inputs['input1'] = 'value1'; // replace with your actual inputs var flowName = 'x_snc_my_namespace.my_flow'; // replace with your actual flow name flow.startFlow(flowName, null, null, inputs); 3) On the beginning of the flow, I want to know who the logged in user is, how do I get the logged in user? - You can get the logged in user using the gs.getUserID() method in a script. Here is a sample code: javascript var userID = gs.getUserID(); - In a flow, you can use the Current User data pill from the Flow Context data pills category.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2024 01:49 AM
* Yes, you can create a flow without a trigger in ServiceNow. This is known as a subflow. Subflows can be called from other flows, scripts, or script actions. Here are the steps to create a subflow and call it from a script: 1. Navigate to Flow Designer in ServiceNow. 2. Click on New -> New Subflow. 3. Define your subflow actions. 4. Save and publish your subflow. To call this subflow from a script, you can use the FlowAPI class. Here is a sample script: javascript var flowAPI = new global.FlowAPI(); var subflowName = 'your_subflow_name'; var inputs = { 'input1': 'value1', 'input2': 'value2' }; flowAPI.executeSubflow(subflowName, inputs); In this script: - FlowAPI is the class provided by ServiceNow to interact with flows and subflows. - executeSubflow is the method to execute a subflow. It takes two parameters: the name of the subflow and the inputs to the subflow. Please replace 'your_subflow_name', 'input1', 'value1', 'input2', 'value2' with your actual subflow name and inputs.
** function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue == '') { return; } var modeToggler = $$("[data-type=mode_toggler]")[0]; // set the operation to execute for processors if (newValue == 'processor' || newValue == 'client_callable_script_include' || newValue == 'REST_Endpoint' || newValue == 'client_callable_flow_object' || newValue == 'GraphQL'){ g_form.setValue('operation', 'execute', 'execute'); g_form.setReadOnly('operation', true); if (modeToggler != null) modeToggler.hide(); } else { g_form.setReadOnly('operation', false); if (newValue == 'ui_page') //for ui_page too, do not show the toggler modeToggler.hide(); else modeToggler.show(); } }
*** In ServiceNow, you can get the logged in user's information using the 'gs.getUser()' method. Here is a sample code: javascript var user = gs.getUser(); gs.info(user.getUserName()); This code will return the user name of the currently logged in user. To summarize: - Use the 'gs.getUser()' method to get the logged in user's information. - The 'getUserName()' method can be used to get the user name of the currently logged in user. - The 'gs.info()' method is used to log the user name in the system log.