Triggering workflow from UI action

pritimayee
Tera Contributor

Hello,

Can anyone please help me on this below requirment.

I have created a new state as Draft for change request.Created new ui action'submit request'.this button will be visible on all type change request like emergency standard and normal and the when the state is in draft.

Once click on the ui action 'submit request' the normal workflow that has been defined for all the type request will start.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

You can use this script to trigger workflow from script

Remember UI Action should be server side

triggerWorkflow();

function triggerWorkflow(){

var wf1 = new Workflow();
wf1.startFlow(wf1.getWorkflowFromName('give the workflow name'), current, 'update');

}

I would insist if possible make the workflow trigger based on the proper condition

Regards
Ankur

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

View solution in original post

11 REPLIES 11

Yash Agrawal1
Tera Guru

Hello,

Please try the below code.

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());

}

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 approve 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"};

 

Please Mark it helpful/correct if my answer helps in any way to resolve your query.
Reach out to me if any more help required.

Regards

Yash.K.Agrawal

Priyanka Patil2
Tera Contributor

You can have a look on below Docs : 

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/c_WorkflowAPI

////where current is a task record with a workflow context

var w = new Workflow();

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

Parameters
NameTypeDescription
workflowIdStringThe sys_id of the workflow to start. This sys_id refers to table wf_workflow.
currentGlideRecordThe record to use as current in this workflow. This is normally from the Table field of the workflow properties for this workflow.
operationStringThe operation to perform on current. Possible values: insertupdatedelete.
varsArrayCollection of variables to add to the workflow

Please mark correct/helpful based on impact.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

You can use this script to trigger workflow from script

Remember UI Action should be server side

triggerWorkflow();

function triggerWorkflow(){

var wf1 = new Workflow();
wf1.startFlow(wf1.getWorkflowFromName('give the workflow name'), current, 'update');

}

I would insist if possible make the workflow trigger based on the proper condition

Regards
Ankur

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

Hello Ankur,

Thank you for reply.I have three workflow defined for each type like emergency,Standard,normal.

 

So do i need try in this way.

find_real_file.png