Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Help looping thru MRVS in Workflow script

Lon Landry4
Mega Sage
 
I am trying to loop thru a MRVS.
The below script is working for 1st row in MRVS.
No errors found; but one interesting info message at same time as transaction ran; CatalogUIPolicyBuilder: Cannot get question for 'a sys ID is shown here'. Skipping term.
The sys ID shown cannot be found on any related tables. 
I have tried using (asset._next()) & (asset.next()) but this is not working as I hoped it would.
 
//Get the value form MRVS field
var MRVSfield = current.variables.assets_assigned.assets_assigned_to_this_user;
var rqstdFor = current.variables.requested_for_full_name.name;
//Get the Name field value connnected to the Sys ID because assigned_to cannot be a sys_id
var rqstdForName = rqstdFor.getDisplayValue();
//get corresponding asset on Hardware table
var asset = new GlideRecord ('alm_hardware');
asset.addQuery('sys_id', MRVSfield);
asset.query();
 
//Update corresponding asset on Hardware table
if (asset._next()){
asset.assigned_to = rqstdForName;
asset.install_status='1';
asset.substatus ='';
asset.update();
}
//If no record found on Hardware table
else{
gs.error('No corresponding asset found for MRVS value:'+MRVSfield);
}
 
I have also tried approach with forEach - but not successful
----------------------------------------------------
//Get the value form MRVS field assets_assigned_to_this_user
var MRVSfield = [current.variables.assets_assigned.assets_assigned_to_this_user];
var rqstdFor = current.variables.requested_for_full_name.name;
//Get the Name field value connnected to the Sys ID because assigned_to cannot be a sys_id
var rqstdForName = rqstdFor.getDisplayValue();
//get corresponding asset on Hardware table
var asset = new GlideRecord ('alm_hardware');
asset.addQuery('sys_id', MRVSfield);
asset.query();
MRVSfield.forEach(myFunction);
 
function myFunction(MRVSfield){
//Update corresponding asset on Hardware table
MRVSfield.asset.assigned_to = rqstdForName;
MRVSfield.asset.install_status='1';
MRVSfield.asset.substatus ='';
MRVSfield.asset.update();
}
//If no record found on Hardware table
/* else{
gs.error('No corresponding asset found for MRVS value:'+MRVSfield);
}*/
1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Here's the approach I always use to loop through an MRVS in a Workflow script, adapted to your example:

var rqstdFor = current.variables.requested_for_full_name.name.getDisplayValue();
//retrieve the entire MRVS contents
var mrvs = current.variables.assets_assigned;
var rowCount = mrvs.getRowCount();
//loop through the MRVS contents
for (var i=0; i<rowCount; i++) {
    var row = mrvs.getRow(i);
    //get corresponding asset on Hardware table
    var asset = new GlideRecord ('alm_hardware');
    if (asset.get(row.assets_assigned_to_this_user)) {
        //Update corresponding asset on Hardware table
        asset.assigned_to = rqstdForName;
        asset.install_status='1';
        asset.substatus ='';
        asset.update();
    } else { //if no record found on Hardware table
        gs.error('No corresponding asset found for MRVS value:'+row.assets_assigned_to_this_user);
    }
}

although if requested_for_full_name is a reference type variable, then rqstdFor should just be

var rqstdFor = current.variables.requested_for_full_name;

since the assigned_to field on the alm_hardware table is a reference type field, it needs a sys_id as its value.

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Here's the approach I always use to loop through an MRVS in a Workflow script, adapted to your example:

var rqstdFor = current.variables.requested_for_full_name.name.getDisplayValue();
//retrieve the entire MRVS contents
var mrvs = current.variables.assets_assigned;
var rowCount = mrvs.getRowCount();
//loop through the MRVS contents
for (var i=0; i<rowCount; i++) {
    var row = mrvs.getRow(i);
    //get corresponding asset on Hardware table
    var asset = new GlideRecord ('alm_hardware');
    if (asset.get(row.assets_assigned_to_this_user)) {
        //Update corresponding asset on Hardware table
        asset.assigned_to = rqstdForName;
        asset.install_status='1';
        asset.substatus ='';
        asset.update();
    } else { //if no record found on Hardware table
        gs.error('No corresponding asset found for MRVS value:'+row.assets_assigned_to_this_user);
    }
}

although if requested_for_full_name is a reference type variable, then rqstdFor should just be

var rqstdFor = current.variables.requested_for_full_name;

since the assigned_to field on the alm_hardware table is a reference type field, it needs a sys_id as its value.

Lon Landry4
Mega Sage

Thanks for the help, this worked. I have been revisiting Chuck Tomasi's training on youtube for this. But, I could not wrap my head around it this... I will keep up with the Training. Thanks again.

You are welcome!