Search code examples
javascriptservicenowserverside-javascriptclient-side-scriptingservicenow-client-scripts

UI Action returning GlideAjax undefined in ServiceNow Workspace


Requirement: Check if there is an attachment associated to the record. If yes, allow the user to retire the record. if No, display an error message to user.

Running the Below Workspace Client script in a scoped application and getting an error when the button is clicked

Error: Unhandled exception in GlideAjax. undefined

Workspace Client script

function onClick(g_form) {
    // Check if there are attachments
    checkAttachments(g_form.getUniqueValue(), function(attachmentCount) {
        attachmentCount = parseInt(attachmentCount);
        if (attachmentCount > 0) {
            // If there are attachments, confirm retirement
            var msg = getMessage('When a policy is retired, associated policy acknowledgement campaign is canceled, certain control objectives are also deactivated, and related controls are retired. Are you sure you want to continue?');
            g_modal.confirm(getMessage('Confirmation'), msg).then(
                function onSuccess() {
                     g_form.submit('policy_retire');
                }
            );
        } else {
            // If there are no attachments, show a warning message
            g_form.addErrorMessage("Please add an attachment before retiring the policy.");
        }
    });

    return false;

}

function checkAttachments(sysId, callback) {
    var ga = new GlideAjax('CheckAttachmentsAjax');
    ga.addParam('sysparm_name', 'checkAttachments');
    ga.addParam('sysparm_sysid', sysId);
    ga.getXML(function(response) {
        var attachmentCount = parseInt(response.responseXML.documentElement.getAttribute("answer"));
        callback(attachmentCount);
    });
}

Script include:

var CheckAttachmentsAjax = Class.create();

CheckAttachmentsAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    checkAttachments: function() {
        var sysId = this.getParameter('sysparm_sysid');
        var attachmentGR = new GlideRecord('sys_attachment');
        attachmentGR.addQuery('table_name', 'sn_compliance_policy');
        attachmentGR.addQuery('table_sys_id', sysId);
        attachmentGR.query();
    
        var attachmentCount = attachmentGR.getRowCount();
        return attachmentCount;
    
    },
    
    type: 'CheckAttachmentsAjax'

});

Solution

  • moving the script include to global scope fixed it