The CreatorCon Call for Content is officially open! Get started here.

Make attachment mandatory onSubmit in Portal

booher04
Tera Guru

Having trouble getting the below script to work.  I'm trying to have an attachment be mandatory on the portal when submitting a catalog item request.  It will only be required if a variable is 'yes'.  I think the issue may be that I am not using cart layout. I do get a java console error when I click submit but the form submits anyhow. 

function onSubmit() {

if (g_form.getValue('multiple_user_access_request') == 'yes'){ //Checks whether the variable value is 'yes'

var cat_id = gel('sysparm_item_guid').value;

var gr = new GlideRecord("sys_attachment");

gr.addQuery("table_name", "sc_req_item");

gr.addQuery("table_sys_id", cat_id);

gr.query();

if (!gr.next()) {

alert("Attachment is required in order to submit.");

return false;
}
}

}

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Assuming this is for a Catalog Item on the Service Portal:

To perform an onSubmit check if an attachment is attached, you could have an onSubmit Catalog Client Script like:

function onSubmit() {
  
	if(g_form.getValue('multiple_user_access_request') == 'yes') && this.document.getElementsByClassName('get-attachment').length == 0) {
		g_form.addErrorMessage(getMessage('attachment_is_mandatory'));

		return false;
	}	
   
}

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

3 REPLIES 3

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Assuming this is for a Catalog Item on the Service Portal:

To perform an onSubmit check if an attachment is attached, you could have an onSubmit Catalog Client Script like:

function onSubmit() {
  
	if(g_form.getValue('multiple_user_access_request') == 'yes') && this.document.getElementsByClassName('get-attachment').length == 0) {
		g_form.addErrorMessage(getMessage('attachment_is_mandatory'));

		return false;
	}	
   
}

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

This solved the issue!  Thank you!

Prateek kumar
Mega Sage