Comparing a user's department against the current Business Owning BU or Platform of a business app

matthew_hughes
Kilo Sage

In the Service Catalogue, I'm trying to create a new catalogue item for maintaining a business application. The requirement is that if the user proposes a change to the Business Owner then check the 'Department' field on the new owner's User record.
If the path to this department does not align to the current Business Owning BU or Business owning Platform then inform the user that the proposed business owner does not sit within the current Business owning Business Unit and/or Platform.

 

I've tried to build the logic in a script include and client script. However, even if the new owner's department is correctly align the below message still appears.

matthew_hughes_0-1766578165236.jpeg

 

I'm using the following functions in my script include:

    _getOneBySysId: function(departmentTable, departmentId) {
        if (!departmentTable || !departmentId) return null;
        var grRelatedDepartments = new GlideRecord(departmentTable);
        grRelatedDepartments.addQuery('sys_id', departmentId);
        grRelatedDepartments.setLimit(1);
        grRelatedDepartments.query();
        return grRelatedDepartments.next() ? grRelatedDepartments : null;
    },
 
 _resolveHierarchyFromDepartment: function(startDeptSysId) {
        var departmentHierarchy = {
            bu_or_function: '',
            platform: ''
        };
        if (!startDeptSysId) return departmentHierarchy;

        var currentId = startDeptSysId.toString();
        var seen = {}; // cycle protection
        var safetyCap = 100; // hard limit to avoid infinite loops
        var hops = 0;

        while (currentId && !seen[currentId] && hops < safetyCap) {
            seen[currentId] = true;

            // Fetch current department (scoped-safe fetch by sys_id)
            var cur = new GlideRecord('cmn_department');
            cur.addQuery('sys_id', currentId);
            cur.setLimit(1);
            cur.query();

            if (!cur.next()) break; // missing row -> stop

            var deptType = (cur.getValue('u_department_type') || '').toString();

            // Capture nearest matches (first seen while climbing)
            if (!departmentHierarchy.platform && deptType === 'Platform') {
                departmentHierarchy.platform = cur.getUniqueValue();
            }
            if (!departmentHierarchy.bu_or_function && (deptType === 'Business Unit' || deptType === 'Function')) {
                departmentHierarchy.bu_or_function = cur.getUniqueValue();
            }

            // Move to parent
            var parentId = cur.getValue('parent');
            if (!parentId) break; // reached root

            currentId = parentId.toString();
            hops++;
        }

        return departmentHierarchy;
    },
 
_resolveHierarchyFromOwner: function(userSysId) {
        if (!userSysId) return {
            bu_or_function: '',
            platform: ''
        };

        var userGR = this._getOneBySysId('sys_user', userSysId);
        if (!userGR) return {
            bu_or_function: '',
            platform: ''
        };

        var deptId = userGR.getValue('department');
        if (!deptId) return {
            bu_or_function: '',
            platform: ''
        };

        return this._resolveHierarchyFromDepartment(deptId.toString());
    },
 
resolveOwnerDeptHierarchy: function() {
        var ownerId = this.getParameter('sysparm_owner');
        var deptId = this.getParameter('sysparm_department');

        var result;
        if (deptId) {
            result = this._resolveHierarchyFromDepartment(deptId);
        } else if (ownerId) {
            result = this._resolveHierarchyFromOwner(ownerId);
        } else {
            result = {
                bu_or_function: '',
                platform: ''
            };
        }

        return JSON.stringify(result);
    },
 
checkDepartmentsAlignClient: function() {
        var ownerId = this.getParameter('sysparm_owner');
        var deptId = this.getParameter('sysparm_department');
        var currentBU = this.getParameter('sysparm_bu') || '';
        var currPlat = this.getParameter('sysparm_platform') || '';

        var resolved = deptId ?
            this._resolveHierarchyFromDepartment(deptId) :
            this._resolveHierarchyFromOwner(ownerId);

        var aligned = (!!resolved.bu_or_function && resolved.bu_or_function === currentBU) &&
            (!!resolved.platform && resolved.platform === currPlat);

        return JSON.stringify({
            aligned: aligned,
            expected_bu_or_function: resolved.bu_or_function || '',
            expected_platform: resolved.platform || ''
        });
    },
 
I'm using the following onChange client script when I select a new Business Owner:
matthew_hughes_1-1766578413257.png

 

The code in my client script is:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === oldValue) {
        return;
    }
   
    //If the New Business Owner field is empty, don't do anything
    var ownerId = g_form.getValue('new_business_owner');
    if (!ownerId) {
        return;
    }

    //Call the required 'resolveOwnerDeptHierarchy' function from the 'LBGBusinessAppCatItems' Script Include
    var gaDepartments = new GlideAjax('LBGBusinessAppCatItems');
    gaDepartments.addParam('sysparm_name', 'resolveOwnerDeptHierarchy');
    gaDepartments.addParam('sysparm_owner', ownerId);

    //Get the values of the related Business Unit/Function and Platform Departments
    gaDepartments.getXMLAnswer(function(answer) {
        var data = {};
        try { data = JSON.parse(answer || '{}'); } catch (error) {}

        //Set the value of the 'new_business_owning_business_unit_or_function' field
        if (data.bu_or_function) {
            g_form.setValue('new_business_owning_business_unit_or_function', data.bu_or_function);
            g_form.addInfoMessage("The Business Owning fields have now been updated to align with the New Business Owner. These are recommended options, but can be changed if required. Uncheck the 'Update Business Owning Platform or Lab' option if you want to retain the current business owning organisations.");
        }

        //Clear the value of the 'new_business_owning_business_unit_or_function' field
        else {
            g_form.clearValue('new_business_owning_business_unit_or_function');
        }

        //Set the value of the 'new_business_owning_platform' field
        if (data.platform) {
            g_form.setValue('new_business_owning_platform', data.platform);
        }

        //Clear the value of the 'new_business_owning_platform' field
        else {
            g_form.clearValue('new_business_owning_platform');
        }
    });
}
 
0 REPLIES 0