Add data in new rows in MRVS using on change client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2023 06:55 AM
I am using a MRVS in which user inputs are recorded. There is a choice field on form named "New portfolio for project".
On change of this field whatever user selects that record should get added up in MRVS. When he selects the value second time, that value should be added up in MRVS in second row and so on.
I have written an onchange client script but it is partially working. When I select the value second time, it gets overwritten in MRVS but it should actually add up in second row.
Any thoughts how we can achieve this ?
Regards,
Shraddha Madye

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2023 07:25 AM
Hi there,
Please share what you tried and is partially working.
Kind regards,
Mark
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2023 12:15 PM
The script that I shared above. When I select a new value for the second time, the onchange CS works, but first row gets overwritten in MRVS with new selected value. I just need to add the second selection in second row.
Form variable = 4 variables [on change is written on 1 amongst these 4 > on change of that 1 field all 4 inputs should get stored in MRVS's 4 columns]
MRVS contains 4 variables.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2023 09:57 AM
On Change Client Script :
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga1 = new GlideAjax('global.itbmDataPatchCatalogUtils');
ga1.addParam('sysparm_name', 'populateMRVS1');
ga1.addParam('p_selected_module', g_form.getDisplayValue('select_a_module'));
ga1.addParam('p_project_number', g_form.getDisplayValue('select_a_project'));
ga1.addParam('p_project_field', g_form.getDisplayValue('select_a_field_from_project'));
ga1.addParam('p_project_portfolio_field_value', g_form.getDisplayValue('new_project_portfolio'));
ga1.getXML(parseData);
function parseData(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
g_form.setValue('data_for_update', answer);
// var mvrs = JSON.parse(g_form.getValue('data_for_update'));
// if (mrvs != '') {
// g_form.setValue('data_for_update', answer);
// } else {
// for (var i = 0; i <= 10; i++) {
// var tempObj = {};
// tempObj.answer = i + 4;
// mvrs.push(answer);
// }
// g_form.setValue('data_for_update', JSON.stringify(mvrs));
// }
}
}
Script Include :
var itbmDataPatchCatalogUtils = Class.create();
itbmDataPatchCatalogUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
populateMRVS1: function() {
var dataArr = [];
var p1 = this.getParameter('p_selected_module');
var p2 = this.getParameter('p_project_number');
var p3 = this.getParameter('p_project_field');
var p4 = this.getParameter('p_project_portfolio_field_value');
dataArr.push({
"itbm_module": p1.toString(),
"record": p2.toString(),
"field_name": p3.toString(),
"new_value": p4.toString()
});
return JSON.stringify(dataArr);
},
type: 'itbmDataPatchCatalogUtils'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2023 10:29 AM
The name of your MRVS is "data_for_update", is that correct? If so, indeed you are just overriding the value, just like it would with any other normal variable.
I think the nicest way would be to have pass your current MRVS row(s) to the Script Include, push a new row, and the new JSON object will than contain your current MRVS row(s) + the new one.
Kind regards,
Mark
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field