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.

ArrayUtil is not properly checking for duplicates

Aaron Duncan
Mega Sage

I have this script where I am attempting to take 300+ names in the assigned to field that show as a query result and then making the array have no duplicates. I've tried the ArrayUtil.unique, indexOf, and ! .contains options with no change.

Any help would be appreciated.

 

var arrUtil = new ArrayUtil();
var groupMembers = [];
var list = new GlideRecord("sn_compliance_policy_acknowledgement_instance"); //Look at the Policy Ack table
list.addEncodedQuery("policy_acknowledgement.active=true^state=1"); //Where the campaign is active, and state is no response
list.orderBy(list.assigned_to); //order by the assigned to.
list.query();
while (list.next()) { //While there is a result
    if (!arrUtil.contains(groupMembers, list.assigned_to)) { //see if that person is in the array already.
        groupMembers.push(list.assigned_to); //if not, then add them to the array.	
    }
    
    for (var i = 0; i < groupMembers.length; i++) { //show me the array
		gs.info(groupMembers[i]);
    }

}

 

 

1 ACCEPTED SOLUTION

I went another way with it that was much easier and more efficient.

var a = gs.getUserID();
var list = new GlideRecord("sn_compliance_policy_acknowledgement_instance");
list.addEncodedQuery(
  "policy_acknowledgement.active=true^state=1^assigned_to="+a
);
list.setLimit(1);
list.query();
if (!list.next()) {
  var groupMembership = new GlideRecord("sys_user_grmember");
  groupMembership.addEncodedQuery(
    "group=d7a8e8174710c610c79c0f68436d4379^user="+a
  );
  groupMembership.query();
  if (groupMembership.next()) {
    groupMembership.deleteRecord();
//    gs.info("User removed from group");
  } else {
  //  gs.info("use has no acks left, and not in group.");
  }
  // gs.info(list.getDisplayValue());
} else {
//  gs.info("user has acknowledgements left to complete.");
}

View solution in original post

6 REPLIES 6

AshishKM
Kilo Patron
Kilo Patron

Hi @Aaron Duncan

 

Please check with below code.

 

 

var arrUtil = new ArrayUtil();
var groupMembers = [];
var list = new GlideRecord("sn_compliance_policy_acknowledgement_instance"); //Look at the Policy Ack table
list.addEncodedQuery("policy_acknowledgement.active=true^state=1"); //Where the campaign is active, and state is no response
list.orderBy('assigned_to'); //order by the assigned to.
list.query();
while (list.next()) { //While there is a result
    groupMembers.push(list.assigned_to); //if not, then add them to the array.	
	/*
	//see if that person is in the array already.
	if (!arrUtil.contains(groupMembers, list.assigned_to)) { 
        groupMembers.push(list.assigned_to); //if not, then add them to the array.	
    }
	*/

} // while loop

//var arrayUtil = new ArrayUtil();
groupMembers = arrayUtil.unique(groupMembers);

 //show me the array
for (var i = 0; i < groupMembers.length; i++) {
        gs.info(groupMembers[i]);
}

 

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

In the list, I'm expecting to see over a hundred individual results, but I'm only getting one sys_id back. It appears that it takes the results and then overwrites each value until only one is left.

Here is a sample before the arrUtil.unique(groupmembers is run.

*** Script: 40579ba11b6e681078c5bb7f034bcb64
*** Script: 064757a11b6e681078c5bb7f034bcbf8
*** Script: 63dc68751b35645078c5bb7f034bcb4e
*** Script: 20a7d7651b6e681078c5bb7f034bcb23
*** Script: 6b77d7691ba6a85026c4fcccdd4bcb2c
*** Script: c6a7dc871b17c51022f52177624bcbf4
*** Script: 5d77d3691ba6a85026c4fcccdd4bcb17
*** Script: e0492c78c3abb918dcc897af05013164
*** Script: d9c377251bc581108769ece0604bcb60
*** Script: a69793651b6e681078c5bb7f034bcbf5
*** Script: 9b3797e51ba6a85026c4fcccdd4bcbdd
*** Script: 2ef025691b7c7c1026c4fcccdd4bcbe9
*** Script: ec9713a91ba6a85026c4fcccdd4bcbfc
*** Script: b8a7d7651b6e681078c5bb7f034bcb6d
*** Script: 91479be51ba6a85026c4fcccdd4bcb5d
*** Script: d2871f691ba6a85026c4fcccdd4bcb21
*** Script: 8a87db691ba6a85026c4fcccdd4bcb66
*** Script: cf3713a11b6e681078c5bb7f034bcb61
*** Script: 71ca402ac3b63114fa787aef0501313d

 

This is after, only 1 value, and the array length is 1

*** Script: 23a4e5769703c11085affe3bf253afe6

 

are you expecting 300+ unique name or it's total 300 including duplicate.

you can check the total unique count in the list view with same filter condition on table (sn_compliance_policy_acknowledgement_instance) and group by on column "assigned_to".

 

 

 

 

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

I'm expecting about 300 unique names. When I output the array right after the value is pushed, i see this happening where the new values overwrites the existing values plus adds it to the end.

 

*** Script: 40579ba11b6e681078c5bb7f034bcb64
*** Script: 064757a11b6e681078c5bb7f034bcbf8,064757a11b6e681078c5bb7f034bcbf8
*** Script: 63dc68751b35645078c5bb7f034bcb4e,63dc68751b35645078c5bb7f034bcb4e,63dc68751b35645078c5bb7f034bcb4e
*** Script: 20a7d7651b6e681078c5bb7f034bcb23,20a7d7651b6e681078c5bb7f034bcb23,20a7d7651b6e681078c5bb7f034bcb23,20a7d7651b6e681078c5bb7f034bcb23
*** Script: 6b77d7691ba6a85026c4fcccdd4bcb2c,6b77d7691ba6a85026c4fcccdd4bcb2c,6b77d7691ba6a85026c4fcccdd4bcb2c,6b77d7691ba6a85026c4fcccdd4bcb2c,6b77d7691ba6a85026c4fcccdd4bcb2c
*** Script: c6a7dc871b17c51022f52177624bcbf4,c6a7dc871b17c51022f52177624bcbf4,c6a7dc871b17c51022f52177624bcbf4,c6a7dc871b17c51022f52177624bcbf4,c6a7dc871b17c51022f52177624bcbf4,c6a7dc871b17c51022f52177624bcbf4
*** Script: 5d77d3691ba6a85026c4fcccdd4bcb17,5d77d3691ba6a85026c4fcccdd4bcb17,5d77d3691ba6a85026c4fcccdd4bcb17,5d77d3691ba6a85026c4fcccdd4bcb17,5d77d3691ba6a85026c4fcccdd4bcb17,5d77d3691ba6a85026c4fcccdd4bcb17,5d77d3691ba6a85026c4fcccdd4bcb17
*** Script: e0492c78c3abb918dcc897af05013164,e0492c78c3abb918dcc897af05013164,e0492c78c3abb918dcc897af05013164,e0492c78c3abb918dcc897af05013164,e0492c78c3abb918dcc897af05013164,e0492c78c3abb918dcc897af05013164,e0492c78c3abb918dcc897af05013164,e0492c78c3abb918dcc897af05013164
*** Script: d9c377251bc581108769ece0604bcb60,d9c377251bc581108769ece0604bcb60,d9c377251bc581108769ece0604bcb60,d9c377251bc581108769ece0604bcb60,d9c377251bc581108769ece0604bcb60,d9c377251bc581108769ece0604bcb60,d9c377251bc581108769ece0604bcb60,d9c377251bc581108769ece0604bcb60,d9c377251bc581108769ece0604bcb60