group and department sync

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?

2 REPLIES 2

rahulswami
Tera Contributor

Hi @LJ_23,

The problem is in your first business rule (Sync Group Manager from Dept head). BR 2 is fine.

if (current.getValue('type') == 'itil') {

 

On "sys_user_group", Type is a list field that stores the sys_ids of Group Type records, not the text “itil”. So this check is never true, and ITIL groups are never skipped. BR 2 already does it correctly, because it looks up the ITIL group type’s "sys_id" first.

Fix for BR 1: use the same approach as BR 2.

// find the ITIL group type
var itil = new GlideRecord('sys_user_group_type');
if (!itil.get('name', 'itil')) {
    return; // if the ITIL type isn't found, stop rather than risk updating ITIL groups
}

// skip ITIL groups
var types = (current.getValue('type') || '').split(',');
if (types.indexOf(itil.getUniqueValue()) != -1) {
    return;
}

 

Small fix for BR 2: add this guard right after the ITIL lookup. If the lookup fails, "itilId" stays empty, and groups with no type would then be skipped by mistake.

if (!itilId) {
    return;
}

 

Also confirm that the ITIL record under System Security > Groups > Group Types has the Name itil exactly. If it doesn’t, change the lookup to match.


If this helped to answer your query, please mark it helpful & accept the solution.

Its_Azar
Mega Sage

Hi there @LJ_23 

 

The issue is in your first BR. You're checking current.getValue('type') == 'itil', but the type field stores group type sys_ids, not the name 'itil'

 

var types = (current.getValue('type') || '').split(',');
if (types.indexOf(itilId) != -1) {
    return; // Use continue inside the while loop in BR 2
}