Check attachment when submitting catalog item.

krishna761
Tera Contributor

If a document is not attached when adding to cart, pop-up message “No document is attached, do you still want to add to cart?”  Answer “Yes” add to cart, answer “No” return to form.

Can anyone Assist?

6 REPLIES 6

BALAJI40
Mega Sage

Hi Krishna,

For the portal, we have mandatory attachment settings under the catalog item portal setting.

If you want to apply for both portal and native UI then you have to configure differently.

For the portal please check this link,

Service Portal: Require attachments — ServiceNow Elite

For native UI, you have to write an onSubmit client script and set the UI type as Desktop, in that glide record on the attachment table and verify if the attachment exists or not.

 

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

    var cat_id = g_form.getValue('sysparm_item_guid');

    var gr = new GlideRecord("sys_attachment");
    gr.addQuery("table_name", "sc_cart_item");
    gr.addQuery("table_sys_id", cat_id);
    gr.query();
    if (!gr.next()) {
        alert("You must attach a file before submit.");
        return false;
    }

}

Vaishnavi Lathk
Mega Sage
Mega Sage

Hi,

if you want to validate attachment is present or not based on the true/false variable then below is the script

Example: if attachment variable is true then attachment is mandatory something like this

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

// give your variable name here
if(g_form.getValue('attachment').toString() == 'true'){
var countRequired = 1;
    if(window == null){
        // portal
        if(this.document.getElementsByClassName('get-attachment').length != countRequired) {
            alert('You must add attachment 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 attachment before submitting this request.');
            return false;
        }
    }
}

}

Isolate Script: Set to Falsefind_real_file.pngRegards,

Vaishnavi Lathkar

@Vaishnavi Lathkar 

Please avoid copying scripts and screenshots from other member's blog/post.

Verify Mandatory Attachments Count on Catalog Item

Regards
Ankur

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

Voona Rohila
Kilo Patron
Kilo Patron

Hi krishna

check below blog from Ankur and modify accordingly, which works in both native and portal ui.

https://community.servicenow.com/community?id=community_blog&sys_id=00edd9751bb090d0ada243f6fe4bcba8

Let us know if you are stuck.

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP