How to: Make attachments mandatory in Employee Center
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hello everyone,
I was looking around for a way to make the default attachment section on a catalog item mandatory, and found that a lot of the community posts out there seem to be outdated and don't work in Employee Center. I wanted to share the fix that I ultimately settled on in hopes that it saves others some time.
Scenario:
A catalog item can be submitted to request something, or to request reimbursement for that item. Attachments are only needed when the request is for reimbursement. This rules out the OOB 'Mandatory Attachment' checkbox since the attachment is based on a condition. We also don't know how many attachments the user will need to submit, so using an attachment variable also doesn't work in this scenario.
Solution:
Create an onSubmit client script that will check for the request type (indicated by a variable), and then prompt the user with an error message if no attachments are found when they click 'Order Now'.
function onSubmit() {
var requestType = g_form.getValue('tr_i_want_to'); // Variable you want to use for the condition
if(requestType == 'reimbursement'){ // Value for the condition
var hasAttachment = false;
var errorMessage = "Your error message here.";
// Count the physical attachment rows rendered on the screen
if (typeof this.document !== 'undefined') {
var filesOnScreen = this.document.getElementsByClassName('get-attachment');
if (filesOnScreen && filesOnScreen.length > 0) {
hasAttachment = true;
}
}
// If no attachments are found, show the error message
if (!hasAttachment) {
g_form.clearMessages();
g_form.addErrorMessage(errorMessage);
return false;
}
}
}
Hope this helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
thanks for sharing the approach
I believe this approach has been already used by many developers in the past and it has worked well
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader