Thursday, October 25, 2018

Mark terminated LDAP users as inactive in ServiceNow


We Find inactive LDAP accounts using the last refresh time. In this method, we add a Last Refreshed field to the user record and set the value during the import process. We create a scheduled job that checks for users that have not been refreshed in 30 days, and deactivate them.

 Create a datetime field on the User [sys_user] table. example, u_last_refreshed.

Add the following code in the transform script

target.u_last_refreshed = gs.now();

Create a scheduled job to find and deactivate the user accounts that have not been refreshed in 30 days.


disable_users();

function disable_users() {
    /*
     * query for active users with ldap source and last updated more than 30 days ago
     * disable them
     */
    var gr = new GlideRecord("sys_user");
    gr.addQuery('u_last_refreshed', '<', gs.daysAgoStart(30));
    gr.addQuery('active', true);
    gr.addQuery('source', '!=', '');
    gr.query();
    while (gr.next()) {
        gr.active = false;
        gs.log("Disabled inactive user: " + gr.user_name + " - last updated: " + gr.u_last_refreshed);
        gr.update();
    }
    gs.log("Completed disabling inactive accounts");
}



Tuesday, October 16, 2018

Script include script is not working from Service Portal catalog client script GlideAjax

Change the privacy setting for a single client-callable script include by adding theisPublic() function.

The isPublic() setting takes precedence over the glide.script.ccsi.ispublic property. For example, if the property is set to false making all client-callable script-includes private, and a script sets isPublic() to true, the script is public.

To change the privacy for a single client-callable script include, add the following method to the script include - The highlighted method.

var RITMUtils = Class.create();
RITMUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isPublic: function () {
return true;
},
type: 'RITMUtils'
});

Tuesday, October 9, 2018

Call a servicenow workflow thorough business rule script

var vars = {};
var w = new Workflow();
var context = w.startFlow('sys_id_of_the_workflow', current, current.operation(), vars);

Sys id of the workflow can be obtained by looking at the wf_workflow table

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();

}
}