Looping through array and comparing multiple values to another variable.

Steven Watts2
Tera Guru

Hi,

I am using the following script in a workflow to query the input record created I have sent a command to the MID Server via the ECC queue.

var ecc = new GlideRecord("ecc_queue");
var source = current.sys_id;
var enc = 'topic!=HeartbeatProbe^topicNOT LIKEqueue.^queue=input^source='+source;
ecc.addEncodedQuery(enc);
ecc.query();


if(ecc.next()) {
	var xmlPayload = ecc.payload.getValue('payload');
	var payload = gs.getXMLText(xmlPayload, "//stdout");
	
	var array = ['User successfully added to group', 'User exists in the group'];
	
	for (var i = 0; i < array.length; i++) {
        if (payload.toString().indexOf(array[i])  > -1 ) {
            workflow.scratchpad.user_in_group = 'true';
        } else {
            workflow.scratchpad.user_in_group = 'false';
        }
    }
	
}

 

I'm looking through the value in <stdout></stdout> and checking the response contains either of the values in my array. When the response is 'User exists in the group', this works as expected and the following if statement activity result is set to 'yes'. When the response is 'User successfully to the group' the activity result is set to 'no' eventhough it should be 'yes.

StevenWatts2_0-1673441996750.png

Any ideas?

 

Steveb

1 ACCEPTED SOLUTION

-O-
Kilo Patron
Kilo Patron

The way the if is written right now, it overwrites a find with a non-find, that is why it is working only one way.

I would do something this:

 

var ecc = new GlideRecord("ecc_queue");
var source = current.sys_id;
var enc = 'topic!=HeartbeatProbe^topicNOT LIKEqueue.^queue=input^source=' + source;

ecc.addEncodedQuery(enc);

ecc.query();

if (ecc.next()) {
	var xmlPayload = ecc.payload.getValue('payload');
	var payload = gs.getXMLText(xmlPayload, "//stdout");

	var array = [/User\s+successfully\s+added\s+to\s+group/i,
				 /User\s+exists\s+in\s+the\s+group/i,
				];

	workflow.scratchpad.user_in_group = array.some(getTesterForPayload(payload));

	function getTesterForPayload (payload) {
		return function tester (regExp) {
			return regExp.test(payload);
		};
	}
}

 

On one hand use regular expressions - can be a bit more flexible, at leas case insensitive, on the other use a readily available array method meant exactly for this: to check whether at least one member of an array fulfills a condition (in this case that the text is to be found in the payload).

View solution in original post

5 REPLIES 5

Basheer
Mega Sage
if (payload.toString().indexOf(array[i])  > -1 ) {
for (var i = 0; i < array.length; i++) {
workflow.scratchpad.user_in_group = 'true';
}
} 
else {
workflow.scratchpad.user_in_group = 'false';
}

Try like below

 

if (payload.toString().indexOf(array[i])  > -1 ) {
for (var i = 0; i < array.length; i++) {
            workflow.scratchpad.user_in_group = 'true';
}
        } else {
            workflow.scratchpad.user_in_group = 'false';
        }

 

Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.

Hi @Basheer 

 

Thanks for the response. I have tried that and the outcome is still the same. See below:

 

var ecc = new GlideRecord("ecc_queue");
var source = current.sys_id;
var enc = 'topic!=HeartbeatProbe^topicNOT LIKEqueue.^queue=input^source=' + source;
ecc.addEncodedQuery(enc);
ecc.query();


if (ecc.next()) {
    var xmlPayload = ecc.payload.getValue('payload');
    var payload = gs.getXMLText(xmlPayload, "//stdout");

    var array = ['User successfully added to group', 'User exists in the group'];


    if (payload.toString().indexOf(array[i]) > -1) {
        for (var i = 0; i < array.length; i++) {
            workflow.scratchpad.user_in_group = 'true';
        }
    } else {
        workflow.scratchpad.user_in_group = 'false';
    }
}

StevenWatts2_0-1673444881493.png

 

<stdout>Transcript started, output file is C:\PS_SCRIPTS\Transcripts\RITM0091890.txt
User successfully added to group</stdout>

for (var i = 0; i < array.length; i++) {
        if (payload.toString().indexOf(array[i])  > -1 ) {
            workflow.scratchpad.user_in_group = 'true';
activity.output = "yes"; //may be activity.result ="yes", try using true instead of yes
        } else {
            workflow.scratchpad.user_in_group = 'false';
activity.output = "no" //maybe activity.result = "no", try using false instead of no
        }
    }

 Try this change in code.

 

 

Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.

jaheerhattiwale
Mega Sage
Mega Sage

@Steven Watts2 Please copy the text "User successfully added to the group" text from the payload and then add it to the array. There might some invisible character in the already added string.

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023