Trigger workflow from UI Action Button

Dazler
Mega Sage

Hi,

Can you trigger a workflow from a UI Action button? Or do I have to create a business rule to trigger the workflow?

I found this and added it to the UI Action button, but it is not working.

var id = '12345647891023456789123456789'; //sys_id of workflow

startWorkflow(id);

function startWorkflow(id) {

var w = new Workflow();

var context = w.startFlow(id, current, current.operation(),getVars());

}

 

I am still quite new to ServiceNow and I am a fast learner.  Any assistance would be greatly appreciated.

19 REPLIES 19

Dazler
Mega Sage

Is there anyone that can help me with this?

Raj68
Mega Guru

Hi,

Let the workflow start itself based on a condition in the workflow properties (State | is | Approval - for example.) Let the UI action set the state to approval and let the workflow start itself.

 

or

 

There's a Workflow object accessible server side that you can use to start a workflow that's documented here.

Here's an example from that wiki article:

 

// where current is a task record with a workflow context
var w = new Workflow();
var context = w.startFlow(id, current, current.operation(), vars);
  • current: A GlideRecord that's been next()'ed to the record against which you're running the workflow
  • id: The sys_id of the wf_workflow you want to run (NOTE: This is NOT the workflow version, the startFlow method handles determining which version is published and executes against it.
  • vars: The input variables that your target workflow accepts. This should be a JavaScript associative array e.g.: var vars = {variable1: "value1", variable2: "value2"};

 

 

NOTE: Mark correct or helpful if it helps you.

 

 

Warm Regards,

Raj patel

 

 

Pranay Tiwari
Kilo Guru

Hi Dazler;

 

Workflow need trigger condition to trigger,so you can give trigger condition by UI Action so workflow will trigger by UI Action.

If you have any hidden string field in your form then push some value on them and give this value as workflow trigger condition.

if not then in push any string field as below...

 

 

 

it will definitely work ...

if face any issue then please ask..!

 

Mark correct or helpful if it helps you.

 

Warm Regards,

Pranay Tiwari

| www.DxSherpa.com | pranay.tiwari@dxsherpa.com |

 

Hi,

It worked when I manually changed the catalog task.  But I need it to work when the catalog task records are selected on the list and the UI action button is clicked.

How do I get that to work?

Since I need to update a field on the record, I decided to use work notes.  I would like to add a work notes that says Exporting List that when the UI button is hit it will add to all the records selected updating the work notes which will trigger the workflow to run.

Hi,

I finally got it to worked.  Once I simplified my script by using the below code in my UI Action.

current.work_notes = 'Export user information to CSV';
current.update();

I thought I had to account for each item selected with some elaborate script, but it ended up being for simple.

 

Thank you to each of you for your help with this.