Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Sync group and department data issue

LJ_23
Tera Contributor

Hi all,

I am having issues with syncing two fields.

I have been assigned a task too set the manager of a group to department head in those groups so not empty (synchronise group manager information from the mapped department head) this change in department table will automatically change in the group table.

ITIL group is out of scope and should NOT update when the manager is selected as department head 

here are my two business rules.

 

1.Sync Group Manager from Dept head noITIL >>>>>>>

(function executeRule(current, previous) {

    // br should ONLY run when group created or department changes AND is not ITIL //
    if (current.operation() != 'insert' &&
    !current.u_department.changes()) {
    return;
    }

    // do NOT UPDATE manager for ITIL groups //
    if (current.getValue('type') == 'itil') {
        return;
    }

    // GET the department linked to the group //
    var departmentId = current.getValue('u_department');

    // do NOT continue if the group does NOT have department thats selected //
    if (!departmentId) {
        return;
    }

    // FIND department record //
    var department = new GlideRecord('cmn_department');
   
    if (!department.get(departmentId)) {
        return;
    }

    // GET department head from department record //
    var departmentHead = department.getValue('dept_head');

    // DO NOT continue if the department does NOT have a department head //
    if (!departmentHead) {
        return;
    }

    // SET group manager to the department head //
    current.setValue('manager', departmentHead);
   
})(current, previous);

 

2.Sync Dept Head to Group Manager no ITIL >>>>>>>>

(function executeRule(current, previous) {

    // only run when department head changes
    if (!current.dept_head.changes()) {
        return;
    }
   
    // find the ITIL group type
    var itil = new GlideRecord('sys_user_group_type');
    itil.addQuery('name', 'itil');
    itil.query();

    var itilId = '';
    if (itil.next()) {
        itilId = itil.getUniqueValue();
    }

    // find groups linked to department
    var group = new GlideRecord('sys_user_group');
    group.addQuery('u_department', current.getUniqueValue());
    group.query();

    // check the link groups
    while (group.next()) {
        // skip the itil groups
        if ((group.getValue('type') || '').split(',').indexOf(itilId) != -1){
            continue;
        }
        // only update department head is set
        if (current.getValue('dept_head')) {
            // set group manager to department head
            group.setValue('manager', current.getValue('dept_head'));
            // save any changes
            group.update();
        }
}

})(current, previous);

 

The issue I am having is that if I select an ITIL group manager as the department head that automatically updates the manager too. The correct department will only feed group. Itil group keep out of scope still - should not be the case

Is there anything in my script which is allowing the ITIL groups to update.

Is there anything I can do to solve this issue?

0 REPLIES 0