Parsing through an mrvs

erin-marie
Tera Contributor

I am working on a workflow script to take a Multi-Row Variable Set from a catalog item.  Create an array to get the data so that I can transfer the display name of the field POAM to a CHG.  Everything is working great for creating the CHG from the catalog item, except for this one last piece.  The POAM field on the CHG form is a string field and I just need the POAM IDs, not the other information.

 

Here is the section of code.  

 

 // POAM START    
    var mrvspoam = current.variables['decommission_poam_details'];
    var mrvspoam_length = mrvspoam.getRowCount();
    var poam, poam_arr = [];
    gs.log('decom mrvspoam: ' + mrvspoam + ' | mrvspoam length: ' + mrvspoam_length); // debug

    // loop through poam's
    for (var i = 0; i < mrvspoam_length; i++) {
        poam = mrvspoam.getRow(i); // next poam listed
        gs.log('decom mrvs in loop poam: ' + poam + ' | poam ref: ' + poam.sc_decomm_poam_id);


        var gr = new GlideRecord('u_poam');
        if (gr.get(poam.sc_decomm_poam_id.toString())) {
            poam_arr.push(poam.getDisplayValue());
        }


    } // end for loop

    gs.log('decom poams len: ' + poam_arr.length + ' | dump: ' + poam_arr.join(','));

    // copy remaining poam's into the poam fields
    if (poam_arr.length > 1)
        change.u_poa_m_ids = poam_arr.join('\n');



    // POAM END
 
And here are the two logs created from my debugging.  The first array works fine, the second ione appears to as well and nothing is transferring to the CHG.
 
erinmarie_0-1758064376758.png

 

 
 
1 ACCEPTED SOLUTION

kaushal_snow
Mega Sage

Hi @erin-marie ,

 

use this updated code:

 

var poamArr = [];
var poamField = current.variables['decommission_poam_details'];
var rowCount = poamField.getRowCount();

// Iterate through each row in the MRVS
for (var i = 0; i < rowCount; i++) {
    var row = poamField.getRow(i);
    var poamId = row.sc_decomm_poam_id;

    // Check if POAM ID exists
    if (poamId) {
        var gr = new GlideRecord('u_poam');
        if (gr.get(poamId)) {
            poamArr.push(gr.getDisplayValue()); // Add display value to array
        }
    }
}

// Assign to CHG field if array has values
if (poamArr.length > 0) {
    change.u_poa_m_ids = poamArr.join('\n');
}

 

To extract POAM IDs from a Multi Row Variable Set and transfer them to a Change Request (CHG) in ServiceNow, initialize an empty array to store the POAM display values, iterate through each row in the MRVS to retrieve the POAM ID, query the u_poam table using GlideRecord to fetch the corresponding record, extract the display value, and append it to the array; finally, assign the concatenated display values to the u_poa_m_ids field in the CHG, separated by newlines.....

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Hello,

Your poam_arr is showing empty in the log so you need to look at the GlideRecord.  Is your u_poam table in the Global or same scope as the Catalog Item? When pushing the value to the poam_arr I think you meant:

poam_arr.push(gr.getDisplayValue());

If this doesn't show values in the third / poam_arr log, add a log within the if block of the GR to make sure a record is found.  Once you've confirmed this, try something more like:   

poam_arr.push(gr.getValue('u_name'));

 or whatever your display value field name is on this table.

kaushal_snow
Mega Sage

Hi @erin-marie ,

 

use this updated code:

 

var poamArr = [];
var poamField = current.variables['decommission_poam_details'];
var rowCount = poamField.getRowCount();

// Iterate through each row in the MRVS
for (var i = 0; i < rowCount; i++) {
    var row = poamField.getRow(i);
    var poamId = row.sc_decomm_poam_id;

    // Check if POAM ID exists
    if (poamId) {
        var gr = new GlideRecord('u_poam');
        if (gr.get(poamId)) {
            poamArr.push(gr.getDisplayValue()); // Add display value to array
        }
    }
}

// Assign to CHG field if array has values
if (poamArr.length > 0) {
    change.u_poa_m_ids = poamArr.join('\n');
}

 

To extract POAM IDs from a Multi Row Variable Set and transfer them to a Change Request (CHG) in ServiceNow, initialize an empty array to store the POAM display values, iterate through each row in the MRVS to retrieve the POAM ID, query the u_poam table using GlideRecord to fetch the corresponding record, extract the display value, and append it to the array; finally, assign the concatenated display values to the u_poa_m_ids field in the CHG, separated by newlines.....

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

This worked perfectly.  Thank you so much!!!