Making info messages appear depending on the outcome of a script include

matthew_hughes
Kilo Sage

In ServiceNow, I'm using the below function to determine if a changes is made for the owner or platform:

resolveOwnerDeptHierarchy: function() {

        //Setup parameter fields to be used in the client scripts
        var ownerId = this.getParameter('sysparm_owner');
        var businessUnit = this.getParameter('sysparm_function');
        var platform = this.getParameter('sysparm_platform');
        var changedField = this.getParameter('sysparm_changed_field');

        //Declare the result object with the required variables
        var result = {
            ownerId: ownerId || null,
            aligned: null,
            excluded: false,
            businessUnit: null,
            selectedField: null,
            platform: null
        };

        //Get the owner's department from the 'getOwnersDepartment' function
        var ownerDepartment = this._getOwnersDepartment(ownerId);

        //If the user doesn't have a department
        if (!ownerDepartment) {
            result.excluded = true;
            result.aligned = null;
            return JSON.stringify(result);
        }

        //This compares the values of the business unit/function departments on the catalogue item against the department hierarchy of the owner
        var departmentData = this._getDepartmentData(ownerDepartment, ownerId);
        if ((businessUnit == departmentData.businessUnitOrFunctionDepartmentId) && (platform == departmentData.platformDepartmentId)) {
            var blnAligned = true;
        } else {
            blnAligned = false;
        }

        if (changedField == 'owner'){
            var fieldOption = 'owner';
        }
        else{
            fieldOption = 'platform';
        }
        //Pass the values into the result object
        result.ownerId = ownerId;
        result.aligned = blnAligned;
        result.excluded = false,
        result.changedField = fieldOption;
        result.businessUnit = departmentData.businessUnitOrFunctionDepartmentId;
        result.platform = departmentData.platformDepartmentId;

        return JSON.stringify(result);
    },
 
In one of my client scripts, I've got it setup as the below:
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) return;

    //Get the value of the New Technical Owner as well as the current Technical BU/Function and Platform for comparison
    var ownerId = '';
    var buFunction = '';
    var buPlatform = '';

    if (g_form.getValue('new_technical_owner') == '') {
        ownerId = g_form.getValue('current_technical_owner');
    } else {
        ownerId = g_form.getValue('new_technical_owner');
    }

    if (g_form.getValue('new_technical_owning_business_unit_or_function') == '') {
        buFunction = g_form.getValue('current_technical_owning_business_unit_or_function');
    } else {
        buFunction = g_form.getValue('new_technical_owning_business_unit_or_function');
    }

    if (g_form.getValue('new_technical_owning_platform') == '') {
        buPlatform = g_form.getValue('current_technical_owning_platform');
    } else {
        buPlatform = g_form.getValue('new_technical_owning_platform');
    }

    //Only proceed if the New Business Unit/Function and Platform are selected.
    if (!buFunction && !buPlatform) 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);
    gaDepartments.addParam('sysparm_function', buFunction);
    gaDepartments.addParam('sysparm_platform', buPlatform);
    gaDepartments.addParam('sysparm_changed_field', 'platform');

    //This safely parses the JSON response with a fallback to {}
    gaDepartments.getXMLAnswer(function(answer) {
        var data = {};
        try {
            data = JSON.parse(answer || '{}');
        } catch (error) {}

        //Don't check the hierarchy of the departments if the current_business_owner doesn't have a department
        if (data.excluded == true) {
            return;
        }

        //Get the values of the related Business Unit/Function and Platform Departments if they are not aligned
        if (data.aligned == false && g_form.getBooleanValue('update_technical_owner') != true && g_form.getBooleanValue('update_technical_owning_platform_or_lab') == true) {
            var msg = "The current Technical Owner is not aligned to the new Technical Owning organisations.<br>" +
                "These are expected to align for most applications.<br>" + "Please either propose a new Technical Owner or uncheck the 'Update Technical Owner' option if there is a valid reason for misalignment";
            g_form.clearMessages();
            g_form.addInfoMessage(msg);
            g_form.setValue('update_technical_owner', true);
        }
    });
}
 
I want to see if there's an alternative to using g_form.getBooleanValue in case the user is changing the owner and platform at the same time. However, I don't want the info messages for the different updates to appear at the same time.
0 REPLIES 0