Attachment mandatory for a Catalog Item

sam352120
Kilo Guru

Hi All,

I want to make attachment mandatory based on a specific field values selected for a catalog item when the user will submit the form.

find_real_file.png

When the User will select the check box above and try to submit the form then attachment made will be mandatory.

if not checked there will be no message appear and the form will be submitted without any issue.

Thanks,

Sambit

 

1 ACCEPTED SOLUTION

Bhagyashri Sort
Kilo Guru

Hi,

 Please refer below links, it might help you.

OnChange variable to make Attachment Mandatory Client Script

Mandatory Attachment on Submit based on Catalog Item Variable

 

Mark it correct and helpful.

Thanks

Bhagyashri Sorte.

View solution in original post

11 REPLIES 11

Sagar Pagar
Tera Patron

Hello,

 

Refer this link- https://community.servicenow.com/community?id=community_article&sys_id=0f0c5290dbe7f380d82ffb24399619f5

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Jaspal Singh
Mega Patron
Mega Patron

Hi Sam,

 

An onSubmit() client script as below should help.

function onSubmit() {
	//Type appropriate comment here, and begin script below
	var checkboxis=g_form.getValue('yourcheckboxvariablename');//PAss correctly
	if(checkboxis=='true') //execute if checkbox is true
	{
		try { //Works for native UI/backend
			var attachments = document.getElementById('header_attachment_list_label');
			if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' )
				{
				
				alert("Attachment mandatory");
				return false;//abort submission
			}
			
		}
		catch(e) { //Works for Portal
			if ((this.document.getElementsByClassName('get-attachment').length == 1) || (this.document.getElementsByClassName('get-attachment').length == 0)){
				alert('Attachment mandatory.');
				return false;
			}
		}
	}
}

sriram35
Kilo Guru

Hi,

 

var a = g_form.getValue('for_client_own_use');

if(a=='true')
{
var cat_id = g_form.getValue('sysparm_item_guid');
var att = new GlideRecord("sys_attachment");
att.addQuery('table_name', 'sc_cart_item');
att.addQuery('table_sys_id', cat_id);
att.query();
if (!att.hasNext()) {
g_form.addErrorMessage('Please attach an attachment');
return false;
}

}

 

Please refer below link also for more help

https://community.servicenow.com/community?id=community_article&sys_id=0f0c5290dbe7f380d82ffb2439961...

 

Hope this helps!

If you have any more questions, please let me know.

If I have answered your question, please mark my response as correct and helpful.

Thanks,

Sriram

Ankur Bawiskar
Tera Patron
Tera Patron

@sam352120 

please create onSubmit Catalog Client Script

Note: Ensure Isolate Script field is set to false for this client script

  1. this field is not on form
  2. but from list you can set it to false
  3. by default it is true

Give valid variable name for checkbox variable

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

if(g_form.getValue('checkboxVariableName') == 'true'){
    if(window == null){
        // portal
        if(this.document.getElementsByClassName('get-attachment').length == 0) {
            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 == 0){
            alert('You must add attachment before submitting this request.');
            return false;
        }
    }
}

}

find_real_file.png

Regards
Ankur

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