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
Post a Comment