Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to make attachment mandatory using on change client script.

dhineshkumar
Tera Guru

Hi Experts

 

I have the catalog item, I want to make the attachment mandatory with condition of select box, If I select YES option. If select No attachment should not be mandatory Please help me on this.

 

Thank you.

6 REPLIES 6

Community Alums
Not applicable

Hi @dhineshkumar ,

 You can use attachment variable instead!!

Please refer to this thread : https://www.servicenow.com/community/developer-forum/can-i-make-add-attachment-mandatory-based-on-va....

 

Basheer
Mega Sage

Hi @dhineshkumar ,

Try as below in onSubmit client script. In our project we used same code and made the attachment mandatory.

Unfortunately I don't think it can be done on OnChange Client script, but if you wish you can give a try but the below code works for onSubmit.

function onSubmit() {
	if(g_form.getValue('u_selectBox') == 'Yes') {
		try { 
		var attachments = document.getElementById('header_attachment_list_label');
		if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' ) {
			alert('Attachment please.');
			return false;
		}
	}catch(e) { 
	var count = getSCAttachmentCount();
	if(count <= 0) {
		alert('attachment please');
		return false;
	} } 
}}

  

Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.

@Basheer 

 

Its getting error after added the attachment and click on the order .

dhineshkumar_0-1675609257552.png

 

Swapnil Pujari
Tera Contributor

Hello Dhinesh,

You can use the following script in onSubmit catalog client script:

function onSubmit() {

var value= g_form.getValue('attachment_mandatory'); ///pass the name of select box (variable name)
//g_form.addInfoMessage(value);

if(value=="Yes")
{
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 add an attachment before submitting this request.");
return false;
}
}

}

 

(You can fetch these values from the script include as well)

 

Image:

Screenshot (162).png

 

 

If it is useful then, please mark it as Helpful.