Attachment in Catalog Item restriction
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 03:45 AM
Hi all
Had a query on attachment restrictions. Have a catalog item developed and it uses the OOB attachment paper icon.
Need to keep this, but want to restrict the user from submitting more than 3 attachments.
If user submits more than 3 attachments, need to pop up a error message and restrict submission. Tried 'onSubmit' catalog client script, but we cannot do any DOM manipulation. 'this.document.getElementsByClassName.length', is where the DOM manipulation caused. This is the script used:
function onSubmit() {
//Type appropriate comment here, and begin script below
var getAttach = this.document.getElementsByClassName('get-attachment').length;
//alert(getAttach+"length is");
if (getAttach > 3) {
g_form.addErrorMessage("Maximum number of attachment is 3");
return false;
}
}
Any other solution which can be used for this use case. Please provide if any.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 11:18 PM
You can use Glide Ajax in Client Script and Script Include to get the attachment count from sys_attachment form.
Please use below code and let me know if this works for you.
Client Script
function onSubmit() {
var cartId = g_form.getParameter('sysparm_item_guid'); // finds the card id
var ga = new GlideAjax('AZFCMassHelper');
ga.addParam('sysparm_name', 'getAttachmentCount');
ga.addParam('sysparm_cartID', cartId);
ga.getXMLWait();
var count = ga.getAnswer();
if (count > 3) {
g_form.addErrorMessage("Maximum number of attachment is 3");
return false;
} else
return true;
}
Script Include
var AZFCMassHelper = Class.create();
AZFCMassHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAttachmentCount:function()
{
var cartid=this.getParameter('sysparm_cartID');
var gr=new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id',cartid);
gr.query();
return gr.getRowCount();
},
type: 'AZFCMassHelper'
});
Regards,
Sourabh