Make attachment mandatory on both native UI and Service portal for a catalog item.

Balaram7
Kilo Sage

Hi all,

 

We created a new catalog item " Application Access", which contains Request type as a variable(Select box) with three choices. 

Request type

-> -none-

-> Add

-> Modify

-> Remove

If Request type is Add or Modify, then need to make Attachment mandatory & if Request type is Remove then Attachment is not required.

Please help me with the best approach to achieve this.

 

Thank you,

Balaram.

3 REPLIES 3

Feddy
Kilo Sage

Hi Balaram, 

You can have onsubmit client script to achieve this.
Please refer below code for you reference.

You catalog client script can be : 

Type : onSubmit
Name : your choice
UI type : ALL
Isolate Script : false

script :

function onSubmit() {

//Checking Attachment
try { //Works in non-portal ui

var attachments = document.getElementById('header_attachment_list_label');

if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {
if (g_form.getValue('<request type backend value') == 'add' || g_form.getValue('<request type backend value') == 'modify') {

alert('Please attach document.');

return false;

}

 

}

catch (e) {

if ((this.document.getElementsByClassName('get-attachment').length == 0)) {

if (g_form.getValue('<request type backend value') == 'add' || g_form.getValue('<request type backend value') == 'modify') {

 

{

alert('Please attach document');

return false;
}

}
}

}

Please check and confirm.

Ankur Bawiskar
Tera Patron
Tera Patron

@Balaram 

refer my blog for achieving this requirement for native + portal

Verify Mandatory Attachments Count on Catalog Item

If it helps please mark blog as helpful and also bookmark it

Adding script for help from same blog of mine

Script: onSubmit

Note: Ensure Isolate Script field is set to False for this Catalog Client Script to ensure DOM manipulation works

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

	var countRequired = 1;

	if(g_form.getValue('request_type') == 'Add' || g_form.getValue('request_type') == 'Modiy'){

		if(window == null){
			// portal
			if(this.document.getElementsByClassName('get-attachment').length != countRequired) {
				alert('You must add attachments before submitting this request.');
				return false;
			}
		}
		else{
			// native view
			var length = $j("li.attachment_list_items").find("span").length;
			if(length != countRequired){
				alert('You must add attachments before submitting this request.');
				return false;
			}
		}
	}
}

Isolate Script: Set to False

image

Regards
Ankur

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

krutarth0023
Tera Contributor

Thank you for  sharing. it work for me.