Skip to main content

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';

 }

Comments

Popular posts from this blog

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

ServiceNow - script include: build up the results as a JSON object (array) - example 2

function call var sCurrentGroupMembers = new <script_include> (). getGroupMembers ( group_sysid ); gs . print ( 'sCurrentGroupMembers:' + sCurrentGroupMembers ); var oGroupMembers = JSON . parse ( sCurrentGroupMembers ); gs . print ( JSON . stringify ( oGroupMembers . users )); oNewGroupMember = JSON . parse ( group_members ); var oGroupConfig = getGroupDeltas ( oGroupMembers . users , oNewGroupMember ); gs . print ( JSON . stringify ( oGroupConfig . remove )); gs . print ( JSON . stringify ( oGroupConfig . add )) output : *** Script: sCurrentGroupMembers: {"manager":"3b2649efdb0f8c10cc0652f3f39xxxxx", "email":"", "type":"33a2226edb99c340edfc7cbdae96xxxx", "description":"Created for xyz", "users":[{"user":"3b2649efdb0f8c10cc0652f3f396xxxx"},{"user":"856a8f71db3d73041b4ffc45ae96196a"},{"user":"3752771