How to add rows to a multi row variable set dynamically

mballinger
Mega Guru

Hello,

I have a requirement to add rows to a multirow variable set based off of a selected value from another variable. The dropdown will be numbered 1 through 5. If 4 is selected, 4 single line text rows get inserted. The same applied to all the other selections.

Thanks

 

1 ACCEPTED SOLUTION

Let's take it down a level; this this: 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var mvrs = [];

    for (var i = 0; i < newValue; i++) {
        var tempObj = {};
        tempObj.test_report_number = '';
        mvrs.push(tempObj);
    }
    g_form.setValue('multi_row_variable_set_test', JSON.stringify(mvrs));

}

find_real_file.png

I hope this helps!

If this was helpful or correct, please be kind and remember to click appropriately!

Michael Jones - Proud member of the CloudPires team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

View solution in original post

11 REPLIES 11

Michael Jones -
Giga Sage

This article gives you the basic idea. You need to create a JSON Object and set the value to be the number of rows you want. You can get a good idea of how this works by manually going in and setting up the MVRS the way you would want it to be, and then use the javascript console to alert the g_form.getValue('mvrs_internal_name') to see what the object looks like, and the work from there. 

https://community.servicenow.com/community?id=community_article&sys_id=a9c7454ddb6880d4414eeeb5ca961...

I hope this helps!

If this was helpful or correct, please be kind and remember to click appropriately!

Michael Jones - Proud member of the CloudPires team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Just to add, you can basically use what's described in the article I wrote which Michael shared. Principle will be the same, only using a onChange Client Script instead of the example with onLoad.

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Yes that's what I was thinking, I would need onchange otherwise all other data in form would be lost. So would I create multiple objects?

Maybe to clarify your requirement, are you needing to add these rows after data has already been added to the MVRS, or are you in some way trying to "build" the values in the MVRS based on multiple field inputs on the form?

If you need to preserve the data that is in the MVRS, then you would need to first pull in the current value of the object, modify the object by adding values, and then set the value of your MVRS to the new object. 

So, this is kind of a simple example but if I have an MVRS named MyNewMVRS with a single variable named singlelinetext, with a value of 4 rows with the values 1, 2, 3, 4 and I want to add 4 more rows with values 5, 6, 7, 8.

alert(g_form.getValue('MyNewMVRS'))
// output is [{"singlelinetext":"1"},{"singlelinetext":"2"},{"singlelinetext":"3"},{"singlelinetext":"4"}]
var mvrs = JSON.parse(g_form.getValue('MyNewMVRS'));

for(var i=0; i <=4; i++) {
var tempObj = {};
tempObj.singlelinetext = i + 4
mvrs.push(tempObj)
}
g_form.setValue('MyNewMVRS', JSON.stringify(mvrs));

//new value is [{"singlelinetext":"1"},{"singlelinetext":"2"},{"singlelinetext":"3"},{"singlelinetext":"4"},{"singlelinetext":4},{"singlelinetext":5},{"singlelinetext":6},{"singlelinetext":7},{"singlelinetext":8}]

I hope this helps!

If this was helpful or correct, please be kind and remember to click appropriately!

Michael Jones - Proud member of the CloudPires team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!