- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 03-07-2022 08:59 AM
Below script may help you to check how many attachments are uploaded in catalog item.
You can create new Catalog Client Script and make sure to set following fields:
- Isolate script: should be unchecked
- UI Type - All
- Script :
function onSubmit() {
var attachmentCount = 0;
var expectedAttachmentCount = 2;
var errorMsg = 'Expected attachments are ' + expectedAttachmentCount;
try {
if (typeof parent.angular != 'undefined') { // portal
var pageScope = parent.angular.element('[template="sp_attachment_single_line"]').scope();
if (!pageScope) { //no attachments
attachmentCount = 0;
} else { // when attachments count is more > 0
attachmentCount = pageScope.attachments.length;
}
} else { // native view
attachmentCount = getCurrentAttachmentNumber();
}
if (attachmentCount < expectedAttachmentCount) {
g_form.addErrorMessage(errorMsg);
return false;
}
} catch (e) {
console.error(e);
g_form.addErrorMessage("Something went wrong. Please contact system administrator");
return false;
}
}
- 4,092 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi
I'm using your script to validate attachment count based on a variable input. It is working when attachment is not attached.
After attaching the attachment, attachmentCount is showing as 1 but I'm getting Javascript error in browser console while submitting the request.
PFB the script which I'm using to Validate attachments in Portal.
function onSubmit() {
if (g_form.getValue('more_than_one_device_the_list_has_been_uploaded') == 'true') {
var attachmentCount = 0;
var expectedAttachmentCount = 1;
var errorMsg = 'You must add an attachment before submitting this request as you have selected more than one device option';
try {
if (typeof parent.angular != 'undefined') { // portal
var pageScope = parent.angular.element('[template="sp_attachment_single_line"]').scope();
if (!pageScope) { //no attachments
attachmentCount = 0;
} else { // when attachments count is more > 0
attachmentCount = pageScope.attachments.length;
}
}
if (attachmentCount < expectedAttachmentCount) {
alert('inside attachment portal script - if loop validation');
alert(errorMsg);
return false;
}
}
catch (e) {
console.error(e);
alert("Something went wrong. Please contact system administrator");
return false;
}
}
}
Can you please help me to understand why I'm getting Javascript error in your browser console error ?
Thanks,
Nivea.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thanks for the tip, but I'm curious: where is "getCurrentAttachmentNumber" defined? I searched for it but could not find it. Definitely not a documented call, which is risky.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@Jim Coyne - You are right getCurrentAttachmentNumber is not a documented API hence there is a risk of using non published API.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
That's too bad. Maybe you have some power to get it documented and supported? 🙂
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Unfortunately, I haven't reached that level yet 🙂
You can submit product enhancement request - https://www.servicenow.com/community/itom-articles/have-you-visited-our-ideas-portal-use-this-to-pro...
I completely understand that relying on DOM is risky but I am really not sure if there are any published API available.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Tried this but 'pageScope' is undefined
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
This was very helpful but question when you have a variable for attachment it doesnt seem that it detects if it has an attachment
i have this variable in an MRV.
how can you modified this for this case