Dynamically add values to Multi row variable set.

Dharma5
Tera Contributor

Hi All,

I have a requirement of adding data to multi row variable set dynamically based on the input value in catalog item. If I select more than 1 input value, it should automatically add row to MRVS and insert the values into it.

Below is the onChange catalog client script which I wrote.

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('MRVS');
ga.addParam('sysparm_name', 'getCatalog');
ga.addParam('sysparm_catalog', g_form.getValue('catalog'));
ga.getXMLAnswer(response);

function response(answer) {
var ans = JSON.parse(answer);
var arr = [];
arr.push({
category: ans.category,
short_description: ans.shortDesc
});
g_form.setValue('catalog_details', JSON.stringify(arr));
}
}

Below is the script include:

getCatalog: function() {
var obj = {};
var catalog = this.getParameter('sysparm_catalog');
var cat = new GlideRecord('sc_cat_item');
cat.addQuery('sys_id', catalog);
cat.query();
if (cat.next()) {
obj.shortDesc = cat.getValue('short_description');
obj.category = cat.getValue('category');
}
return JSON.stringify(obj);
},

Using the above scripts I can able to insert only one row and will overwrite it if it is select more than once.

Any help on this would be appreciated! Thanks!

1 ACCEPTED SOLUTION

_ChrisHelming
Tera Guru

Your problem is in your response callback.

function response(answer) {
    var ans = JSON.parse(answer);
    var arr = [];
    arr.push({
        category: ans.category,
        short_description: ans.shortDesc
    });
    g_form.setValue('catalog_details', JSON.stringify(arr));
}

 

You're setting the entire MRVS to your response array `arr`, which overwrites any values that were already in there. What you need to do is get the current value of the variable using g_form.getValue() and then using a .push() on that array.

 

Perhaps something like 

function response(answer) {
    var ans = JSON.parse(answer);
    var arr = JSON.parse(g_form.getValue('catalog_details'));
    arr.push({
        category: ans.category,
        short_description: ans.shortDesc
    });
    g_form.setValue('catalog_details', JSON.stringify(arr));
}

View solution in original post

4 REPLIES 4

_ChrisHelming
Tera Guru

Your problem is in your response callback.

function response(answer) {
    var ans = JSON.parse(answer);
    var arr = [];
    arr.push({
        category: ans.category,
        short_description: ans.shortDesc
    });
    g_form.setValue('catalog_details', JSON.stringify(arr));
}

 

You're setting the entire MRVS to your response array `arr`, which overwrites any values that were already in there. What you need to do is get the current value of the variable using g_form.getValue() and then using a .push() on that array.

 

Perhaps something like 

function response(answer) {
    var ans = JSON.parse(answer);
    var arr = JSON.parse(g_form.getValue('catalog_details'));
    arr.push({
        category: ans.category,
        short_description: ans.shortDesc
    });
    g_form.setValue('catalog_details', JSON.stringify(arr));
}

Dharma5
Tera Contributor

Hi Chris,

Thanks for the response.

Above mentioned code is working in native UI.

But it was not working in the service portal. I'm getting the below error. 

Error - There is a java script error in your browser console.

Could you please help me on this.

Hey, make sure you use the reply button below the persons message so they know you replied to them.

 

Did you check your browser console for what the error was? What was the error message?

Murthy Ch
Giga Sage

Hi @Dharma

Can you please check my Article

Something similar I have written.

 

Thanks

Murthy

Thanks,
Murthy