- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2023 05:00 AM
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.
Any ideas?
Steveb
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2023 07:14 AM - edited ‎01-11-2023 08:01 AM
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).

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2023 05:25 AM
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 mark correct if my response has solved your query.
Cheers,
Mohammed Basheer Ahmed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2023 05:48 AM
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';
}
}
<stdout>Transcript started, output file is C:\PS_SCRIPTS\Transcripts\RITM0091890.txt
User successfully added to group</stdout>

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2023 05:52 AM
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 mark correct if my response has solved your query.
Cheers,
Mohammed Basheer Ahmed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2023 05:49 AM
@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.
ServiceNow Community Rising Star, Class of 2023