How we can initiate a flow from ui action?

jobin1
Tera Expert

Hi Team,
How can we initiate a flow using ui action I tried below, but it's not working.

 

ui action script -runs on serviceside

(function executeAction(current) {
    // Set approval to 'Requested'
    current.approval = 'requested';
    current.update();
var ritmNumber = current.number;
  gs.log("inputsvalue0" +ritmNumber);
     var inputs = {
        ritmNumber:ritmNumber
    };
   

    // Log the input being sent to the flow
   gs.log("inputsvalue" + JSON.stringify(inputs));
    // Start the flow
    var flowAPI = new sn_fd.FlowAPI();
    flowAPI.startFlow('request_approve_flow1', inputs, gs.getUserID());
})(current);
9 REPLIES 9

JenniferRah
Mega Sage

A few questions/observations:

 

  1. What scope is your flow in? You didn't specify it in the startFlow line, so I'm wondering if the name might need to be global.request_approve_flow1 or something like that?
  2. You are sending the RITM number to your flow rather than the actual RITM variable. Is that what your flow is expecting?
  3. There are only 2 parameters for startFlow: flow name and inputs. You don't need the userID one.

If none of those seem to apply, maybe post some screen prints of your flow. It's hard to validate this code without seeing what it's supposed to run.

1. is sys_id is fine?
2.flow expecting RITM no
3.ok will remove 
can u suggest a code which will work in the above case?

SumanthDosapati
Mega Sage
Mega Sage

@jobin1 

You can generate a code snippet from the flow and use it.

  1. Go to your flow in flow designer
  2. Open your flow
  3. Click on Actions menu(three dots icon) on top right
  4. Select 'Create code snippet'

You can use this is your server side ui action script by adjusting accordingly.

 

Accept the solution and mark as helpful if it does, to benefit future readers.
Regards,
Sumanth

 

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@jobin1 

use this and share what came in logs

Is your flow published?

UI action and flow are in which scope?

You can use the Create Code snippet to get script on how to call it from any server side script.

   // Start the flow
   try {
       // Set approval to 'Requested'
       current.approval = 'requested';
       current.update();
       var ritmNumber = current.number;
       gs.info("inputsvalue0" + ritmNumber);
       var inputs = {
           ritmNumber: ritmNumber
       };
       // Log the input being sent to the flow
       gs.info("inputsvalue" + JSON.stringify(inputs));

       // Start Asynchronously: Uncomment to run in background.
       // sn_fd.FlowAPI.getRunner().flow('global.assign_remediation_task8e3').inBackground().withInputs(inputs).run();

       // Execute Synchronously: Run in foreground.
       sn_fd.FlowAPI.getRunner().flow('global.request_approve_flow1').inForeground().withInputs(inputs).run();

   } catch (ex) {
       var message = ex.getMessage();
       gs.error(message);
   }

AnkurBawiskar_0-1746774471197.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader