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

Hello Ankur,

I need only client-side because I have enabled the client field in UI action.

then use GlideAjax

function setRedirect() {
	var workflow = new GlideAjax("YaraNSSRWorkflowUtil");
	workflow.addParam("sysparm_name", "getDesignWorkflow");
	workflow.addParam("sysparm_sysId", g_form.getUniqueValue());
	workflow.addParam("sysparm_tableName", g_form.getTableName());
	workflow.getXMLAnswer(function(answer) {
	});
}

Script Include:

getDesignWorkflow: function() {
	var sysId = this.getParameter('sysparm_sysId');
	var table = this.getParameter('sysparm_tableName')
	var wfContext = new GlideRecord('wf_context');
	wfContext.addQuery('id', sysId);
	wfContext.addQuery('workflow_version.name', "Yara_NSSR_Before_Design_Approvals");
	wfContext.query();
	if (!wfContext.next()) {
		var rec = new GlideRecord(table);
		rec.get(sysId);
		var wf = new GlideRecord("wf_workflow");
		wf.startFlow(wf1.getWorkflowFromName('Yara_NSSR_Before_Design_Approvals'), rec, 'update');
	}
},

Regards
Ankur

 

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

Hello Ankur,

Tried with the above code but still, it's not triggering workflow. Need more help to sove this?

Can we call the script include in workflow instead of ui action?

Hi,

you want to call workflow from Script include using GlideAjax

So the above code I shared would work fine

what debugging have you performed?

Did you check the table and sysId came fine in script include?

did it go inside the if condition?

Regards
Ankur

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

Hello Ankur,

Logs and alerts are not triggering. I think the code not working. Can you please look into it once?