To add extra record in multirow variable set based on checkbox selection.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
I have a multirow variable set named pack_size.
when user add value in multirow variable set without clicking rho1 checkbox and clicks Add button. That selected record will gets added in the multirow variable set. Eg:
If user add values and selects rho1 , and clicks Add , we should get two records. One should be the user input record and another record should contains:
How can we implement this.
I tried with OnSubmit script in the multirow variable set, but SG01 - BT Singapore record is not getting inserted.
Sharing with the OnSubmit script :
function onSubmit() {
alert('onSubmit triggered');
// RH01 checkbox checked?
var rh01 = g_form.getValue('rho1');
alert('RH01 Value: ' + rh01);
if (rh01 != 'true') {
alert('RH01 not checked');
return true;
}
alert('RH01 checked');
// Parent MRVS name
var mrvsName = 'pack_size';
alert('MRVS Name: ' + mrvsName);
// Parent form
var parentForm = g_service_catalog.parent;
alert('Parent form accessed');
// Existing MRVS rows
var mrvsValue = parentForm.getValue(mrvsName);
alert('Existing MRVS Raw Value: ' + mrvsValue);
var rows = [];
if (mrvsValue) {
try {
rows = JSON.parse(mrvsValue);
alert('JSON parsed successfully');
alert('Existing Rows Count: ' + rows.length);
} catch (e) {
alert('JSON Parse Error: ' + e);
rows = [];
}
} else {
alert('MRVS is empty');
}
// Add SG01 row
rows.push({
sales_organisation_addpack: 'SG01 - BT Singapore',
distribution_channel_addpack: 'RH - Regional Hub',
plant_addpack: 'RH01 - Regional Hub Direct',
storage_location_addpack: '101 - RH Direct - GEN'
});
alert('SG01 row added');
alert('Updated Rows Count: ' + rows.length);
// Update MRVS
parentForm.setValue(
mrvsName,
JSON.stringify(rows)
);
alert('MRVS updated successfully');
return true;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Amrutha K V ,
It is not recommended to create two records with a single click — one using the input value and another using the backend value.
For a better user experience, the end user should enter the value from the frontend, and the record should be created based on that input.
It would be better to ask the user to click the Add button in the MRVS once again if another record needs to be added.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
you need to detect the add/remove button if clicked and then accordingly handle setting the MRVS
How to calculate the total of variables into another variable from a multi-row variable set?
Add the total cost in multirow variable set
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
While I agree with @Tanushree Maiti that it feels like the approach is not ideal for the situation I believe I can provide a couple insights into how to achieve it.
Specific to your script, because you are running as an onSubmit g_service_catalog.parent.getValue(mrvsName) does not contain the row you are adding as part of the action so it is effectively missing. Functionality is inconsistent using the pattern in your script and is likely creating a situation where you update the parent MRVS with your script but that change is then overridden and wiped by the actual process that adds the new row. This is because the way MRVS works it re-writes the entire value for all operations and the original process to add the row is using the same version of the parent MRVS value as your onSubmit and does not update.
This would work much better in an onChange script because that would trigger when the row gets added. Then setup your check on the RH01 trigger to look at just the last row of the MRVS and if true, then perform the processing to add the new row. Edit: Just be careful, if the onChange adds a row with RH01=true then it will create an infinite loop.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi John,
We already tried with onchange of rh01 catalog client script in the multi row variable set level. But SG01 record was not getting inserted if user selects RH01. Thats why tried with OnSubmit script.
Onchange Catalog Client script: