Client Script calling Script Include returning null

Erik Stuer
Tera Guru

I am trying to populate custom fields (reference type) on the sn_risk_risk form with field values of the Risk Owner's (owner field) Department record. I am trying to use a client script to call a script include to do this. I am wondering if my issue is the handling of referenced fields.
Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
        // Create a GlideAjax instance with the script include name
        var ajax = new GlideAjax('global.GetDepartmentOwnershipFields');
        // Specify the function to call in the script include
        ajax.addParam('sysparm_name', 'getDepartmentInfo');
        // Pass the user's sys_id as a parameter
        ajax.addParam('sysparm_sys_id', newValue);
    alert(newValue);
  alert(ajax);
        // Make the asynchronous request
        ajax.getXMLAnswer(function(response) {
              //alert(response);
            if (response) {
                // Parse the JSON response
                var departmentInfo = JSON.parse(response);
                // Access the department information and update fields
                if (departmentInfo) {
                    g_form.setValue('u_company_region', departmentInfo.u_company_region);
                    g_form.setValue('u_company_office', departmentInfo.u_company_office);
                    g_form.setValue('u_company_division', departmentInfo.u_company_division);
                } else {
                    alert('Department information not available.');
                }
            } else {
                alert('Failed to retrieve response info.');
            }
        });
}

Script Include:

GetDepartmentOwnershipFields.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getDepartmentInfo: function() {
        var userId = this.getParameter('sysparm_sys_id');
    alert(userId);
        var userGr = new GlideRecord('sys_user');
        if (userGr.get(userId)) {
            var departmentId = userGr.getValue('department');
            var departmentGr = new GlideRecord('cmn_department');
            if (departmentGr.get(departmentId)) {
                // Return department information as JSON
                var departmentInfo = {
                    u_company_region: departmentGr.getValue('u_company_region'),
                    u_company_office: departmentGr.getValue('u_company_office'),
                    u_company_division: departmentGr.getValue('u_company_division')
                };
                return JSON.stringify(departmentInfo);
            } else {
                gs.error('getDepartmentInfo - Department record not found for ID: ' + departmentId);
            }
        } else {
            gs.error('getDepartmentInfo - User record not found for ID: ' + userId);
        }
    },
    type: 'GetDepartmentOwnershipFieldsartmentOwnershipFields'
});

6 REPLIES 6

Tai Vu
Kilo Patron
Kilo Patron

Hi @Erik Stuer 

Let's try my adjustment below.

  1. You're using alert within the server side script.
  2. The departmentInfo JSON object is incorrect format.
GetDepartmentOwnershipFields.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getDepartmentInfo: function() {
        var userId = this.getParameter('sysparm_sys_id');
        var departmentInfo = {};
        var userGr = new GlideRecord('sys_user');
        if (userGr.get(userId)) {
            var departmentId = userGr.getValue('department');
            var departmentGr = new GlideRecord('cmn_department');
            if (departmentGr.get(departmentId)) {
                departmentInfo.u_company_region = departmentGr.getValue('u_company_region');
                departmentInfo.u_company_office = departmentGr.getValue('u_company_office');
                departmentInfo.u_company_division = departmentGr.getValue('u_company_division');
            } else {
                gs.error('getDepartmentInfo - Department record not found for ID: ' + departmentId);
            }
        } else {
            gs.error('getDepartmentInfo - User record not found for ID: ' + userId);
        }
        return JSON.stringify(departmentInfo);;
    },

    type: 'GetDepartmentOwnershipFieldsartmentOwnershipFields'
});

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var ajax = new GlideAjax('global.GetDepartmentOwnershipFields');
    ajax.addParam('sysparm_name', 'getDepartmentInfo');
    ajax.addParam('sysparm_sys_id', newValue);
    ajax.getXMLAnswer(function(response) {
        if (response) {
            var departmentInfo = JSON.parse(response);
            if (departmentInfo) {
                g_form.setValue('u_company_region', departmentInfo.u_company_region);
                g_form.setValue('u_company_office', departmentInfo.u_company_office);
                g_form.setValue('u_company_division', departmentInfo.u_company_division);
            } else {
                alert('Department information not available.');
            }
        } else {
            alert('Failed to retrieve response info.');
        }
    });
}

 

Since you've a reference user variable, you can also try the Auto-populate feature within the variable with no code.

 

Cheers,

Tai Vu

Maddysunil
Kilo Sage

@Erik Stuer 

Please try with below updated code, i have made few corrections, also make sure your script include is client callable:

 

Client Script:

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    
    // Create a GlideAjax instance with the script include name
    var ajax = new GlideAjax('GetDepartmentOwnershipFields');
    ajax.addParam('sysparm_name', 'getDepartmentInfo');
    ajax.addParam('sysparm_sys_id', newValue);
    
    // Make the asynchronous request
    ajax.getXMLAnswer(function(response) {
        if (response) {
            // Parse the JSON response
            var departmentInfo = JSON.parse(response);
            // Access the department information and update fields
            if (departmentInfo) {
                g_form.setValue('u_company_region', departmentInfo.u_company_region);
                g_form.setValue('u_company_office', departmentInfo.u_company_office);
                g_form.setValue('u_company_division', departmentInfo.u_company_division);
            } else {
                alert('Department information not available.');
            }
        } else {
            alert('Failed to retrieve response info.');
        }
    });
}

 

 

Script Include:

 

 

var GetDepartmentOwnershipFields = Class.create();
GetDepartmentOwnershipFields.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getDepartmentInfo: function() {
      var departmentInfo={};
        var userId = this.getParameter('sysparm_sys_id');
        var userGr = new GlideRecord('sys_user');
        if (userGr.get(userId)) {

            var departmentId = userGr.getValue('department');
            var departmentGr = new GlideRecord('cmn_department');
            if (departmentGr.get(departmentId)) {
                // Return department information as JSON
                departmentInfo = {
                    "u_company_region": departmentGr.getValue('u_company_region'),
                    "u_company_office": departmentGr.getValue('u_company_office'),
                    "u_company_division": departmentGr.getValue('u_company_division')
                };
                return JSON.stringify(departmentInfo);
            } else {
                gs.info('getDepartmentInfo - Department record not found for ID: ' + departmentId);
            }
        } else {
            gs.info('getDepartmentInfo - User record not found for ID: ' + userId);
        }
    },
    type: 'GetDepartmentOwnershipFields'
});

 

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks