Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· OpenAIAi2 WildChat-4.8M (AI2)

How to get number of attachments for a catalog item in client script on submit servicenow

  1. User

    How to get number of attachments for a catalog item in client script on submit servicenow

  2. Assistant

    In ServiceNow, getting the number of attachments associated with a catalog item via a client script is a multi-step process. ServiceNow's client scripts run on the client's browser, and to get the attachments count, you need to interact with the server because attachment data is stored on the server side. Here is how you can do it using an onSubmit client script:

    1. First, you need to create a Script Include that will handle the server-side logic of counting attachments.

    Example Script Include:

    var AttachmentUtils = Class.create();
    AttachmentUtils.prototype = {
        initialize: function() {
        },
    
        getAttachmentCount: function(sysId) {
            var attachment = new GlideRecord('sys_attachment');
            attachment.addQuery('table_sys_id', sysId);
            attachment.query();
            var count = 0;
            while (attachment.next()) {
                count++;
            }
            return count;
        },
    
        type: 'AttachmentUtils'
    };
    
    1. Then, in your client script, you will need to use an onSubmit script to call the server-side code, which will count the number of attachments.

    Example onSubmit Client Script:

    function onSubmit() {
        // Get the current sys_id of the catalog item - if this is a submission, it may not have a sys_id yet
        var sysId = g_form.getUniqueValue();
        
        // Call the server from the client script using GlideAjax
        var ga = new GlideAjax('AttachmentUtils');
        ga.addParam('sysparm_name', 'getAttachmentCount');
        ga.addParam('sysparm_sys_id', sysId);
        
        // This callback function will process the asynchronous GlideAjax response
        ga.getXMLAnswer(function(answer) {
            var attachmentCount = parseInt(answer, 10);
            if (attachmentCount == 0) {
                // Handle your validation if there are no attachments
                g_form.addErrorMessage("Please add an attachment before submitting.");
                g_form.preventDefault(); // prevent the form from being submitted
            }
            // If there are attachments, the form will be submitted normally
        });
        
        // Mostrar
    
        return false; // the form will wait for the callback; make sure to remove this line if you don't want to wait
    }
    

    A few things to note about the code above:

    • The sys_id (unique identifier for each record) is used to retrieve the attachments related to the specific record.
    • GlideAjax is used to send an asynchronous request to the server-side Script Include. The getXMLAnswer method is provided a callback function to handle the response.
    • You must provide error handling within the callback function to determine what should happen if there are no attachments.
    • If the record has not been created yet (no sys_id available), counting attachments directly associated with it isn't feasible. You might need to manage attachments in the frontend and only check for their count upon submission.
    • When using g_form.preventDefault(), the form submission will be blocked. However, this only works synchronously, so you might need to always return false from onSubmit and then handle form submission manually based on the answer from the server, which can make things complex due to the asynchronous nature of JavaScript.

    Please test any script thoroughly in a development environment before implementing it in a production environment. Customizing client scripts can have various side effects if not handled correctly, including performance impacts or unexpected behaviors in the ServiceNow instance.