How to make the Attachments mandatory in catalog ITIL view

Mainkanta
Tera Contributor

Hello All,

I have Requirement, to make the managed attachments need to be mandatory while submitting the catalog item Request in ITIL view. Please refer the below screen shot.

 

Mainkanta_0-1687517213446.png

 

i have tried the client script as below:

function onSubmit() {
    //Type appropriate comment here, and begin script below
    if (this.document.getElementsByClassName('get-attachment').length == 0) {
        g_form.addErrorMessage("Please add at least one attachment.");
        return false; 
    }
var length = $j("li.attachment_list_items").find("span").length;
        if (length == 0) {
            g_form.addErrorMessage('You must add an attachment before submitting this incident');
 
           return false;
        }
 
}
it is throwing an error like this "Error MessageonSubmit script error: TypeError: Cannot read properties of null (reading 'document'):
function () { [native code] }
"
It was not submitting the form even after adding file to the item while submitting the request. The issue hapening in only itil view only.
 
In the service portal we have made add attachment link as mandatory through the check box called "Mandatory attachment" on the portal settings in catalog item form. it was working as expected.
 
Please suggest me how can we fix the issue.
 
Thanks,
Manikanta
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Mainkanta 

this will work in native view

function onSubmit(){
	var count = getCurrentAttachmentNumber(); // this function is out of the box
	if(count == 0){
		g_form.addErrorMessage('You must add an attachment before submitting this incident');
		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

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Mainkanta 

this will work in native view

function onSubmit(){
	var count = getCurrentAttachmentNumber(); // this function is out of the box
	if(count == 0){
		g_form.addErrorMessage('You must add an attachment before submitting this incident');
		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

It is working as expected in native view.

Thank you for the quick response.

Hi Ankur,

You are always a Saviour .

Can you please let me know is there a way to make attachment not mandatory only for one subcategory on catalog item.

I have raised a new question 

How to make attachment not mandatory based on subc... - ServiceNow Community

can you please help me