Why do I get an error in an Onchange Client script for Multi Row Variable Set when I parse the value

Community Alums
Not applicable

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: 

Rocio_1-1679336669308.png

 

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

5 REPLIES 5

Sal12
Tera Expert

@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:

 

Sal12_0-1679350074495.png

 

The variable Set is: 

Sal12_1-1679350238003.png

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.

 

Sal12_3-1679350762135.png

Output:

Upon changing the color of the variable, I get two alert messages, one after the other, as configured.

 

Sal12_4-1679350981919.pngSal12_5-1679351032764.png

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.

 

Sal12_6-1679351609432.png

Outputs:

 

Sal12_7-1679351687643.png

Sal12_8-1679351802230.png

Hope this helps.

Community Alums
Not applicable

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

Rocio_0-1679410442044.png

 

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:

 

Sal12_0-1679437065692.png

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.

Sal12_2-1679437349124.png

 

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.

Sal12_3-1679437444585.png

 

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:

 

Sal12_6-1679439527386.png

 

 

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.

Sal12_5-1679439391667.png

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.

 

Sal12_7-1679439635503.png

 

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.

 

 

AnveshKumar M
Tera Sage
Tera Sage

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

Thanks,
Anvesh