Server Side

Remain on the current form


Get all table field values

server side script include script:

getAllTableFieldVals: function (tableName, changeType, changeRisk, supportGroup, instance){
       var xmlStr='<xml><fields>';
       var grGrp= new GlideRecord(tableName);
       grGrp.addQuery('u_change_type', changeType);
       grGrp.addQuery('u_active', true);
       if (!gs.nil(changeRisk) && changeType=='Normal'){
           grGrp.addQuery('u_risk_level', changeRisk);
       if (supportGroup!=''){
           grGrp.addQuery('u_support_group', supportGroup);
       if (instance!=''){
           grGrp.addQuery('u_instance', instance);
       if ({
           gs.log('Data lookup sys id: ' + grGrp.sys_id + '; tableName: ' + tableName + '; changeType: ' + changeType + '; changeRisk: ' + changeRisk + '; supportGroup: ' + supportGroup + '; instance: ' + instance, 'getAllTableFieldVals');
           var fields  = grGrp.getFields();
           for (var num=0; num<fields.size(); num++) {
               var ed = fields.get(num).getED();
               var edStr=ed.toString();
               if (edStr.indexOf ('u_')>-1){
                   if  (!gs.nil(grGrp[edStr])){
                       xmlStr+='<tableColumn>' + edStr + '</tableColumn>';
                       xmlStr+='<label>' + ed.getLabel() + '</label>';
                       xmlStr+='<value>' + grGrp[edStr].getDisplayValue()+ '</value>';
       return xmlStr;

Prevent business rules from running in a script


Service Catalog stuff

loop round variables with similar names to get values:

current.variables.<variable_name> works fine when you know the variable name

but what if you have a number of repeat variables with very similar names on the catalog item form, such as

And you wish to loop round and get all the values, and then use them to carry out a repeat task (for example, insert new CIs into the CMDB)?

must be an easier way of doing this...but until I find it here's some code that does the trick:

repeat fields on the catalog item form:

code to loop round these, for example in the RITM workflow:
//--get a load of variable values which are named very similar
var grRITM=new GlideRecord('sc_req_item');
if (grRITM.get('number', 'RITM0016189')){

   getVariables (grRITM);

function getVariables (grRITM){
    var variablePrefix='retail_kit_';
    for (i=1;i<10;i++){
        var kit_extra= get_variable(variablePrefix+i, grRITM);
                if (!gs.nil(kit_extra)){
                    //--do what ya gotta do...

function get_variable (variable_name, grRITM){

        //--retrieve a variable value where the variable name is passed in as a string
        var sReturn='';
        var varGR=new GlideRecord('sc_item_option_mtom');
        varGR.addQuery('request_item', grRITM.sys_id);   //--sys id of the sc_req_item
             var gr= new GlideRecord ('sc_item_option');
            gr.addQuery('sys_id', varGR.sc_item_option.toString());
                sReturn=gr.value; //--sys id, typically for this search           
        return sReturn;

Service Catalog: retrieve the catalog item sysid in a **server side** script:

Scenario: I need to obtain the catalog item sys id in the client callable script include, used to build up a reference qualifier for a catalog variable

requirement: this must be Service Portal AND classic ServiceNow compatible!


1) create a hidden string variable in which to store the sys id on the catalog item, and create a UI policy to hide the variable everywhere (under advanced on the UI policy, tick boxes next to cat item, request item, tasks)

2) populate the hidden variable in form load catalog client script:

//--set hidden field--store sys id for ref qualifiers (referenced in script include MAB_refqual_functions)
    //--Cat item id:
var itemID='';
   //--standard servicenow form
     itemID= gel('sysparm_id').value;
 }catch (ex){
        //--service portal friendly, need the catch ex to continue to here in SP
        itemID= g_form.getSysId();
 g_form.setValue('hidden_catitem_sysid',itemID );

3) Et Voila! in the client callable script include, you can access the variable like so:

var cat_item=current.variables.hidden_catitem_sysid;
 if (cat_item=='4c08fea3379d97803bef532e53990e03'){
            //--must be additional equipment item:
            //--must be lost or stolen item:

Email Notification scripting

notification email scripting:
some key features highlighted, such as using event parameters and calling email notification scripts

Dear ${},

Regarding your order number: ${number}

Request Short description: ${request.short_description}


Please note the order delivery date and engineer visit date:

delivery date:  ${event.parm1}

Engineer date: ${event.parm2}

Click here to view your request in ServiceNow: ${URI_REF}

Yours sincerely,

message HTML - pull in a notification email script:


notification email script:
- set background of variables section to 'LightGray' color using a div
- call a script include to pull in some variables, passing in the current RITM object

(function runMailScript(current, template, email, email_action, event) {
    //--pull in some variable values
    var si= new servicerequest_funcs();
    template.print('<div style="background-color:LightGray">');
    template.print('<b>Total cost:</b>' + current.variables.retail_grand_total);
})(current, template, email, email_action, event);

script include snippet:

retrieve_variables_email: function(ritm){
        //--build up a string of variable values for the email notification script to template.print out
        var sReturn='';
        sReturn=sReturn+ '<b>Equipment:</b><br/> ' + this._get_datalookup_name(ritm.variables.retail_kit) + ', quantity: ' + ritm.variables.retail_quantity;
        if (ritm.variables.retail_additional_flag=='true'){
            sReturn=sReturn+ '<b>Additional Equipment:</b><br/>';
            for (iCount=1;iCount<9;iCount++){
                var kit=this._get_variable('retail_kit_' + iCount, ritm);
                if (!gs.nil(kit)){
                    sReturn=sReturn + this._get_datalookup_name(kit) + ', quantity: ' + this._get_variable('retail_quantity_' + iCount, ritm) +  '<br/>';
        return sReturn;
    _get_variable: function(variable_name, grRITM){
        //--retrieve a variable value where the variable name is passed in as a string
        var sReturn='';
        var varGR=new GlideRecord('sc_item_option_mtom');
        varGR.addQuery('request_item', grRITM.sys_id);   //--sys id of the sc_req_item
            var gr= new GlideRecord ('sc_item_option');
            gr.addQuery('sys_id', varGR.sc_item_option.toString());
                sReturn=gr.value; //--sys id, typically for this search. or string value if not a ref field
        return sReturn;


Popular posts from this blog

GlideRecord setValue

variable advanced reference qualifier example

Running transform maps asynchronously