- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hi Team,
I have a requirement on a Service Catalog Item where an attachment should be mandatory based on a
variable selection.
Requirement
Catalog Item has a Select Box variable: request_type
Values:
Bank Account Maintenance
Scheduler Maintenance
Other
When request_type = Bank Account Maintenance OR Scheduler Maintenance, an attachment must be mandatory
For Other, attachment is not required
What I Tried
I implemented a Catalog Client Script (onSubmit) to validate attachments using g_form.getAttachments().
function onSubmit() {
var requestType = g_form.getValue('request_type');
// Check only for required options
if (requestType == 'Bank Account Maintenance' || requestType == 'Scheduler Maintenance') {
var attachments = g_form.getAttachments();
if (!attachments || attachments.length === 0) {
alert('Attachment is mandatory for Bank Account Maintenance or Scheduler Maintenance requests.');
return false; // Stop submission
}
}
return true;
}
Not working - any one please help me
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello @nameisnani
function onSubmit() {
var requestType = g_form.getValue('request_type');
// Check only for required options
if (requestType == 'Bank Account Maintenance' || requestType == 'Scheduler Maintenance') {
if (this.document.getElementsByClassName('get-attachment').length == 0) {
alert('Attachment is mandatory for Bank Account Maintenance or Scheduler Maintenance requests.');
return false; // Stop submission
}
}
return true;
}
If my response has helped you, hit the helpful button, and if your concern is solved, do mark my response as correct.
Thanks & Regards
Viraj Hudlikar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hope you are doing good.
Did my reply answer your question?
💡 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 weeks ago
Hello @nameisnani
function onSubmit() {
var requestType = g_form.getValue('request_type');
// Check only for required options
if (requestType == 'Bank Account Maintenance' || requestType == 'Scheduler Maintenance') {
if (this.document.getElementsByClassName('get-attachment').length == 0) {
alert('Attachment is mandatory for Bank Account Maintenance or Scheduler Maintenance requests.');
return false; // Stop submission
}
}
return true;
}
If my response has helped you, hit the helpful button, and if your concern is solved, do mark my response as correct.
Thanks & Regards
Viraj Hudlikar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
you want this validation in native or portal OR Both?
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 weeks ago
@Ankur Bawiskar Both
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
then this will work in both
function onSubmit() {
//Type appropriate comment here, and begin script below
try {
var requestType = g_form.getValue('request_type');
var count = getRPAttachmentCount();
if (window == null && (requestType == 'Bank Account Maintenance' || requestType == 'Scheduler Maintenance')) {
// portal
if (count == 0) {
alert('Attachment is mandatory for Bank Account Maintenance or Scheduler Maintenance requests.');
return false;
}
}
} catch (ex) {
// native view
var length = getSCAttachmentCount();
var requestType1 = g_form.getValue('request_type');
if (length == 0 && (requestType1 == 'Bank Account Maintenance' || requestType1 == 'Scheduler Maintenance')) {
alert('Attachment is mandatory for Bank Account Maintenance or Scheduler Maintenance requests.');
return false;
}
}
}
function getRPAttachmentCount() {
var length;
try {
length = angular.element("#sc_cat_item_producer").scope().attachments.length;
} catch (e) {
length = -1;
}
return length;
}
💡 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

