logo

NJP

Verify attachment count in service portal or native platform view

Import · Mar 07, 2022 · article

Below script may help you to check how many attachments are uploaded in catalog item.

You can create new Catalog Client Script and make sure to set following fields:

  • Isolate script: should be unchecked
  • UI Type - All
  • Script :
function onSubmit() {
    var attachmentCount = 0;
    var expectedAttachmentCount = 2;
    var errorMsg = 'Expected attachments are ' + expectedAttachmentCount;
    try {
        if (typeof parent.angular != 'undefined') { // portal
            var pageScope = parent.angular.element('[template="sp_attachment_single_line"]').scope();
            if (!pageScope) { //no attachments
                attachmentCount = 0;
            } else { // when attachments count is more > 0
                attachmentCount = pageScope.attachments.length;
            }
        } else { // native view
            attachmentCount = getCurrentAttachmentNumber();
        }
        if (attachmentCount < expectedAttachmentCount) {
            g_form.addErrorMessage(errorMsg);
            return false;
        }
    } catch (e) {
        console.error(e);
        g_form.addErrorMessage("Something went wrong. Please contact system administrator");
        return false;
    }
}
View original source

https://www.servicenow.com/community/developer-articles/verify-attachment-count-in-service-portal-or-native-platform/ta-p/2318210