- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2019 06:41 AM
Hi all,
I am trying to to add rows to a Multi-row Variable Set in a Catalog Item from a UI Page client script before submission of the form.
The GlideForm setValue method will only set the first row in the Multi-row variable set and will overwrite it if it is called more than once.
function continueOK(){
var selectbox = document.getElementById("quick_message_dropdown").value;
var slushbucket = slush.getValues(slush.getRightSelect());
var obj = [{
"primary": selectbox,
"sub": slushbucket
}];
g_form.setValue("varset", JSON.stringify(obj));
}
Iterating over the obj variable will allow me to add multiple rows, but will overwrite every row in the Multi-Row Variable Set. Also, setting the values from an array is not easy to work with, since the array is lengthened each time the function is called. This means that if I delete a row in the Multi-row variable set, but call the function again, the deleted row will be re-added. Ideally, the GlideForm setValue method could work to add a row instead of overwriting the first row.
//Global Variable
var selectedArray = [];
//Called when the 'OK' button gets clicked
function continueOK(){
//Get the selected values from the right slushbucket
var selectbox = document.getElementById("quick_message_dropdown").value;
var slushbucket = slush.getValues(slush.getRightSelect());
//Push to global variable selectedArray
selectedArray.push([selectbox,slushbucket]);
var obj = [];
for (var i = 0; i < selectedArray.length; i++){
obj.push({
"selectbox": selectedArray[i][0],
"slushbucket": selectedArray[i][1]
});
}
g_form.setValue("varset", JSON.stringify(obj));
I have read the ServiceNow documentation on "Scriptable service catalog variables", Brad Tilton's blog post on "Scripting the Multi-row Variable Set", and hrng's article on "Accessing the Multi-row Variable Set" but can't seem to reference the variable set this way from a client script on an unsubmitted form.
current.variables.<variable_name>
current.variables.<variable_set_name>.<variable_name>
Any help with this would be greatly appreciated!!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2019 09:23 AM
Here is a link to my video where I show you how to add rows with client script and also how to preserve the rows that already might be there.
//Göran

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2019 06:27 PM
This is probably a little off-topic, but I could not get defaults set for a multi-row variable, so I turned to a little angular in a widget.
My problem would have been greatly simplified if I could have disabled "isolate script" but for some bizarre reason (nothing to do with scope), it just refused to work from a catalog client script. Even console.log(window) failed. Anyway...
Exasperated, I used a catalog variable type of macro to include a really simple widget with this code.
function() {
/* widget controller */
var c = this;
setTimeout(function(){
var myScope = window.angular.element(document.body).scope();
var varSet = myScope.containers[1].rows[0].columns[0].widgets[0].widget.data.sc_cat_item._fields.myMultiRowVariableSet;
if(!varSet.hasOwnProperty('displayValue')){
varSet.displayValue=[];
varSet.value=[];
varSet.displayValue.push({user:myScope.user.name, mobile_phone:myScope.user.mobile_phone, additional_information:''});
varSet.value.push( {user:myScope.user.sys_id, mobile_phone:myScope.user.mobile_phone, additional_information:''});
varSet._displayValue.push({user:myScope.user.name, mobile_phone:myScope.user.mobile_phone, additional_information:''});
varSet._value.push( {user:myScope.user.sys_id,mobile_phone:myScope.user.mobile_phone, additional_information:''});
console.log('%cDefaults added to multi row variable set','color:red');
}
},500);
}
This allowed me to set up the first row in a multi row variable set with default values for the user opening the catalog item.
To modify this, simply replace the name of the variable set and the variables within that set so they match your multi-row variable set (and include some console logging if you get stuck so you can see what's happening). There is a slight 'danger' in that some instances might have the catalog widget in a different container, row, column, etc, so you may need to adjust that accordingly.
I hope this helps someone else