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.

How to compare values in an array and set values?

User205031
Tera Contributor

Hi All,

 

I have a VIT from and there are 3 detections for that VIT. We have a field 'A' in the detection form

I need to gliderecord the detection form and check the value of field 'A' for 3 records.

If any of the value is 'true' for field 'A' , I have to return the value as 'true'. 

If all the values of field 'A' are false, then I have to return the value as 'false'.

 

How to achieve this?

1 ACCEPTED SOLUTION

Pratiksha2
Mega Sage

Hello @User205031 -
Please give a try using below code:

var detectionFormGR = new GlideRecord('detection_form');
detectionFormGR.addQuery('A', current.sys_id);
detectionFormGR.query();
var isAnyTrue = false;
while (detectionFormGR.next()) {
    if (detectionFormGR.getValue('A') == 'true') {
        isAnyTrue = true;
        break;
    }
}
var result = isAnyTrue ? 'true' : 'false';
gs.info("Final Result: " + result);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Pratiksha

View solution in original post

2 REPLIES 2

Harish KM
Kilo Patron
Kilo Patron

Hi @User205031 you can do something like this though I didnot understand your requirement

var gr = new GlideRecord('detection table');
gr.addQuery('A', current.sys_id);// field A from detection table should match current table
gr.query();

var hasTrueValue = false;

while (gr.next()) {
var fieldValue = gr.getValue('A');
//has anyone record with true, then break
if (fieldValue === 'true') {
//your logic
hasTrueValue = true;
break;
}
else{
// no record with true value

}

var result = hasTrueValue ? 'true' : 'false';

gs.info('Result: ' + result);

Regards
Harish

Pratiksha2
Mega Sage

Hello @User205031 -
Please give a try using below code:

var detectionFormGR = new GlideRecord('detection_form');
detectionFormGR.addQuery('A', current.sys_id);
detectionFormGR.query();
var isAnyTrue = false;
while (detectionFormGR.next()) {
    if (detectionFormGR.getValue('A') == 'true') {
        isAnyTrue = true;
        break;
    }
}
var result = isAnyTrue ? 'true' : 'false';
gs.info("Final Result: " + result);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Pratiksha