how to check if attachments are added before submitting the RITM?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
How to check if attachments are added to the catalog item before submitting the Request?
Regards,
Saranya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hi @saranyavs,
You can just make the attachment mandatory using a checkbox in the catalog item.
You can use the below image for reference:
If you find my answer useful, please mark it as Helpful and Correct 😊
Regards,
Soham Tipnis
ServiceNow Developer || Technical Consultant
LinkedIn: www.linkedin.com/in/sohamtipnis10
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
If you users are only using Portal then this checkbox will achieve your requirement.
If they are using both Native + Portal then this will work in both places
Create onSubmit Catalog Client Script
UI Type - ALL
function onSubmit() {
//Type appropriate comment here, and begin script below
try {
if (window == null) {
// portal
if (this.document.getElementsByClassName('get-attachment').length != 1) {
alert('You must attach file before submitting this request.');
return false;
}
}
} catch (ex) {
// native view
var length = getSCAttachmentCount();
if (length != 1) {
alert('You must attach file before submitting this request.');
return false;
}
}
}
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hey @saranyavs
Option 1: Client-Side Validation Using g_form.getAttachments()
This is the simplest and recommended approach for most cases.
Type: onSubmit
UI Type: All
Script
function onSubmit() {
var attachments = g_form.getAttachments();
if (!attachments || attachments.length == 0) {
alert("Please attach the required document before submitting the request.");
return false; // Prevent submission
}
return true; // Allow submission
}Option 2: GlideAjax with Script Include
If attachment validation is inconsistent in Service Portal or custom widgets, use this method.
We will check the sys_attachment table before submission.
Step 1: Create Script Include
Name: CheckAttachmentAjax
Client Callable: True
var CheckAttachmentAjax = Class.create();
CheckAttachmentAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkAttachment: function() {
var sysId = this.getParameter('sysparm_sys_id');
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', sysId);
gr.query();
if (gr.hasNext()) {
return 'true';
}
return 'false';
}
});
Step 2: Create Catalog Client Script
Type: onSubmit
UI Type: All
function onSubmit() {
var ga = new GlideAjax('CheckAttachmentAjax');
ga.addParam('sysparm_name', 'checkAttachment');
ga.addParam('sysparm_sys_id', g_form.getUniqueValue());
var answer = ga.getXMLWait();
if (answer != 'true') {
alert('Please attach a file before submitting the request.');
return false; // Prevent submission
}
return true; // Allow submission
}
*************************************************************************************************************************************
If this response helps, please mark it as Accept as Solution and Helpful.
Doing so helps others in the community and encourages me to keep contributing.
Regards
Vaishali Singh
