Run workflow from background script

Khanna Ji
Tera Guru

Hi Team,

I have a workflow that is running on a schedule. I wanted to check if I can run that workflow from background script on ad-hoc without modifying the workflow?

Please help me with a script as I need to test few things by running the workflow on ad-hoc basis

1 ACCEPTED SOLUTION

Hi,

yes few updates

just give the workflow name as well

var gr = new GlideRecord('table');

gr.addQuery('sys_id','sys id of a record');

gr.query();

if(gr.next())

{

var wflw = new Workflow();
wflw.startFlow(wflw.getWorkflowFromName('give the workflow name'), gr, 'insert');

}

For insertion

var gr = new GlideRecord('table');

gr.initialize();

gr.insert();

var wflw = new Workflow();
wflw.startFlow(wflw.getWorkflowFromName('give the workflow name'), gr, 'insert');

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

Regards
Ankur

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

View solution in original post

8 REPLIES 8

Hope you are doing good.

Let me know if that answered your question.

If so, please mark my response as correct & 👍 helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

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

Hi,

yes few updates

just give the workflow name as well

var gr = new GlideRecord('table');

gr.addQuery('sys_id','sys id of a record');

gr.query();

if(gr.next())

{

var wflw = new Workflow();
wflw.startFlow(wflw.getWorkflowFromName('give the workflow name'), gr, 'insert');

}

For insertion

var gr = new GlideRecord('table');

gr.initialize();

gr.insert();

var wflw = new Workflow();
wflw.startFlow(wflw.getWorkflowFromName('give the workflow name'), gr, 'insert');

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

Regards
Ankur

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

Chander Bhusha1
Tera Guru

Hi Servicenow User,

 

 

You have to mention the gliderecord object of the record whose workflow you wan't to start 

Replace the current with the glidercord objec.

 

var w = new Workflow();

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

In stead of current you can give glide record: gr (it must be glide record)

id: The sys_id of the workflow to start. This sys_id refers to table wf_workflow.

operation: insert or update

 

Mark helpful and correct if it helps.

Thanks,

CB

Himanshu Dubey
Giga Guru

Hi 

You can use below script

var gr = new GlideRecord('your_table');

gr.addQuery('sys_id','sys_id of your record');

gr.query();

if(gr.next())

{

var wflw = new Workflow();
wflw.startFlow(wflw.getWorkflowFromName('your workflow name'), gr, 'insert');

}


// To insert

var gr = new GlideRecord('your_table');

gr.initialize();

gr.insert();

var wflw = new Workflow();
wflw.startFlow(wflw.getWorkflowFromName('give the workflow name'), gr, 'insert');


// To update

var gr1 = new GlideRecord('your_table');

gr1.addQuery('sys_id', sys_id of your record);

gr1.query();

if(gr1.next())
{

var wflw = new Workflow();
wflw.startFlow(wflw.getWorkflowFromName('give the workflow name'), gr1, 'update');
}

Mark Correct if my answer solves your issue and also mark 👍 Helpful if you find my response worthy.

Thanks & Regards

Himanshu Dubey