Skip to main content

ServiceNow Code a service catalog variable to add/remove rows in table form

ServiceNow Code a service catalog variable to add/remove rows in table form 

can look something like this:

 

 

implemented using variable set:

 

 

example client script to load in values

function onLoad() { if (this.my_g_form) { var mrvs = this.my_g_form.getValue("u_cvp_room_new"); //internal name of mrvs //============================================================================================ //Add options to cvp_new_room based on the number of rooms requested (no_of_rooms_needed) //============================================================================================ var roomsRequired = this.my_g_form.getValue('no_of_rooms_needed'); if (roomsRequired) { for (i = 1; i <= roomsRequired; i++) { g_form.addOption('cvp_room_new_mrvs', 'Room ' + i, 'Room ' + i); } } //================================================================================================================ //Adjust cvp_room_new_mrvs options based on existing entries in mrvs to prevent adding more than 1 admin per room //================================================================================================================ if (mrvs) { var jsonObj = JSON.parse(mrvs); var arrMrvs = jsonObj.slice().sort(); var roomCounter = 0; while (roomCounter <= roomsRequired) { for (var loopCounter = 0; loopCounter < arrMrvs.length; loopCounter++) { var adminsPerRoom = arrMrvs.filter( function(arrMrvs) { return arrMrvs.cvp_room_new_mrvs == "Room " + roomCounter; } ); } if (adminsPerRoom.length >= 1) { g_form.removeOption('cvp_room_new_mrvs', 'Room ' + roomCounter); } roomCounter++; } } //====================================================== //Set mrvs max rows based on number of rooms requested //====================================================== var intMaxRows = parseInt(this.my_g_form.getValue('no_of_rooms_needed')) * 1; //1 is the max no of admins per room if (mrvs != null) { g_form.setValue('u_cvp_room_new', JSON.stringify([])); mrvs.max_rows_size = intMaxRows; } else { g_form.addErrorMessage("No multi-row variable set found"); } //====================================================== //Get variable values from item and set values in mrvs //====================================================== if (this.my_g_form.getValue("cvp_jurisdiction") == 'other') { g_form.setValue('cvp_jurisdiction_new_mrvs', this.my_g_form.getDisplayValue("cvp_jurisdiction_other")); } else { g_form.setValue('cvp_jurisdiction_new_mrvs', this.my_g_form.getDisplayValue("cvp_jurisdiction")); } if (this.my_g_form.getValue("cvp_tribunal") == 'other') { g_form.setValue('cvp_tribunal_new_mrvs', this.my_g_form.getDisplayValue("cvp_tribunal_other")); } else { g_form.setValue('cvp_tribunal_new_mrvs', this.my_g_form.getDisplayValue("cvp_tribunal")); } if (this.my_g_form.getValue("region") == 'other') { g_form.setValue('cvp_region_new_mrvs', this.my_g_form.getDisplayValue("region_other")); } else { g_form.setValue('cvp_region_new_mrvs', this.my_g_form.getDisplayValue("region")); } g_form.setValue('cvp_court_new_mrvs', this.my_g_form.getDisplayValue("court")); } //end of if (this.my_g_form) }


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 )){

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