Tuesday, October 9, 2018

Execute powershell script from ServiceNow workflow run script activity

var pScript = "Script that needs to be executed";
var powershell = new PowershellProbe('MID_SERVER_NAME', gs.getProperty('mid.server.ip'));
powershell.setScript(pScript);
var resp = powershell.execute(true);
workflow.scratchpad.error = resp.error;
workflow.scratchpad.output = resp.output;
if(!resp.output.nil() && resp.output ! = 'null')
   current.work_notes = resp.output;
else if(!resp.error.nil()) {
current.work_notes = 'Error in script:  ' + resp.error;
}
gs.log('Response Output ' + resp.output);
gs.log('Response Error ' + resp.error);

Friday, October 5, 2018

Script to check if the current user is impersonating or not in SNOW

var CheckImpersonation = Class.create();
CheckImpersonation.prototype = {
    IsImpersonating: function() {
var gi=new GlideImpersonate().isImpersonating();
return gi;
    },

    type: 'CheckImpersonation'
};

Monday, October 1, 2018

Script to add variables to catalog item created through run script in workflow


var reqvar = new GlideRecord('sc_item_option_mtom');
reqvar.addQuery('request_item',current.sys_id);
reqvar.query();

while (reqvar.next()) {
var variablereference = new GlideRecord('sc_item_option');

if (variablereference.get(reqvar.sc_item_option)) {
var add_task_var = new GlideRecord('sc_item_variables_task');
add_task_var.task = tsk_sys_id;
add_task_var.variable = variablereference.item_option_new;
add_task_var.insert();

}
}

Thursday, September 27, 2018

ServiceNow cart API to request catalog item through script

var cart = new Cart();
var item = cart.addItem('12345678123456781234567812345678'); // sys id of the Catalog item
cart.setVariable(item, "employee", current.u_employee_name);
cart.setVariable(item, "employee_email", current.u_employee_email);
cart.setVariable(item, "employee_number", current.u_employee_number);
cart.setVariable(item, "termination_date", current.u_termination_date);
var rc = cart.placeOrder();

ServiceNow script to remove inactive user from all the groups and roles

function removeTheGroupsOfInactiveUser() {
        var groupGR = new GlideRecord('sys_user_grmember');
        groupGR.addQuery('user', current.u_employee_name);
        groupGR.query();
        while (groupGR.next()) {
                groupGR.deleteRecord();
        }
}


function removeTheRolesOfInactiveUser() {
        var roleGR = new GlideRecord('sys_user_has_role');
        roleGR.addQuery('user', current.u_employee_name);
        roleGR.query();
        while (roleGR.next()) {
                roleGR.deleteRecord();
        }
}

ServiceNow workflow run script to check if all the catalog tasks are closed for the current RITM

var tsk = new GlideRecord('sc_task');
tsk.addQuery('request_item', current.sys_id);
tsk.addActiveQuery();
tsk.query();

if(tsk.next()){
answer = false;
}
else{
answer = true;
}

Sunday, September 16, 2018

Add show workflow UI Action in ServiceNow for Global and scoped applications

For Global Application use the following script

onclik: showWorkflowContext()

condition: !current.isNewRecord() && (new Workflow().hasWorkflow(current))

function showWorkflowContext() {
   var id = g_form.getUniqueValue().toString();
   var url = new GlideURL('context_workflow.do');
   url.addParam('sysparm_stack', 'no');
   url.addParam('sysparm_table', g_form.getTableName());
   url.addParam('sysparm_document', id);
   g_navigation.open(url.getURL(), "_blank")
}

For Scoped Application use the following script

onclik: showWorkflowContext()

condition: !current.isNewRecord() && (new global.Workflow().hasWorkflow(current))
Script: Same as above