The CreatorCon Call for Content is officially open! Get started here.

Attempting to compare two arrays and if the first array has differences, then remove user from group

Aaron Duncan
Mega Sage

I have a script I'm putting together that compares an array from a query to the Group Membership table and the Policy Acknowledgement Table.

The logic supposed to compare the 2 arrays, and if a user is in the group membership array but not in the Policy Acknowledgement table, then remove their group membership. I keep getting no output from my script, and you can see where I have tried to output multiple times. ON line 14 and 28, I can see the values from the query, but after that, nothing will show. Can someone help point out what I'm doing wrong?

 

   //Set Arrays
   var groupArray = [];
   var ackFinalArray = [];
   var result = [];
   var gArray = [];
   var aArray = [];

   function groupMembershipQuery() { //Query Group Memberhsip for Policy Ack Group
       var userArray = [];
       var userList = new GlideRecord("sys_user_grmember");
       userList.addEncodedQuery("group=d7a8e8174710c610c79c0f68436d4379");
       userList.query();
       while (userList.next()) {
           // gs.info("Group Memberhsip list: " + userList.sys_id);
           groupArray = userArray.push(userList.sys_id);
       }

       //output should be same as https://<instancename>.service-now.com/sys_user_grmember_list.do?sysparm_query=group%3Dd7a8e8174710c610c79c0f68436d4379&sysparm_first_row=1&sysparm_view=
       return groupArray;
   }

   function activePolicyAcknowledgementInstanceQuery() { //Query to find all active Policy Ack Campaigns and have status of no response.
       var ackArray = [];
       var ackList = new GlideRecord("sn_compliance_policy_acknowledgement_instance");
       ackList.addEncodedQuery("state=1^policy_acknowledgement.active=true");
       ackList.query();
       while (ackList.next()) {
           //  gs.info("Policy Ack List: "+ ackList.assigned_to);
           ackFinalArray = ackArray.push(ackList.assigned_to);
       }
       //Output should be same as https://<instancename>.service-now.com/sn_compliance_policy_acknowledgement_instance_list.do?sysparm_query=state%3D1%5Epolicy_acknowledgement.active%3Dtrue&sysparm_clear_stack=true&sysparm_first_row=1
       return ackFinalArray;
   }

   function compareArrays(a, b) { 
       // Compare the 2 arrays. If a user is in the 1st array and not in the 2nd, then they need the group membership deleted.
       var groupArray = groupArray.filter(function(value) {
           return ackFinalArray.indexOf(value) == -1;
           });
           //Write code to remove on condition
   }

   groupMembershipQuery();
  //  gs.print(compareArrays(groupArray, ackFinalArray));
//    ackArray.forEach(function(element) {
//        gs.print('User Array Element: ' + element);
//    });
//    activePolicyAcknowledgementInstanceQuery();

//    groupArray.forEach(function(element) {
//        gs.print('Ack Array Element: ' + element);
//    });
   

 

1 ACCEPTED SOLUTION

Aaron Duncan
Mega Sage

@Akif_Shah @Mark Endsley @Its_Azar 
Thank you all for your help! I was able to complete the script and it works perfectly! The final issue was figuring out the arrayutil.diff setup to see what values were left over. In this case I was able to just output the users that had no Policy Acknowledgements left but were in the group. From there, I fed it into a function that looped through each value and deleted the appropriate record.

var grArr = groupMembershipQuery();
gs.print("Group Membership Array: " + grArr);
var acArr = activePolicyAcknowledgementInstanceQuery();
gs.print("Active Policy Array: " + acArr);
var cr = compareArrays(grArr, acArr);
gs.print("Compare Array: " + cr);
var userToRemoveArray = [];
removeUsers(cr);
//Show the final Ack Array
/*ackFinalArray.forEach(function(element) {
    gs.print('Final Array Element: ' + element);
});*/


/////////Functions///////

function groupMembershipQuery() { //Query Group Memberhsip for Policy Ack Grouip
    var groupArray = [];
	var userArray = [];
    var userList = new GlideRecord("sys_user_grmember");
    userList.addEncodedQuery("group=d7a8e8174710c610c79c0f68436d4379");
    userList.query();
    while (userList.next()) {
		userArray.push(userList.user.toString());
    }
	var uniqueUserArray = new ArrayUtil().unique(userArray);
	return uniqueUserArray;
}

function activePolicyAcknowledgementInstanceQuery() { //Query to find all active Policy Ack Campaigns and have status of no response.
    var ackFinalArray = [];
    var ackList = new GlideRecord("sn_compliance_policy_acknowledgement_instance");
    ackList.addEncodedQuery("state=1^policy_acknowledgement.active=true");
    ackList.query();
    while (ackList.next()) {
        ackFinalArray.push(ackList.assigned_to.toString());
    }
	var uniqueAckArray = new ArrayUtil().unique(ackFinalArray);
    return uniqueAckArray;
}

function compareArrays(uniqueUserArray, uniqueAckArray) {
    // Compare the 2 arrays. If a user is in the 1st array and not in the 2nd, then they need the group membership deleted.
    // groupArray = uniqueUserArray.filter(function(value) {
    //     return uniqueAckArray.indexOf(value) == -1;
    // });
	
	userToRemoveArray = new ArrayUtil().diff(uniqueUserArray,uniqueAckArray);
	gs.print("Compared Array Output: " + userToRemoveArray); //The 2nd array going in needs to be the user array, as what is left over are those in the group, but with no acks left.
	return userToRemoveArray;
}

function removeUsers(userToRemoveArray){
	userToRemoveArray.forEach(function(value) {
	var groupMembership = new GlideRecord("sys_user_grmember");
    groupMembership.addEncodedQuery(
      "group=d7a8e8174710c610c79c0f68436d4379^user=" + value
    );
    groupMembership.query();
    if (groupMembership.next()) {
    //   gs.info("User " + value.getDisplayValue('name') + " has no acknowledgements left, but is in group.");
       groupMembership.deleteRecord();
	}
});

}

 

View solution in original post

9 REPLIES 9

Akif_Shah
Kilo Sage

Could you try adding .toString() whenever you pushing a value in the array. So

groupArray = userArray.push(userList.sys_id.toString());
//AND
ackFinalArray = ackArray.push(ackList.assigned_to.toString());

Mark Endsley
Tera Guru

It doesn't look like it should output anything. The only function you are running is 

groupMembershipQuery()

that returns a value, but does not log it anywhere.

Its_Azar
Tera Guru
Tera Guru

Hi there @Aaron Duncan 

 

I think  the compareArrays function where you're attempting to filter groupArray. , you're not correctly referencing the arrays within the function due to variable scoping.

 

If this helps kindly accept the response 

☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.




Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG

 Microsoft MVP (AI Services), India

Aaron Duncan
Mega Sage

@Its_Azar @Mark Endsley @Akif_Shah 

 

I updated the code to show that it should be printing out the values of the arrays, but I get nothing back.

I also corrected the compareArrays function to correctly reference the groupArray. I also put in the toString() method as a test.

I added these one at a time to see if anything changed, and I'm not getting an error, I'm actually not getting anything back.

   //Set Arrays
   var groupArray = [];
   var ackFinalArray = [];
   var result = [];
   var gArray = [];
   var aArray = [];

   function groupMembershipQuery() { //Query Group Memberhsip for Policy Ack Grouip
       var userArray = [];
       var userList = new GlideRecord("sys_user_grmember");
       userList.addEncodedQuery("group=d7a8e8174710c610c79c0f68436d4379");
       userList.query();
       while (userList.next()) {
           groupArray = userArray.push(userList.sys_id.toString());
       }

       return groupArray;
   }
   groupArray.forEach(function(element) {
       gs.print('Group Array Element: ' + element);
   });

   function activePolicyAcknowledgementInstanceQuery() { //Query to find all active Policy Ack Campaigns and have status of no response.
       var ackArray = [];
       var ackList = new GlideRecord("sn_compliance_policy_acknowledgement_instance");
       ackList.addEncodedQuery("state=1^policy_acknowledgement.active=true");
       ackList.query();
       while (ackList.next()) {
       
           ackFinalArray = ackArray.push(ackList.assigned_to.toString());
       }
       
       return ackFinalArray;
   }
      ackFinalArray.forEach(function(element) {
       gs.print('Ack Final Array Element: ' + element);
   });

   function compareArrays(groupArray, ackFinalArray) {
       // Compare the 2 arrays. If a user is in the 1st array and not in the 2nd, then they need the group membership deleted.
       groupArray = groupArray.filter(function(value) {
           return ackFinalArray.indexOf(value) == -1;
       });
       //Write code to remove on condition
   }

   groupMembershipQuery();
   activePolicyAcknowledgementInstanceQuery();
   compareArrays(groupArray, ackFinalArray);

   //Show the final Ack Array
   ackFinalArray.forEach(function(element) {
       gs.print('Final Array Element: ' + element);
   });