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

Remove all the conditions. sys_id in the conditions part is the sys_id of the record that belongs to the table on which the workflow is defined. 

-What table is your workflow on ? Global ? 

Did you publish your workflow after making changes to it ?

12345647891023456789123456789 is this your real sys_id or a sample one ? 

What is telling you that your workflow is not getting triggered ? 

My UI Action button is set to table sc_task.

The workflow I set to Global and I didn't publish the workflow after making changes.

I know it isn't triggered because I do not see it in the "Workflow All Contexts" log.

That is not the real sys_id.

 

I did a little test and changed the workflow table to sys_ui_action.  When I hit the button that I created on the catalog task list that didn't work, but if I made a change to the UI Action Button then the workflow ran.

If you want to trigger the workflow from UI Action or any other script then you must do the below:

1. Remove the workflow conditions, make If Condition matches select box as '-none-'

2. If you are triggering from UI Action you have to consider two things here:

        a) if you are doing either client side operation or server side operation but not both then you can write the code appropriately.

        b) if you are performing both operations then make sure you are using gsftSubmit() function to call server side script.

 

The below could help you which is similar as yours but not hardcoded.

 

var wf = new GlideRecord("wf_workflow");
wf.addQuery("name", gs.getProperty("<property name where you have mentioned name of your workflow>")); //Define your workflow name in the property
wf.query();
wf.next();
var wflw = new Workflow();
wflw.startFlow(wf.getValue('sys_id'), current, current.operation());

 

Please mark 'Correct' or 'Helpful' if it is so

I appreciate the help.

I set my workflow table to global, is that what it should be or should it be sys_ui_action?

I changed the conditions to none.

I added the code you provided, because the UI Action button is located at the bottom of the catalog task and you must select the records you want to export, I assume that client side and server side is needed.  Is this correct?

 

Could you please post the screenshots of entire UI Action definition and Workflow definition