Wednesday, February 14, 2018

Create a user approval through script in ServiceNow workflow


var approval_gr = new GlideRecord('sysapproval_approver');
approval_gr.initialize();
approval_gr.state = 'requested';
approval_gr.approver = gs.getUserID();
approval_gr.sysapproval = current.sys_id;
approval_gr.insert();

The approver and sysapproval columns should be replaced according to your required values.

Monday, February 12, 2018

Make attachment mandatory for catalog item submission

We can use the following catalog client script onSubmit


var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", g_form.getUniqueValue());
gr.query();

if (!gr.next()) {
alert("Attachment mandatory.");
return false;
}

Friday, February 9, 2018

Cancel a workflow using a script when any one of RITM approvals are rejected | ServiceNow

Use the following script in the run script activity of the current workflow



answer = true;
var sag = new GlideRecord('sysapproval_group');
sag.addQuery('parent', current.sys_id);
sag.query();
while(sag.next()) {
if(sag.approval == 'rejected') {
current.state = '4';

var w = new Workflow();
var gr = new GlideRecord('wf_context');

if (gr.get(current.context))
w.cancelContext(gr);
}
}