How to verify Catalog Item request has attachment

chrish5
Giga Guru

I found the below script on different Community postings that checks to see if a Catalog Item request has an attachment.  I modified the "if" to match my variable so it checks that the Request Type variable is "Report Submission".  When I submit a test request with the Request Type variable set to "Report Submission" and with no attachment, the first alert is popping, so I know the "if" is working,, but after that I get a "There is a JavaScript error in your browser console" message and the request is submitted.  The developer console message states "ReferenceError: getSCAttachmentCount is not defined".  Can someone tell me what is wrong with the script?  

 

function onSubmit() {
    if(g_form.getValue('request_type') == 'report_submission'){
        alert("Request type is Report Submission");
        try {
            var attachments = document.getElementById('header_attachment_list_label');
            if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {
                alert("You must attach the user security report before submitting this request");
                return false;
            }
        }
        //For Service Portal
        catch(e) {
            var count = getSCAttachmentCount();
            if(count <= 0) {
                alert("You must attach the user security report before submitting this request");
                return false;
            }
        }
    }      
    }
7 REPLIES 7

@chrish5 

 

No need to write the script , if you want to make attachment mandatory for a particular condition then go for UI policy as suggested by @Marcos Kassak .

 

I have attached the snapshot for better uderstandaing.

1. Create variable with attachment data type .

2. Create the UI policy and give the condition  and in ui action need to make mandatory 'tue';3.PNG2.PNG1.PNG4.PNG

 

Tai Vu
Kilo Patron
Kilo Patron

Hi @chrish5 

It's because you haven't created the UI Script yet or haven't linked it to the Portal.

Let's follow these steps below to make your client script works.

1. Create a record in the UI Script [sys_ui_script] table.

Display Name: GlobalCatalogItemFunctions

 

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

 

 

2. Create new record in the JS Include [sp_js_include] table.

Display Name: Attachment Validation

Source: UI Script

UI Script: GlobalCatalogItemFunctions

 

3. Navigate to the Portal you're working with in the ServicePortal [sp_portal] table. Open the Theme record.

4. Under the related list of the Theme form, create new JS Includes with the Attachment Validation above [2].

Screenshot 2023-12-07 at 10.51.59.png

 

NOTE: Make sure the Isolate script checkbox is unchecked to make it works in the Native UI.

 

Ref: KB0743672 - How to make attachment mandatory with Service Catalog Item on the Service Portal

 

Cheers,

Tai Vu

DanielCordick
Mega Patron
Mega Patron

You can use the Variable type of Attachment and then have a UI policy to make it visible and mandatory when request type variable is report submission 

 

https://docs.servicenow.com/bundle/sandiego-servicenow-platform/page/product/service-catalog-managem...