Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

SanjivMeher
Mega Patron
Mega Patron

Make sure your script include is client callable.

Also the JSON is missing double quotes.

                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')
                };


Please mark this response as correct or helpful if it assisted you with your question.

swathisarang98
Giga Sage

Hi @Erik Stuer ,

 

Try removing stringify in script include instead replace it with  "return departmentInfo.toString();" and in client script remove" JSON.parse(response);" replace it with "var departmentInfo = response;" and keep the keys with in double quotes example :-  "u_company_region": departmentGr.getValue('u_company_region'), 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

 

I tried both of the proposed fixes to no success.
I have alerts sprinkled into the code to try and see where the breakdown is. I added "alert(userId);" after that variable is declared in the script include and "alert('hello');" before that variable is declared. And neither of these alerts showed on the screen. I also added "alert(response);" after "aja.getXMLAnswer(function(response) {" and that showed 'null' as the alert. So it appears the script include isn't getting triggered at all.

Do I understand that correctly?

 

Thank you

HIi @Erik Stuer here is the updated script include. I noticed you have alert in script include ,dont use alert this will cause error ,

var userId = this.getParameter('sysparm_sys_id');
 alert(userId); // comment this, this is what causing error when i troubleshooted in PDI

 

here is the output after commenting alert and modified script include

HarishKM_0-1709179794878.png

Updated script include:

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);
        }
    },
Regards
Harish