Help with Workflow script = forEach

Lon Landry4
Mega Sage
I can get the script below to work for a single record without any of the forEach function lines below...
But, I must be so missing something when trying to loop through this array (mrvsValues).
 
Any suggestions?
 
 
var mrvsValues = current.variables.mrvs_sys_ids;
gs.info("payload = "+ mrvsValues);
 
mrvsValues.forEach(function (value){
var payloadParsed = JSON.parse(mrvsValues); //this gets my input readable
gs.info('Parsed: '+payloadParsed);
var grAsset = new GlideRecord('alm_hardware');
grAsset.addQuery('sys_id', payloadParsed);
grAsset.query();
if (grAsset.next()){
grAsset.install_status = '6';
grAsset.update();
}
});
1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

I think you are suppose to parse it to JSON before the loop. Also can you try the for loop?

 

var mrvsValues = current.variables.mrvs_sys_ids;
gs.info("payload = "+ mrvsValues);
var payloadParsed = JSON.parse(mrvsValues); //this gets my input readable
for(var i in payloadParsed)
{
gs.info('Parsed: '+payloadParsed[i]);
var grAsset = new GlideRecord('alm_hardware');
grAsset.addQuery('sys_id', payloadParsed[i]);
grAsset.query();
if (grAsset.next()){
grAsset.install_status = '6';
grAsset.update();
}
}

 


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

7 REPLIES 7

Aniket Chavan
Tera Sage
Tera Sage

Hello @Lon Landry4 

 

You can refer to the below script and give a shot.

 

var mrvsValues = current.variables.mrvs_sys_ids;

gs.info("payload = " + mrvsValues);

 

mrvsValues.forEach(function (value) {

  var payloadParsed = JSON.parse(value); // Parse each element of the array

  gs.info('Parsed: ' + payloadParsed);

 

  var grAsset = new GlideRecord('alm_hardware');

  grAsset.addQuery('sys_id', payloadParsed);

  grAsset.query();

 

  if (grAsset.next()) {

    grAsset.install_status = '6';

    grAsset.upda

te();

  }

});

 

Please let me know your views on this and mark helpful and correct if it's helpful to you.

 

Thank you,

Aniket 

Ethan Davies
Mega Sage
Mega Sage

I think the issue is with how you are handling the payload for the MVRS. The payload for a Multi-Row Variable Set is an Array of Objects, so you need to treat it as such. Each row in the Multi-Row Variable Set is represented as a single Object in the Array. So if you have 3 rows, there will be three Objects in there. For each of the Objects, there are key-value pairs that represent the MVRS field name and value.

 

With all that said - your code will need to look something like this.

var mrvsValues = current.variables.mrvs_sys_ids;
var payloadParsed = JSON.parse(mrvsValues); //this gets my input readable

for (var row in payloadParsed) {
    // Replace 'element' with the name of the field in your MVRS that contains the sys_id of the asset record you want to update.
    updateAsset(payloadParsed[row].element);
}

function updateAsset(assetID) {
    var grAsset = new GlideRecord('alm_hardware');
    if (grAsset.get(String(assetID))){
        grAsset.install_status = '6';
        grAsset.update();
    }   
}

 

You could also make the code above more efficient by just making one GlideRecord call instead of multiple.

var mrvsValues = current.variables.mrvs_sys_ids;
var payloadParsed = JSON.parse(mrvsValues); //this gets my input readable
var idsToUpate = [];

for (var row in payloadParsed) {
    // Replace 'element' with the name of the field in your MVRS that contains the sys_id of the asset record you want to update.
    idsToUpate.push(payloadParsed[row].element);
}

updateAssets(idsToUpate.toString());

function updateAssets(assetIDs) {
    var grAsset = new GlideRecord('alm_hardware');
    grAsset.addEncodedQuery('sys_idIN'+assetIDs);
    if (grAsset.hasNext()){
        grAsset.install_status = '6';
        grAsset.updateMultiple();
    }   
}