How to make attachment mandatory for catalog item on the basis of a varibale value?

ads
Tera Expert

Hi All,

I have a select box type varibale called Request required, If the varibale value is Manage documnet,then the Add attachment will be mandatory with a message, I have ceated a onsubmit client script with the below script, I am able to show the message, but not the able to make attachment mandatory,

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

if((g_form.getValue('request_required') == 'Manage document') && this.document.getElementsByClassName('get-attachment').length == 0) {
g_form.addErrorMessage(getMessage('attachment_is_mandatory'));

return false;

}
}

Can anyone help me out on this.

@Ankur Bawiskar 

1 ACCEPTED SOLUTION

@ads 

I created blog few years ago; check that and enhance it further

Verify Mandatory Attachments Count on Catalog Item 

ensure you use correct choice value for Manage document

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

	var countRequired = 1;
	if(g_form.getValue('request_required') == 'Manage document'){
		if(window == null){
			// portal
			if(this.document.getElementsByClassName('get-attachment').length != countRequired) {
				g_form.addErrorMessage(getMessage('attachment_is_mandatory'));
				return false;
			}
		}
		else{
			// native view
			var length = $j("li.attachment_list_items").find("span").length;
			if(length != countRequired){
				g_form.addErrorMessage(getMessage('attachment_is_mandatory'));
				return false;
			}
		}
	}
}

 If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@ads 

this is for normal form or catalog form?

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

@Ankur Bawiskar  - This is for catalog form

@ads 

I created blog few years ago; check that and enhance it further

Verify Mandatory Attachments Count on Catalog Item 

ensure you use correct choice value for Manage document

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

	var countRequired = 1;
	if(g_form.getValue('request_required') == 'Manage document'){
		if(window == null){
			// portal
			if(this.document.getElementsByClassName('get-attachment').length != countRequired) {
				g_form.addErrorMessage(getMessage('attachment_is_mandatory'));
				return false;
			}
		}
		else{
			// native view
			var length = $j("li.attachment_list_items").find("span").length;
			if(length != countRequired){
				g_form.addErrorMessage(getMessage('attachment_is_mandatory'));
				return false;
			}
		}
	}
}

 If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Ankur Bawiskar  - Hi, I tested with some users, its not working for the locatio1 users, can you please check once.