Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

MVRS on a Record Producer

Alon Grod
Tera Expert

Hi,

I created a new record producer that generate record in sn_slm_task table.

Inside this record producer i added a Multi Row Variable Sets. 
Is there any way to access this MVRS values using the producer script on the sc_cat_item_producer record?

using this script:

AlonGrod_0-1787818625855.png

 

1 ACCEPTED SOLUTION

Yaswanth_M000
Tera Expert

Hi @Alon Grod 

You Can Parse the Variable , 

 

Lets Say for Example 

if variable name is requested_items and it has name and Quantity

 

The Script Can be Similar as 

 

var mrvs = producer.requested_items;

if (mrvs)

{

    var rows = JSON.parse(mrvs);

    for (var i = 0; i < rows.length; i++)

    {

          gs.info('Item: ' + rows[i].item_name); 

          gs.info('Quantity: ' + rows[i].quantity);

    } 

}

 

Output can be similar as 

[
    {
        "item_name": "Laptop",
        "quantity": "2",
        "description": "Developer laptop"
    },
    {
        "item_name": "Monitor",
        "quantity": "3",
        "description": "External monitors"
    }
]

View solution in original post

4 REPLIES 4

Yaswanth_M000
Tera Expert

Hi @Alon Grod 

You Can Parse the Variable , 

 

Lets Say for Example 

if variable name is requested_items and it has name and Quantity

 

The Script Can be Similar as 

 

var mrvs = producer.requested_items;

if (mrvs)

{

    var rows = JSON.parse(mrvs);

    for (var i = 0; i < rows.length; i++)

    {

          gs.info('Item: ' + rows[i].item_name); 

          gs.info('Quantity: ' + rows[i].quantity);

    } 

}

 

Output can be similar as 

[
    {
        "item_name": "Laptop",
        "quantity": "2",
        "description": "Developer laptop"
    },
    {
        "item_name": "Monitor",
        "quantity": "3",
        "description": "External monitors"
    }
]

Sujit Jadhav
Tera Guru

Hello @Alon Grod ,

No, this specific dynamic loop will not read individual MRVS rows directly, but it will return the raw MRVS string if it encounters the MRVS internal name.
In your current script, var val = producer[variableName]; fetches standard variables perfectly. However, for a Multi-Row Variable Set (MRVS), that same line will return a stringified JSON array.
To cleanly handle the MRVS inside or alongside your current dynamic setup, you can check if the variable is an MRVS type or parse it when found.

Use this script :

checkRedirect();

function checkRedirect() {
// Your existing redirect logic
}

var bb = new GlideRecord('item_option_new');
bb.addEncodedQuery('cat_item=4016a1a1...^name!=hash'); // Your existing query
bb.query();

while (bb.next()) {
var variableName = bb.getValue('name');
var val = producer[variableName];

if (val) {
// 1. CHECK IF THE VARIABLE IS A STRINGIFIED MRVS OBJECT
if (typeof val === 'string' && val.indexOf('[{"') === 0) {
try {
var mrvsArray = JSON.parse(val);

// Loop through the MRVS rows
for (var i = 0; i < mrvsArray.length; i++) {
var mrvsRow = mrvsArray[i];

// Access individual columns within the MRVS row
// var rowValue = mrvsRow.your_mrvs_column_name;
}
} catch (e) {
gs.error("Failed to parse MRVS: " + e.message);
}
} else {
// 2. STANDARD VARIABLE LOGIC (Your existing mapping code)
/*
var gr = new GlideRecord('question_answer');
...
*/
}
}
}


"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"


Thank You,

Sujit Jadhav



Ankur Bawiskar
Tera Patron

@Alon Grod 

what's your actual requirement?

in record producer script you can access that MRVS using this syntax and it will give you JSON string

producer.mrvsVariableSetName

💡 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

s_p_naidu
Mega Guru

Hi @Alon Grod,

 

For Example, there is multi row variable set named Task Details mrvs (internal name: task_details_mrvs)

s_p_naidu_0-1787823833767.png

 

 

In your Record Producer script you can get your array of variable set variable values for each row.

Parse and loop through each row, get each variable value for that row with it’s name.
Iam setting the description of incident with each row variables.

s_p_naidu_1-1787823833777.png

 


Script:

(function() {

   

    var mrvsRaw = producer.task_details_mrvs;//give your multi row variable set internal name

 

   

        // Parse the JSON string into a JavaScript array

        var rows = JSON.parse(mrvsRaw);

        var formattedOutput = '';

 

        //Loop through the array rows

        for (var i = 0; i < rows.length; i++) {

            var row = rows[i];

            formattedOutput += 'Row ' + (i + 1) + ':\n';

            formattedOutput += '  Short Description: ' + row.task_short_desc + '\n';

            formattedOutput += '  Description: ' + row.task_desc + '\n\n';

        }

 

        //Set fields on the incident record

        current.short_description = 'Incident created via MRVS';

        current.description = formattedOutput;

   

})();

 

s_p_naidu_2-1787823833781.png

 

I created two rows with sample values and after submission , I get the each row values in the description as shown below.

 

s_p_naidu_3-1787823833786.png