I was trying to get all of the unique sys_id’s of groups that were included in HRSD COE Security Policies using the below query
var groups = [];
var polgroupGR = new GlideRecord("sn_hr_core_m2m_security_policy_group");
polgroupGR.addEncodedQuery('security_policy.active=true');
polgroupGR.query();
while (polgroupGR.next()){
var groupSysId = polgroupGR.group.sys_id;
if(groups.indexOf(groupSysId) == -1){
groups.push(groupSysId);
}
}
However, this was not producing unique results, the same sys_ids were being added repeatedly to the array.
After some googling I realized that this was due to a problem with the type of the variable groupSysId. It is an object, and [I think] indexOf requires a string to work properly. It’s good practice to use getValue and getDisplayValue when working with GlideRecord… this shows where my sloppiness bit me in the buttocks. Adding toString() add the end of polgroupGR.group.sys_id fixed the issue:
var groups = [];
var polgroupGR = new GlideRecord("sn_hr_core_m2m_security_policy_group");
polgroupGR.addEncodedQuery('security_policy.active=true');
polgroupGR.query();
while (polgroupGR.next()){
var groupSysId = polgroupGR.group.sys_id.toString();
if(groups.indexOf(groupSysId) == -1){
groups.push(groupSysId);
}
}
