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.

use array in Script Include

tanz
Tera Expert

I have a script include which has an array. 

var x= [{prod_id_n: "12",lower_id_n:"34"},{prod_id_m: "56",lower_id_m:"78"}];

Now I am calling these values from business rule/background script: 

if user belongs to location n and is present in prod get the value prod_id_n

if user belongs to location n and is present in lower get the value lower_id_n

if user belongs to location m and is present in prod get the value prod_id_m

if user belongs to location m and is present in lower get the value lower_id_m

how can I achieve this using scripting.

 

1 REPLY 1

Amit Pandey
Kilo Sage

Hi @tanz 

 

Please try-

 

// Assume 'x' is the array defined in your script include

function getUserIdentity(location, presence) {
    var userId;
    
    // Iterate over the array
    for (var i = 0; i < x.length; i++) {
        var obj = x[i];
        
        // Check conditions based on location and presence
        if (location === 'n') {
            if (presence === 'prod' && obj.prod_id_n) {
                userId = obj.prod_id_n;
                break;
            } else if (presence === 'lower' && obj.lower_id_n) {
                userId = obj.lower_id_n;
                break;
            }
        } else if (location === 'm') {
            if (presence === 'prod' && obj.prod_id_m) {
                userId = obj.prod_id_m;
                break;
            } else if (presence === 'lower' && obj.lower_id_m) {
                userId = obj.lower_id_m;
                break;
            }
        }
    }
    
    return userId;
}

 

Please mark my answer helpful and correct, if it works for you.

 

Regards,

Amit