How to call workflow from script include

Rama Rao
Tera Contributor

Hello,

I need to call the workflow from client-side code using script include. I have tried with the below code but it did not work.

Can anyone please help me to solve this issue? I am unable to call the workflow through this code.

Clientside code :

function setRedirect() {

current.u_next_state = '4';
var workflow = new GlideAjax("YaraNSSRWorkflowUtil");
workflow.addParam("sysparm_name", "getDesignWorkflow");
workflow.addParam("sysparm_profile", g_form.getUniqueValue());
workflow.getXMLAnswer(function(answer) {
answer;
});
}

 

Script Include Code:

getDesignWorkflow: function() {
var w = new Workflow();
var name = this.getParameter("sysparm_profile");
var wfContext = new GlideRecord('wf_context');
wfContext.addQuery('sys_id', name);
wfContext.addQuery('workflow_version.name', "Yara_NSSR_Before_Design_Approvals");
wfContext.query();
if (!wfContext.next()) {

var wf = new GlideRecord("wf_workflow");
if (wf.get("name", "Yara_NSSR_Before_Design_Approvals"))

return w.startFlow(wf.sys_id, current, current.operation());

}
},

24 REPLIES 24

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you are running client script on which form?

workflow is on which table?

you need to query that table and pass the record as GlideRecord object

something like this

var wf1 = new Workflow();

wf1.startFlow(wf1.getWorkflowFromName('give the workflow name'), obj, 'update');

Regards
Ankur

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

Hello Ankur,

I need to run the script on UI action(NSSR table) and in the UI action client is true. the workflow also NSSR table.

 

Not required query the "wf_context" table?

Hi,

then why not make that UI action as server side and no need of script include

I assume you want to start workflow for the current record

Use this in server side UI action

var wfContext = new GlideRecord('wf_context');
wfContext.addQuery('id', current.sys_id);
wfContext.addQuery('workflow_version.name', "Yara_NSSR_Before_Design_Approvals");
wfContext.query();
if (!wfContext.next()) {
	var wf = new GlideRecord("wf_workflow");
	wf.startFlow(wf1.getWorkflowFromName('Yara_NSSR_Before_Design_Approvals'), current, 'update');
}

Regards
Ankur

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