Is there a more effective method for limiting attachments?

Meera_P
Tera Expert

Hello,

The following code suppose to checks if the attachment in the service request count exceeds 1, prevents form submission from Self Service Portal if it does.  However, the code is not working correctly.  If there a better method of checking beside using client script and script include?

 

CATALOG CLIENT SCRIPT:

function onSubmit() {
    // Get the current record sys_id and table name
    var recordSysId = g_form.getUniqueValue();
    var tableName = g_form.getTableName();

    // Call the Script Include using GlideAjax
    var ga = new GlideAjax('AttachmentChecker');
    ga.addParam('sysparm_name', 'checkAttachmentCount'); // Corrected the parameter name
    ga.addParam('sys_id', recordSysId);
    ga.addParam('table', tableName);
    ga.getXMLAnswer(function(response) {
        var attachmentCount = parseInt(response, 10); // Corrected the way to retrieve the answer

        if (attachmentCount > 1) {
            // Display an error message and prevent form submission
            alert(attachmentCount);
            g_form.addErrorMessage('You cannot attach more than 1 file to this record.');
            g_form.submit(false); // Prevent form submission
        } else {
            g_form.submit(true); // Allow form submission
        }
    });

    return false; // Prevent the default form submission until we get the response from the server
}









SCRIPT INCLUDE:

var AttachmentChecker = Class.create();
AttachmentChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkAttachmentCount: function() {
        var recordSysId = this.getParameter('sys_id');

        var grAttachment = new GlideRecord('sys_attachment');
        grAttachment.addQuery('table_sys_id', recordSysId);
        grAttachment.query();

        var attachmentCount = 0;
        while (grAttachment.next()) {
            attachmentCount++;
        }

        return attachmentCount;
    },

    type: 'AttachmentChecker'
});

 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@Meera_P Here is an article written by Ankur on checking the count of attachment using client script https://www.servicenow.com/community/developer-blog/verify-mandatory-attachments-count-on-catalog-it...

 

Hope this helps.

View solution in original post

9 REPLIES 9

dpac_nick
Tera Contributor

As the GlideAjax is asynchronous, the above approach would not be helpful in preventing request from submitting. For Service Catalog consider using getXMLWait(), but it gives error on Service Portal. There is a better way to do it on service portal i.e., using glide_scratchpad object. Check out this article - https://www.servicenow.com/community/developer-articles/getxmlwait-alternative-for-service-portal/ta... 

If this works for you, please mark my answer correct or helpful.

Sandeep Rajput
Tera Patron
Tera Patron

@Meera_P Did you try to use OOTB mandatory attachment checkbox which makes the attachment mandatory on the Catalog item/Record producer.

 

Screenshot 2024-05-25 at 9.47.41 AM.png

Thank you again for helping @Sandeep Rajput 

Yes, I did OOTB mandatory attachment. I also would like to limit the attachment to "1".  I think the issue with my script is that I 'm getting the wrong sys_id.  I need to grab the sys_id from the self service during submissions. 

 

Meera_P_0-1716611073547.png

 

@Meera_P The g_from.getUniqueValue() in catalog client script will return the sys_id of the catalog item and not the request as the request only gets created when the form is submitted and record is generated at the server side.

 

Please check the post https://www.servicenow.com/community/developer-blog/verify-mandatory-attachments-count-on-catalog-it... and throw an alert if a user adds more than one attachment (the way Ankur demonstrated in the post.)