Call flow from business rule

Servicenow Use4
Kilo Guru

Hello Experts!

 

I have a requirement to call a Flow from business rule. Now, online I'm getting 2 versions of syntax to do so:

sn_fd.FlowAPI.executeFlow('<<flow_name>>>', inputs);
AND 
sn_fd.FlowAPI.getRunner().flow('<<flow_name>>').inForeground().withInputs(inputs).run();
 
1. which one is correct?

2. My next question is: how does the flow receive the inputs that I'm passing from the business rule?
I need the inputs so that I can use these in the custom actions that I will be calling in my flow.
 
3. Silly question (maybe), Is it possible that the flow should not have a trigger defined within the flow.. but should only be executed when invoked from the Business rule?
 
Looking for urgent help.
 
Many thanks in advance!
 
1 ACCEPTED SOLUTION

Craig Gruwell
Mega Sage

Hi @Servicenow Use4,

 

Based on the documentation for .executeFlow():

 

Note: This API is replaced by ScriptableFlowRunner, which deprecates the existing methods used to build objects and execute Workflow Studio flows and actions. Use the getRunner() method in the FlowAPI class to return a ScriptableFlowRunner object and use the associated methods. Use the ScriptableFlowRunner methods if you need to support domain separation.

 

Based on the above, the preferred method is the getRunner() method.  

 

To pass in the inputs, you can refer to the doc (or below)

 

https://developer.servicenow.com/dev.do#!/reference/api/xanadu/server/sn_fd-namespace/ScriptableFlow... 

 

 

 

(function() {
  try {

    var inputs = {}; 

    inputs['sys_id'] = '57af7aec73d423002728660c4cf6a71c';  // Pass the record's sys_id in as input.  

    var result = sn_fd.FlowAPI.getRunner()  // Create a ScriptableFlowRunner builder object.
      .action('global.markapproved')        // Run the global scope action named markapproved.
      .inForeground()
      .inDomain('TOP/ACME')                 // Run the action from the TOP/ACME domain.                               
      .withInputs(inputs)
      .run();                               // Run the action and return a FlowRunnerResult object.

    var contextId = result.getContextId();  // Retrieve the context ID from the result
    var dateRun = result.getDate();
    var domainUsed = result.getDomainId();  // Retrieve the Domain ID from the result.
    var flowName = result.getFlowObjectName();
    var flowObjectType = result.getFlowObjectType();

    var outputs = result.getOutputs();            // Retrieve any outputs from the action execution.
    var newApprovalStatus = outputs['approval'];  // Echo back the approval status for verification.
		
  } catch (ex) {
    var message = ex.getMessage();
    gs.error(message);
  }
	
})();

 

 

 

For the 3rd question, you'll have to define a trigger as part of the flow.  You can always select "Run Once" with a date in the past.  Another option might be to use a subflow (instead of a flow).  You can call the subflow via script and in that case of the subflow, there is no trigger.

 

View solution in original post

3 REPLIES 3

Craig Gruwell
Mega Sage

Hi @Servicenow Use4,

 

Based on the documentation for .executeFlow():

 

Note: This API is replaced by ScriptableFlowRunner, which deprecates the existing methods used to build objects and execute Workflow Studio flows and actions. Use the getRunner() method in the FlowAPI class to return a ScriptableFlowRunner object and use the associated methods. Use the ScriptableFlowRunner methods if you need to support domain separation.

 

Based on the above, the preferred method is the getRunner() method.  

 

To pass in the inputs, you can refer to the doc (or below)

 

https://developer.servicenow.com/dev.do#!/reference/api/xanadu/server/sn_fd-namespace/ScriptableFlow... 

 

 

 

(function() {
  try {

    var inputs = {}; 

    inputs['sys_id'] = '57af7aec73d423002728660c4cf6a71c';  // Pass the record's sys_id in as input.  

    var result = sn_fd.FlowAPI.getRunner()  // Create a ScriptableFlowRunner builder object.
      .action('global.markapproved')        // Run the global scope action named markapproved.
      .inForeground()
      .inDomain('TOP/ACME')                 // Run the action from the TOP/ACME domain.                               
      .withInputs(inputs)
      .run();                               // Run the action and return a FlowRunnerResult object.

    var contextId = result.getContextId();  // Retrieve the context ID from the result
    var dateRun = result.getDate();
    var domainUsed = result.getDomainId();  // Retrieve the Domain ID from the result.
    var flowName = result.getFlowObjectName();
    var flowObjectType = result.getFlowObjectType();

    var outputs = result.getOutputs();            // Retrieve any outputs from the action execution.
    var newApprovalStatus = outputs['approval'];  // Echo back the approval status for verification.
		
  } catch (ex) {
    var message = ex.getMessage();
    gs.error(message);
  }
	
})();

 

 

 

For the 3rd question, you'll have to define a trigger as part of the flow.  You can always select "Run Once" with a date in the past.  Another option might be to use a subflow (instead of a flow).  You can call the subflow via script and in that case of the subflow, there is no trigger.

 

Thanks! using a subflow instead of a flow worked.

 

P.S, Apologies for the delayed response 🙂

maheshkhatal
Mega Sage
Mega Sage

@Servicenow Use4 you can copy the code snippet from the flow designer and paste it in the BR and pass the required parameter. See the help from the SN Community 

Run Flow from The Business Rules 

 

If my response helped you kindly mark it as helpful.

Thank you,

Mahesh.