How to call workflow from script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-15-2022 01:45 AM
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());
}
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2022 12:32 AM
If the requirement is there we can use UI action as Client + Server
Ensure you don't use GlideRecord in client side code of UI action as it is not best practice.
you can use GlideAjax if you wish to perform some validation or check
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2022 12:58 AM
Hello Ankur,
Okay thanks, But I am unable to call the workflow in the script include. Can you please help me with that?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2022 01:05 AM
use GlideAjax and send current record sys_id using g_form.getUniqueValue()
and then use this script
var wfContext = new GlideRecord('wf_context');
wfContext.addQuery('id', this.getParameter('sysparm_sysId'));
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');
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2022 02:54 AM
Hello Ankur,
I have tried with the below code but it's not triggering the workflow. Can you please look into it and advise?
Ui action script:
function setRedirect() {
current.u_next_state = '4';
var workflow = new GlideAjax("YaraNSSRWorkflowUtil");
workflow.addParam("sysparm_name", "getDesignWorkflow");
workflow.addParam("sysparm_sysId", g_form.getUniqueValue());
workflow.getXMLAnswer(function(answer) {
answer;
});
current.update();
action.setRedirectURL(current);
}
Script include code:
getDesignWorkflow: function() {
var ans;
var wfContext = new GlideRecord('wf_context');
wfContext.addQuery('id', this.getParameter('sysparm_sysId'));
wfContext.addQuery('workflow_version.name', "Yara_NSSR_Before_Design_Approvals");
wfContext.query();
if (!wfContext.next()) {
var wf = new GlideRecord("wf_workflow");
ans = wf.startFlow(wf1.getWorkflowFromName('Yara_NSSR_Before_Design_Approvals'), current, 'update');
}
return ans;
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2022 03:25 AM
Hi,
which approach you want? because your have mixed up Ajax with Server side
only server?
OR
client + server?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader