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

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.");
}

good to know ... finally you got the result.


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