To add extra record in multirow variable set based on checkbox selection.

Amrutha K V
Tera Contributor

I have a multirow variable set named pack_size.

AmruthaKV_0-1779198660818.png

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:

AmruthaKV_1-1779199014980.png

AmruthaKV_2-1779199047159.png

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:

                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',
                rh01: 'false'

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;
}



5 REPLIES 5

Tanushree Maiti
Tera Patron

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.

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Ankur Bawiskar
Tera Patron

@Amrutha K V 

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! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

John Gilmore
Tera Guru

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.

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:

function onChange(control, oldValue, newValue, isLoading) {

    alert('onChange triggered');
    alert('Old Value: ' + oldValue);
    alert('New Value: ' + newValue);

    if (isLoading || newValue != 'true') {

        alert('Form is loading OR checkbox not checked');
        return;
    }

    alert('RH01 checkbox checked');

    // MRVS variable name
    var mrvsName = 'pack_size';

    alert('MRVS Name: ' + mrvsName);

    // Get MRVS value
    //var mrvsValue = g_form.getValue(mrvsName.toString());
    var mrvsValue = g_service_catalog.parent.getValue(mrvsName.toString());

    alert('MRVS Raw Value: ' + mrvsValue);

    // Convert to array
    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');
    }

    // Check duplicate
    var alreadyExists = rows.some(function(row) {

        return row.sales_organisation_addpack == 'SG01 - BT Singapore' &&
               row.distribution_channel_addpack == 'RH - Regional Hub' &&
               row.plant_addpack == 'RH01 - Regional Hub Direct' &&
               row.storage_location_addpack == '101 - RH Direct - GEN';
    });

    alert('Already Exists: ' + alreadyExists);

    // Add row if not exists
    if (!alreadyExists) {

        alert('Adding new RH01 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('New Row Added');
        alert('Updated Rows Count: ' + rows.length);

        // Update MRVS
        g_service_catalog.parent.setValue(
            mrvsName,
            JSON.stringify(rows)
        );

        alert('MRVS updated successfully');

    } else {

        alert('RH01 row already exists. No new row added.');
    }
}