Skip to main content

Posts

Showing posts from April, 2018

Check if a column exists on a table in a server script

var grDict = new GlideRecord ( 'sys_dictionary' ); grDict . addQuery ( 'name' , 'cmdb_ci_computer' ); grDict . addQuery ( 'element' , ' cd_rom ' ); grDict . query (); gs . print ( grDict . getRowCount ()); //--column exists, output is 1 var grDict = new GlideRecord ( 'sys_dictionary' ); grDict . addQuery ( 'name' , 'cmdb_ci_computer' ); grDict . addQuery ( 'element' , ' cd_romZ ' ); grDict . query (); gs . print ( grDict . getRowCount ()); //--column does not exist, output is 0

Check if a table exists in a server-side script

var  gr  =   new  GlideRecord ( 'incident' ); gs . info ( gr . isValid ()); var  anotherGr  =   new  GlideRecord ( 'incidentZ' ); gs . info ( anotherGr . isValid ()); https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_ScopedGlideRecordIsValid  see  https://docs.servicenow.com/bundle/kingston-application-development/page/app-store/dev_portal/API_reference/TableUtils/concept/c_TableUtilsAPI.html another option: var  table  =   new  TableUtils ( " cmdb_ci_computer " ); gs . print ( "Does 'my_table' exist? "   +  table . tableExists ());   //--returns true var  table  =   new  TableUtils ( " cmdb_ci_computerZ " ); gs . print ( "Does 'my_table' exist? "   +  table . tableExists ());   //--returns false

Catalog Client Script: how to determine if loaded in Service Portal or Platform UI?

Basic principle is to use the URL via  top . location  which will be different in service portal: function onLoad () {     //shared functions for form load     var urlString = top . location . toString ();     var bPortal = false ; //--loaded from Service Portal?     if ( urlString . indexOf ( 'rp?id=sc_cat_item&sys_id=' )>- 1 ){         bPortal = true ;     }         //--set hidden field--store sys id for ref qualifiers (referenced in script include MAB_refqual_functions)     //--Cat item id:     var itemID = '' ;     try {         if ( bPortal ){             itemID = g_form . getSysId (); //--service portal friendly         } else {             //--standard servicenow form             itemID = gel ( 'sysparm_id' ). value ;         }     } catch ( ex ){           //--         alert ( 'An error has occurred in the form load ' + ex . toString ());     }     g_form . setValue ( 'hidden

getXMLWait() does not work in Service Portal

Aw shucks! I found out the hard way...but synchronous calls being disallowed in SP kinda makes sense note: getXMLWait() does not work in Service Portal, use getXMLAnswer() It seems others have noticed too    (  https://community.servicenow.com/community?id=community_question&sys_id=c2f5c361db1cdbc01dcaf3231f96190c  )

Service Catalog: spit out variable values

in an email notification script for example <mail_script> var item = new GlideRecord ( "sc_req_item" );   item . addQuery ( "sys_id" , 'f5a6ff1b37a993c08ca1138943990ee6' );   item . query ();   while ( item . next ()) {           gs . print ( item . number + ":   " + item . cat_item . getDisplayValue () + " \n" );       gs . print ( "     Options:\n" );       var keys = new Array ();       var set = new GlideappVariablePoolQuestionSet ();         set . setRequestID ( item . sys_id );       set . load ();       var vs = set . getFlatQuestions ();       for ( var i = 0 ; i < vs . size (); i ++) {          var sLabel = vs . get ( i ). getLabel ();           if ( sLabel != '' && sLabel . indexOf ( 'hidden' )< 0 ) { //--RDS Apr2018 hide hidden variables                var displVal = vs . get ( i ). getDisplayValue () . trim ();   

Email scripting - catalog variables and styling

today, some email notification scripting fun (in this case on the sc_req_item table) 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_ac