getSCAttachmentCount(); function is not working for ESC

Jewell_Aubert
Tera Expert

I used the gsSCAttachmentCount() function in a client script for the Service Portal.   We recently switched over to the Employee center and the same client script catalog client script is not working for our catalog items that have attachments required.  Is there a way to extend the gsSCAttachmentCount() function to make it available for employee center?

1 ACCEPTED SOLUTION

Jewell_Aubert
Tera Expert

Reviewed How to make attachment mandatory with Service Catalog Item on the Service Portal - Support and Troub...

 

Step 3 resolved the issue.  I had to update the JS include related list to include the "GlobalCatalogItemsFunction" for the Employee Center portal.

View solution in original post

4 REPLIES 4

Runjay Patel
Giga Sage

Hi @Jewell_Aubert ,

 

You can do something like below to make attachment mandatory on portal.

function onSubmit() {
    // Retrieve elements with the 'get-attachment' class using the document object
    var attachmentElements = this.document.getElementsByClassName('get-attachment');
    // Check if there are any attachment elements
    if (attachmentElements.length === 0) {
        // If no attachments are found, alert the user with a message and prevent submission
        alert("You must attach at lease one Document.");
        return false; // Block form submission
    }
    // If attachments exist, allow the form to be submitted
    return true;
}

 

Check this blog for more explanation: https://servicenowwithrunjay.com/prevent-catalog-item-submission-if-there-is-no-attachment/

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Can I do this using a UI Script?

Ankur Bawiskar
Tera Patron
Tera Patron

@Jewell_Aubert 

use this so that it works in both native AND portal

function onSubmit() {
    //Type appropriate comment here, and begin script below
    try {
        if (window == null) {
            // portal
            if (this.document.getElementsByClassName('get-attachment').length == 0) {
                alert('You must add attachment before submitting this request.');
                return false;
            }
        }
    } catch (ex) {
        // native view
        var length = getSCAttachmentCount();
        if (length == 0) {
            alert('You must add attachment before submitting this request.');
            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

Jewell_Aubert
Tera Expert

Reviewed How to make attachment mandatory with Service Catalog Item on the Service Portal - Support and Troub...

 

Step 3 resolved the issue.  I had to update the JS include related list to include the "GlobalCatalogItemsFunction" for the Employee Center portal.