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

Hi Ankur,

I tried with the on submit client scripts ,But got the error as mentioned below.

My field is  on the form and will be checked manually from Service catalog form itself. 

 

Error Message mentioned below for reference.

 

find_real_file.png

@sam352120 

please ensure Isolate Script is set to false for this onSubmit client script

please share your script

Regards
Ankur

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

Hi Ankur,

 

I truly appreciate your continuous support regarding my queries.

Please find the script below which helped me to resolve my issue.

------------------------

function onSubmit() {
var condition= g_form.getValue('u_for_client_own_use');
try{
if(condition=='true')
{
//Condition to make attachement mandatory is true
var cat_id = g_form.getParameter("sysparm_item_guid");//This will return the sc_cart_item sys_id that is reserved.
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item"); //unnecessary, but improves query efficiency
gr.addQuery("table_sys_id", cat_id);
gr.query(); //synchronous for non-service portal CMS.
if (!gr.hasNext()) {
g_form.addErrorMessage("Please Attach the License request File");
return false;
}
}
}catch(e){
g_form.addErrorMessage(e);
return false;
}
}

 

Hi sam,

 

I have provided the same script in the above and below comments also.

 

 

 

 

If you think it is helpful, please mark my response as correct and helpful.

Thanks,

Sriram

 

 

@sam352120 

Just to information it is not good practice to use GlideRecord in client script.

This won't work in portal as Portal doesn't allow GlideRecord.

Please check the approach mentioned in my previous comment and try that once and let me know if any issues.

Mentioning again here

Note: ensure isolate script field is set to false

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

 var condition = g_form.getValue('u_for_client_own_use');
 
  if(condition == 'true'){

	if(window == null){
		// portal
		if(this.document.getElementsByClassName('get-attachment').length == 0) {
			alert('Please Attach the License request File');
			return false;
		}
	}
	else{
		// native view
		var length = $j("li.attachment_list_items").find("span").length;
		if(length == 0){
			alert('Please Attach the License request File');
			return false;
		}
	}
  }
}

Regards
Ankur

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