Copy multi row variable set to another multi row variable set

madanm7786
Mega Guru

Hi All,

In a order guide i have 3 catalog items, and each item is having multi row variable set. When order guide is created, i have copy all these multi row variable set to 1 multi row variable set.

Script i used is copying only 1 multi row variable set and other 2 is not copying.

Any help please.

var sourceRitmGR = new GlideRecord('sc_req_item');
 sourceRitmGR.addEncodedQuery('sys_idIN3f58c09c5353221008b9d2a0a0490ee1,6c88c49c5353221008b9d2a0a0490e81,4598c0934553221118b9d2a0a0491edf');
    sourceRitmGR.query();
    while(sourceRitmGR.next())
    {
        var sourceMultiRow = JSON.parse(sourceRitmGR.variables.employee_details);
        current.variables.employee_details = JSON.stringify(sourceMultiRow);
        current.update();
    }
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@madanm7786 

you need to merge the JSON and then add

So update as this

 var mergedData = [];
 var sourceRitmGR = new GlideRecord('sc_req_item');
 sourceRitmGR.addEncodedQuery('sys_idIN3f58c09c5353221008b9d2a0a0490ee1,6c88c49c5353221008b9d2a0a0490e81,4598c0934553221118b9d2a0a0491edf');
 sourceRitmGR.query();
 while (sourceRitmGR.next()) {
     var mrvsData = JSON.parse(sourceRitmGR.variables.employee_details);
     mergedData = mergedData.concat(mrvsData);
 }
 current.variables.employee_details = JSON.stringify(mergedData);
 current.update();

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

3 REPLIES 3

jameswood32
Tera Contributor

Copying one multi-row variable set (MRVS) to another directly isn’t straightforward because MRVS data is stored as child records linked to the parent record (like a record producer, catalog item, or task). However, you can achieve this by iterating over the rows of the source MRVS and programmatically creating equivalent rows in the target MRVS.

Here’s a general approach:

 

Identify the Parent Records:
Determine the record(s) where the source and target MRVS are attached (e.g., a Catalog Item or a specific record).

Query the Source MRVS Rows:
Use a GlideRecord query on the sc_item_option_mtom table (or relevant table depending on platform) to get all rows related to the source MRVS.

Create Rows in the Target MRVS:
For each source row, create a new row for the target MRVS, copying the relevant field values.


Example Script (ServiceNow):


// Assume 'current' is the parent record from which you want to copy MRVS

// sys_id of source MRVS variable (the multi-row variable set you want to copy)
var sourceMRVSVariableSysId = 'source_variable_sys_id_here';

// sys_id of target MRVS variable (the multi-row variable set you want to copy to)
var targetMRVSVariableSysId = 'target_variable_sys_id_here';

// GlideRecord for multi-row variable set (sc_item_option_mtom)
var grSource = new GlideRecord('sc_item_option_mtom');
grSource.addQuery('request_item', current.sys_id); // or appropriate parent field
grSource.addQuery('sc_item_option', sourceMRVSVariableSysId);
grSource.query();

while (grSource.next()) {
var grTarget = new GlideRecord('sc_item_option_mtom');
grTarget.initialize();

// Copy parent record
grTarget.request_item = grSource.request_item;

// Assign the target MRVS variable sys_id
grTarget.sc_item_option = targetMRVSVariableSysId;

// Copy the value from the source MRVS row to the target MRVS row
grTarget.value = grSource.value;

// Insert the new row
grTarget.insert();
}

 

Key Points:
Replace sourceMRVSVariableSysId and targetMRVSVariableSysId with the actual sys_ids of your MRVS variables.

The table sc_item_option_mtom stores the multi-row variable data in ServiceNow.

This script should be adapted based on your specific platform and variable set context.

Always test in a sub-production environment to avoid data loss.

 

If you’re working in a different system or need help with a particular platform, please share more details! Happy to help tailor the solution.

Ankur Bawiskar
Tera Patron
Tera Patron

@madanm7786 

you need to merge the JSON and then add

So update as this

 var mergedData = [];
 var sourceRitmGR = new GlideRecord('sc_req_item');
 sourceRitmGR.addEncodedQuery('sys_idIN3f58c09c5353221008b9d2a0a0490ee1,6c88c49c5353221008b9d2a0a0490e81,4598c0934553221118b9d2a0a0491edf');
 sourceRitmGR.query();
 while (sourceRitmGR.next()) {
     var mrvsData = JSON.parse(sourceRitmGR.variables.employee_details);
     mergedData = mergedData.concat(mrvsData);
 }
 current.variables.employee_details = JSON.stringify(mergedData);
 current.update();

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Thanks Ankur.... It worked.