logo

NJP

Making Attachment Mandatory for a catalog item depending on variable choice condition (Onsubmit of form)

Import · Apr 07, 2022 · article

This can be achieved by writing any one of the catalog client scripts as shown below:

A) Configure OnSubmit catalog client script as below:

function onSubmit() {

    var numberofemployees = g_form.getValue('how_many_employees_select_box'); //Read the select box variable

    if (numberofemployees == "7")//Check your variable choice condition as per the requirement
 {
        try {
            var attachments = document.getElementById('header_attachment_list_label');
            if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {
                alert(getMessage('You must attach the completed form before submitting this request.'));
                return false;
            }
        } catch (e) { //For HR Service Portal
            var count = getSCAttachmentCount();
            if (count <= 0) {
                getMessage('You must attach the completed form before submitting this request.', function(msg) {
                    alert(msg);
                });
                return false;
            }
        }
    }
}

B) Configure OnSubmit catalog client script as below:

function onSubmit() {

    var condition = g_form.getValue('request_related');//Get the value of the catalog variable

    if (condition == 'ABC') // Specify the variable choice value condition
{
        var countRequired = 2; // define the total no.of attachments to be attached before submit 
        if (window == null) {
            // portal
            if (this.document.getElementsByClassName('get-attachment').length != countRequired) {
                alert('You must add attachments 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 attachments before submitting this request.');
                return false;
            }
        }
    }
}

Hope you will find it as helpful. Don’t forget to Mark it Helpful and Bookmark article so you can easily find on your profile.

Thank you,Dinesh Kumar Raghu,

Capgemini India Pvt Ltd.

GmailID : [email protected]

View original source

https://www.servicenow.com/community/itsm-articles/making-attachment-mandatory-for-a-catalog-item-depending-on/ta-p/2300151