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

In the compare function you are returning ackFinalArray but your function isn't set assign it to any value.

var result = compareArrays(groupArray, ackFinalArray);
gs.log(result);

Try This^ 

Also, check your data, there may be nothing left after the comparison

Akif_Shah
Kilo Sage

Can you try this script below and see if anything is printed/logged

var grArr = groupMembershipQuery();
gs.print("Group Membership Array: " + grArr);
var acArr = activePolicyAcknowledgementInstanceQuery();
gs.print("Active Policy Array: " + acArr);
gs.print("Compare Array: " + compareArrays(grArr, acArr));

//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 userList = new GlideRecord("sys_user_grmember");
    userList.addEncodedQuery("group=d7a8e8174710c610c79c0f68436d4379");
    userList.query();
    while (userList.next()) {
        groupArray = userArray.push(userList.sys_id.toString());
    }
    return groupArray;
}

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());
    }
    return ackFinalArray;
}

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
}

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();
	}
});

}

 

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