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.

Accessing reference variable present in Variable set in Service request workflow.

Nirav
Tera Expert

Hi,

 

I am creating a new workflow for service request. What I am trying to check is when a request is created then the workflow should check if the variable cost center contains "PV90" then it should move forward and go for specific user approval. 

 

Catalog Variable : Cost Center (cost_center)   // Reference Variable referencing cmn_cost_center table

Catalog Variable Set :  R12 Account Number Information (internal name: account_number_service_catalog)

 

I have tried the below script but it does not works. I have also tried to access it with "current.variables.cost_center.account_number_service_catalog.getDisplayValue();" which also did not work. Please help.

 

function ifScript() {

var CC = current.variables.cost_center.getDisplayValue();
if (CC.toString().indexOf('PV90') >= 0) {


return 'yes';

}
return 'no';
}

 

 

shelp.png

1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

If by 'variable set' you mean that the cost_center variable exists as part of a multi-row variable set, then it's best to iterate through the JSON to look for the value.  The value, however will be the sys_id of the record since this is a reference field.  getDisplayValue() does not work when retrieving a mrvs:

 

function ifScript() {
    var mrvs = current.variables.account_number_service_catalog;
    var rowCount = mrvs.getRowCount();
    for (var i = 0; i < rowCount; i++) {
        var row = mrvs.getRow(i);
        if (row.cost_center == "sys_id") {
            return 'yes';
        }
    }
    return 'no';
}

 

of course replacing "sys_id" with that of the record you are looking for.  If you have multiple Cost Center records that have a Name which contains "PV90", then you'll need to add a GlideRecord into the loop to check the name field for this string:

 

function ifScript() {
    var mrvs = current.variables.account_number_service_catalog;
    var rowCount = mrvs.getRowCount();
    for (var i = 0; i < rowCount; i++) {
        var row = mrvs.getRow(i);
        var cc = new GlideRecord('cmn_cost_center');
        if (cc.get(row.cost_center)) {
            if (cc.name.indexOf("PV90") > -1) {
                return 'yes';
            }
        }
    }
    return 'no';
}