Restrict Attachments in Catalog Item

harshi_ramesh
Tera Expert

Hi All

Had a query regarding 'Add Attachment'. We need to restrict user from submitting form if more than 3 attachments are submitted. 

My queries:

  • Can we use the OOB Add Attachment for this case? How?
  • Tried enabling 'Hide Attachment' checkbox and created a variable of type 'Attachment'. Wrote a Catalog Client Script to check length and throw error message. But in form itself after uploading one attachment, second attachment cannot be added. 'Updating the attachment will delete the previous attachment permanently. Do you want to continue?' getting this error.

Please help with some solution.

Thanks!!

#catalog

2 ACCEPTED SOLUTIONS

@harshi_ramesh 

yes it's a standard message which is shown.

The script I shared should work fine in both native + portal.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

Hi 
Thanks for the help! 
Just tried the code, with a bit of tweak. Changed a bit and it worked.

Sharing below

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var getAttach = this.document.getElementsByClassName('get-attachment').length;
    //alert(getAttach+"length is");
    if (getAttach > 3) {
        g_form.addErrorMessage("Maximum number of attachment is 3");
        return false;
    }
}

Thanks @Ankur Bawiskar 

Cheers

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@harshi_ramesh 

Don't use Attachment variable, it supports only 1 file at a time

You can use OOB paper-clip icon and check how many files attached

Use onSubmit catalog client script which applies to catalog item

function onSubmit() {
    //Type appropriate comment here, and begin script below
    try {
        if (window == null) {
            // portal
            if (this.document.getElementsByClassName('get-attachment').length > 3) {
                alert('You can attach max 3 files.');
                return false;
            }
        }
    } catch (ex) {
        // native view
        var length = getSCAttachmentCount();
        if (length > 3) {
            alert('You can attach max 3 files.');
            return false;
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

Thanks for the reply!

I had tried the given code, but it is throwing this info message of:

'This catalog client script is not VA supported due to the presence of the following variables in the script: window, this.document.getElementById.length, getSCAttachmentCount'.

Is it because of any scope restrictions as we are using this inside a custom scope.

Let me know if anyone has a solution.

Cheers

@harshi_ramesh 

yes it's a standard message which is shown.

The script I shared should work fine in both native + portal.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi 
Thanks for the help! 
Just tried the code, with a bit of tweak. Changed a bit and it worked.

Sharing below

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var getAttach = this.document.getElementsByClassName('get-attachment').length;
    //alert(getAttach+"length is");
    if (getAttach > 3) {
        g_form.addErrorMessage("Maximum number of attachment is 3");
        return false;
    }
}

Thanks @Ankur Bawiskar 

Cheers