Skip to main content

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

URL link in addInfoMessage

var ga=new GlideAjax('gld_HR_ajax'); ga.addParam('sysparm_name', 'checkEmployeeNumber_hrProfile'); ga.addParam('sysparm_hrprofilenumber', g_form.getValue('number')); ga.addParam('sysparm_employeenumber', newValue); ga.getXMLAnswer(function(answer) { if (answer!='undefined' && answer!=''){ var navURL="<a style='text-decoration:underline;color:blue' href=hr_profile.do?sysparm_query=number=" + answer + ">" + answer + "</a><img width='3' src='images/s.gif'/>"; var sMsg='The employee number entered already exists on another HR Profile ' + navURL; //alert(sMsg); g_form.showErrorBox('employee_number', 'error - please check'); g_form.addInfoMessage(sMsg); } });

GlideRecord setValue

setValue(String name, Object value) Sets the specified field to the specified value. Normally a script would do a direct assignment, for example,  gr.category = value . However, if in a script the element name is a variable, then  gr.setValue(elementName, value)  can be used. When setting a value, ensure the data type of the field matches the data type of the value you enter. This method cannot be used on journal fields. If the value parameter is null, the record is not updated, and an error is not thrown https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=r_GlideRecord-setValue_String_Object