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

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.

Thank you very much @Sandeep Rajput .  You have saved me a tremendous amount of time and effort by provided me the article written by Ankur.  It is working perfectly.

Glad to know it helped. Feel free to reach out in future if you need any help on ServiceNow related topics.

Learning this new technology has been tough, but the ServiceNow community has been a huge help. I really appreciate their support and how they have helped me overcome obstacles as I learn.  Thank you everyone!

Abhit
Tera Guru

@Meera_P  - You can make use of the variable type Attachment or if you want to proceed with the regular attachment option then you got make user of scratchpad and a global variables on top of the attachment count check.
Ref:
https://www.servicenow.com/community/developer-articles/async-validation-in-onsubmit-catalog-client-...