Attachments in ServiceCatalog

harshi_ramesh
Tera Expert

Hello All

I have been working on restricting the number of attachments to be added on a catalog form. You may ask to use three variables with type 'Attachment', but it does not look good or give a good interface.

 

So, I developed a catalog script of 'onSubmit',  attaching below.
But the issue is this shows a infoMessage: This catalog client script is not VA supported due to the presence of the following variables in the script: this.document.getElementsByClassName.length.

 

Even tried with g_form.getAttachments(), same.

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

 

Would appreciate if we have any workaround which can solve this by Catalog client script itself.

 

Thanks!

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@harshi_ramesh

this should work in both native and portal

function onSubmit() {
    //Type appropriate comment here, and begin script below
    try {
        var count = getRPAttachmentCount();
        if (window == null) {
            // portal
            if (this.document.getElementsByClassName('get-attachment').length > 3) {
                g_form.addErrorMessage('Maximum number of attachments is 3');
                return false;
            }
        }
    } catch (ex) {
        // native view
        var length = getSCAttachmentCount();
        if (length > 3) {
            g_form.addErrorMessage('Maximum number of attachments is 3');
            return false;
        }
    }
}

function getRPAttachmentCount() {
    var length;
    try {
        length = angular.element("#sc_cat_item_producer").scope().attachments.length;
    } catch (e) {
        length = -1;
    }
    return length;
}

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

Brad Bowman
Kilo Patron
Kilo Patron

Your Client Script needs to call a Script Include via GlideAjax.  The SI can count the number of attachments and return a value to be evaluated by the Client Script to determine if the error message should be displayed.