Making an attachment mandatory in a catalog item

SK41
Giga Guru

Hi,

 

I have a requirement to make the attachment mandatory on the catalog item. User does not use service portal, so I cannot use "mandatory checkbox" in the catalog. My requirement is to check if the user has attached the file before submitting the catalog. If not, show them error message "Please attach a file".

 

How can I achieve that?

Thanks!

1 ACCEPTED SOLUTION

Hi Anurag,

 

I used below code in onSubmit catalog client script and it worked for me:

 

var count = getCurrentAttachmentNumber();
    if(count == 0){
        g_form.addErrorMessage('You must add an attachment');
        return false;
    }

 

View solution in original post

17 REPLIES 17

Sonam_Tiwari
Kilo Sage

Hey @SK41 ,

 

This KB is what you need probably - https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0743672

 

This is how we can do it -

We can  have a UI script - with a function to validate the attachment count

 

 

function getCatAttachmentCount() {
	var length;
	try {
		length = angular.element("#sc_cat_item").scope().attachments.length;
	} catch(e) {
		length = -1;
	}
	return length;
}

 

 

And then in a onSubmit Client script, we simply call that function - 

 

 

function onSubmit() {	
		var count = getCatAttachmentCount();
		if(count <= 0) {
			g_form.addErrorMessage('Please attach required artefacts');
			return false;
		
}
}

 

 

Try this out once.

Consider indicating the response as helpful and marking it as correct if it meets your needs.

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @SK41 

 

How user is going to use catalog then? by Native view?

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Stefan Georgiev
Tera Guru

Hello @SK41,

 

you need to create a onSubmit client script to check if there is an attachment, I saw this article, but I haven't tested it if it works:

https://www.servicenow.com/community/itsm-articles/making-attachment-mandatory-for-a-catalog-item-de...

 

if it does not work for your case you need to create an additional script include that checks if there are attachments added and call it from the client script with a glide ajax there is a bit more work there because you need to make the call to the script include and fake the return and call the submit so it does not not close the request.
Write me again if this does not work to try to explain to you the other solution.
Hope that this helps you!

If the provided information answers your question, please consider marking it as Helpful and Accepting the Solution so other community users can find it faster.

All the Best,
Stefan

Rajdeep Ganguly
Mega Guru

Sure, you can achieve this by using a client script in ServiceNow. Here are the steps: 1. Navigate to Service Catalog > Catalog Definitions > Maintain Items. 2. Open the catalog item where you want to make the attachment mandatory. 3. Go to the "Client Scripts" related list and click on "New" to create a new client script. 4. Fill in the fields as follows: - Name: Give a name to your client script. - Type: Select "OnSubmit". - Script: Add the following script: javascript function onSubmit() { var attachment = new GlideRecord('sys_attachment'); attachment.addQuery('table_sys_id', g_form.getUniqueValue()); attachment.addQuery('table_name', 'sc_cart_item'); attachment.query(); if (!attachment.next()) { g_form.addErrorMessage('Please attach a file'); return false; } return true; } 5. Click on "Submit" to save the client script. This script will check if there is an attachment associated with the catalog item before submission. If there is no attachment, it will show an error message "Please attach a file" and prevent the form from being submitted.

Ankur Bawiskar
Tera Patron
Tera Patron

@SK41 

this should work in native and you can check the attachment count and show alert and stop form submission

function onSubmit(){

	var count = getCurrentAttachmentNumber();
	if(count == 0){
		alert("Please attach a file");
		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