- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2024 05:28 AM
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!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 03:10 AM
Hi Anurag,
I used below code in onSubmit catalog client script and it worked for me:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2024 05:42 AM - edited 01-09-2024 05:47 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2024 05:42 AM
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]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2024 05:44 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 01:43 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 03:10 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader