Why do I get an error in an Onchange Client script for Multi Row Variable Set when I parse the value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2023 11:28 AM
Hello! I created an OnChange Catalog Client Script and I get an error when I parse the Values from the Multi Row Variable set. This is the configuration of my Catalog Client Script:
And this is the code that I'm getting an Error.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
var multiRowVariableSet = JSON.parse(g_form.getValue('multiRow_variableSetTest'));
}
And the error is [SCRIPT:EXEC] Error while running Client Script "ONCHANGE - Test": SyntaxError: Unexpected end of JSON input
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2023 03:38 PM - edited 03-20-2023 03:38 PM
@Community Alums
To get the values from the Variables of the Variable Set using an On Change Catalog client script, that Applies to A Variable set, I did the following to achieve that, using the provided out of the box configurations.
If that's what you'd need in the process, then this shall help you with that.
Part - 1:
This is the RITM record that I created:
The variable Set is:
This is the Catalog Client Script that I used is. Note this does not need JSON.parse() method.
You'd just need to use g_form.getValue() method.
Output:
Upon changing the color of the variable, I get two alert messages, one after the other, as configured.
So essentially, I get the values of the variables.
Part -2:
Note, that to use the JSON.parse() method, I used On Change Client Script, but this Applies to A Catalog Item.
Outputs:
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2023 07:54 AM
Hello! Thank you for your answer It work but It only brings one value, I would need the value of the column of all the rows. Do you know how I could bring it? In your example, It would be 64GB AND 128GB at the same time
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2023 04:10 PM
Hey @Community Alums
If you'd like to need all the values of a particular column, for example, as you mentioned, you'd need 64GB AND 128GB at the same time, this is possible.
Method 1: (I'd prefer this method)
I'd suggest using the On Change Client Script, when this Applies to A Catalog Item.
Using the Applies to A Catalog Item, will give you the values of a particular column in the Variable Set at your disposal for you to use, to be used only when you are changing the values of fields (like Department or Business Justification), and not the fields of the Variable Set itself
Below is the method for the same. I have used on Change of Business Justification for example.
On Change Client Script, when this Applies to A Catalog Item below:
The script in the above picture, written below:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var mrvs = g_form.getValue("mobile_devices_set");
alert(mrvs);
var mrvsObj = JSON.parse(g_form.getValue("mobile_devices_set"));
var storageArray = []; //An array to store the storage values of all the phones.
//Loop through all the rows of the storage column
for (var i = 0, len = mrvsObj.length; i < len; i++) {
storageArray.push(mrvsObj[i].storage);
}
//Output the array,containig the storage values of all the phones.
alert(storageArray);
}
Output:
This is the alert message from line number 6.
This is storageArray values from line number 17. This is a comma separated array. So, you can use these values as you'd like to, per your need.
Method 2:
When I use the On Change Catalog client script, that Applies to A Variable set,
then the storage values will be given to me, each time the color is changed. This also makes sense, since I am using the On Change Catalog client script, that Applies to A Variable set. So, for each individual change in the values of the variable color, each time, I will get the storage values for that row only.
Here's what I mean:
On Change Client Script, when this Applies to A Variable set below:
The script in the above picture, written below:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Retrieve the value of phone storage.
//To get the values of storage of all the phones, you'd need to change each row's color, each time
var storage = g_form.getValue("storage");
alert("Phone storage = " + storage);
}
If I need the value of the Storage for the first row, then I will edit the color of the first row.
I will get the storage value of the first row (which is 64GB)
Next time, if I'd need the value of the Storage for the second row, then I will edit the color of the second row.
I will get the storage value of the second row (which is 128GB)
Conclusion:
It really depends on the requirements/functionalities. As I shared, the above 2 methods would help you to achieve, according to each scenario.
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2023 03:49 PM
Hi @Community Alums ,
Try using the below modified script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
var multiRowVariableSet = JSON.parse(g_form.getValue('IO:<Sys ID of multi row variable set>));
}
Instead of name (internal name) of the variable set, pass 'IO:<Sys_ID of multi row variable set>' in g_form.getValue() method.which will return the stringified version of the variable set inputs, as you are using JSON.parse() it will return the object.
Thanks,
Anvesh
Anvesh