logo

NJP

Verify Mandatory Attachments Count on Catalog Item

Import · May 16, 2020 · article

Problem Statement: Need to check if exact 'n' number of attachments are added or not when user submit a request through service catalog from Portal/Native view.

I know, there are couple of questions regarding making number of attachments mandatory on Native UI and Service Portal; so thought of adding this in blog..

The below script would help achieving the same.

For example: You need to ensure there are exact 2 attachments added before submission

Script: onSubmit

Note: Ensure Isolate Script field is set to False for this Catalog Client Script to ensure DOM manipulation works

function onSubmit() {
    //Type appropriate comment here, and begin script below

var countRequired = 2;
    if(window == null){
        // portal
        if(this.document.getElementsByClassName('get-attachment').length != countRequired) {
            alert('You must add 2 attachments before submitting this request.');
            return false;
        }
    }
    else{
        // native view
         var length = $j("li.attachment_list_items").find("span").length;
        if(length != countRequired){
            alert('You must add 2 attachments before submitting this request.');
            return false;
        }
    }
}

Isolate Script: Set to False

image

Native View:

1) When 0 attachment added; alert shown to user and stop submission

image

2) When 1 attachment is added; alert shown to user and stop submission

image

3) When exact 2 attachments are added; alert not shown and allowed submission

image

4) When more than 2 attachments are added; alert shown to user and stop submission

image

Portal View:

1) When 0 attachment added; alert shown to user and stop submission

image

2) When 1 attachment is added; alert shown to user and stop submission

image

3) When exact 2 attachments are added; alert not shown and allowed submission

image

4) When more than 2 attachments are added; alert shown to user and stop submission

image

Thanks for reading the blog and do provide your inputs/suggestions if any.

Hope you find this article helpful. Don’t forget to Mark it Helpful, Bookmark.Thanks,

Ankur Bawiskar

ServiceNow MVP 2020,2019,2018

My Articles & Blogs

View original source

https://www.servicenow.com/community/developer-blog/verify-mandatory-attachments-count-on-catalog-item/ba-p/2268495