Skip to main content

Server Side

Remain on the current form


action.setRedirectURL(current);


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);
       }
       grGrp.query();
       if (grGrp.next()){
           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+='<field>';
                       xmlStr+='<tableColumn>' + edStr + '</tableColumn>';
                       xmlStr+='<label>' + ed.getLabel() + '</label>';
                       xmlStr+='<value>' + grGrp[edStr].getDisplayValue()+ '</value>';
                       xmlStr+='</field>';
                   }
               }
           }
       }
       xmlStr+='</fields></xml>';
       return xmlStr;
   },

Prevent business rules from running in a script

current.setWorkflow(false);


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
retail_kit_1
retail_kit_2
retail_kit_3
...

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)){
                    gs.print(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
                varGR.addQuery('sc_item_option.item_option_new.name='+variable_name);
        varGR.query();
               
        if(varGR.next()){
             var gr= new GlideRecord ('sc_item_option');
            gr.addQuery('sys_id', varGR.sc_item_option.toString());
            gr.query();
                       
            if(gr.next()){
                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!

Solution:

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='';
 try{
   //--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:
            sQuery=sQuery+'^u_order_type!=replacement';
 }else{
            //--must be lost or stolen item:
            sQuery=sQuery+'^u_order_type!=additional';
 }



Email Notification scripting

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

Dear ${request.requested_for.name},


Regarding your order number: ${number}

Request Short description: ${request.short_description}

${mail_script:retail_sc_req_item_variables}

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:


${mail_script:retail_sc_req_item_variables}



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(si.retrieve_variables_email(current));
    template.print('<br/>');
    template.print('<b>Total cost:</b>' + current.variables.retail_grand_total);
    template.print('</div>');
   
})(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;
        sReturn=sReturn+'<br/>';
        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
    varGR.addQuery('sc_item_option.item_option_new.name='+variable_name);
        varGR.query();
        if(varGR.next()){
            var gr= new GlideRecord ('sc_item_option');
            gr.addQuery('sys_id', varGR.sc_item_option.toString());
            gr.query();
            if(gr.next()){
                sReturn=gr.value; //--sys id, typically for this search. or string value if not a ref field
            }
        }
        return sReturn;
    }





Comments

Popular posts from this blog

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){

Service Catalog: variable advanced reference qualifiers

Call a script include to apply a reference qualifier on a catalog item variable: - variable reference qualifier dependent on another variable selection, in this case a variable referencing sys_user (requested_for) On the catalog item form. variable name to apply ref qual filter : retail_equipment variable reference qualifier (on cmdb table ): javascript : new  refqual_functions (). lostStolen_getAssignedCIs (); client-callable script include ( refqual_functions)  function : lostStolen_getAssignedCIs : function (){         //--called from variable set client script, for lost/stolen request (service catalog)     gs . log ( current . variables . requested_for , 'retail_lostStolen_getAssignedCIs' );         return ( 'install_statusNOT IN8,7^owned_by=' + current . variables . requested_for );             //owned_by=1269b79937f1060041c5616043990e41^install_statusNOT IN8,7            },

Get URL Parameter - server side script (portal or classic UI)

Classic UI : var sURL_editparam = gs . action . getGlideURI (). getMap (). get ( ' sysparm_aparameter ' ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); } Portal : var sURL_editparam = $sp . getParameter ( " sysparm_aparameter " ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); }