- drop the current group members
- repopulate the group members
- update the group (e.g. group manager)
import sheet format: , delimited list of users (email)
transform map scripts
//--prevent insert
onstart:
var grGrps = new GlideRecord('sys_user_group');
grGrps.query();
log.info('onStart:TrMap:BulkUpdateCCGrps:: sys_user_group count= ' + grGrps.getRowCount());
var grGrpMems = new GlideRecord('sys_user_grmember');
grGrpMems.query();
log.info ('onStart:TrMap:BulkUpdateCCGrps:: sys_user_grmember= ' + grGrpMems.getRowCount());
oncomplete:
//--record the final counts
var IS=source.sys_import_set.getDisplayValue();
var grGrps = new GlideRecord('sys_user_group');
grGrps.query();
log.info(IS + ' - onComplete:TrMap:BulkUpdateCCGrps:: sys_user_group count= ' + grGrps.getRowCount());
var grGrpMems = new GlideRecord('sys_user_grmember');
grGrpMems.query();
log.info(IS + ' - onComplete:TrMap:BulkUpdateCCGrps:: sys_user_grmember= ' + grGrpMems.getRowCount());
log.info(IS + ' - onComplete:TrMap:BulkUpdateCCGrps:: sys_user_grmember expected difference= 13780 [52 times 265]');
onafter:
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
var IS = source.sys_import_set.getDisplayValue();
var sGroupSysID = target.sys_id;
if (checkGroup(sGroupSysID)) {
//--drop the current group members
var grGrp = new GlideRecord('sys_user_grmember');
grGrp.setLimit(100); //--failsafe
grGrp.addQuery('group', sGroupSysID);
/*grGrp.query();
//log.info(IS + ' - onAfter:TrMap:BulkUpdateCCGrps:: ' + sGroupName+', TOTAL MEMBERS: '+grGrp.getRowCount());*/
grGrp.deleteMultiple();
//--now update the group memberships:
var sGroupMembers = source.u_group_members.toString();
var sGroupMembers_Arr = sGroupMembers.split(",");
var sGroupName = source.u_group_name;
if (sGroupMembers_Arr.length > 0) {
//log.info(IS + ' - onAfter:TrMap:BulkUpdateCCGrps:: ' + sGroupName + ',' + sGroupSysID);
for (member in sGroupMembers_Arr) {
if (checkUser(sGroupMembers_Arr[member])) {
addUserToGroup(sGroupMembers_Arr[member], sGroupSysID);
}
}
}
}
})(source, map, log, target);
function addUserToGroup(userSYSID, groupSYSID) {
var grGrp = new GlideRecord('sys_user_grmember');
grGrp.newRecord();
grGrp.group = groupSYSID;
grGrp.user = userSYSID;
grGrp.insert();
}
function checkUser(userEmail) {
var bReturn = false;
var grGrp = new GlideRecord('sys_user');
if (grGrp.get('email', userEmail)) {
bReturn = true;
}
return bReturn;
}
function checkGroup(groupSysID) {
var bReturn = false;
var grGrp = new GlideRecord('sys_user_group');
if (grGrp.get('sys_id', groupSysID)) {
bReturn = true;
}
return bReturn;
}
Comments
Post a Comment