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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

sample script below for running workflow from background

ensure you use GlideRecord object of correct table

var gr = new GlideRecord('schedule table');

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

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

var gr = new GlideRecord('schedule table');

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

Do I need to update the script to add few lines?

Like 

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

}

 

Also I want to run it for already existing record.

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