How to limit the total number of attachments in a form like incident?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-11-2019 10:17 PM
How to limit the total number of attachments on a form like incident i.e., For Example the maximum number of attachments a user can upload should be 5 and not more than that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-11-2019 10:24 PM
Hi,
This can be done by writing a Before Insert Business rule on sys_attachment table as mentioned below:
Condition of the Business Rule: Run it only for the table you want this functionality for. For example:
current.table_name == 'xyz' //Replace xyz with your Table Name
(function executeRule(current, previous /*null when async*/) {
// Add your code here
if(current.table_name == 'incident'){
var attRec = new GlideRecord('sys_attachment');
attRec.addQuery('table_sys_id', current.table_sys_id);
attRec.query();
while(attRec.next()){
var getCount = attRec.getRowCount();
gs.addInfoMessage(getCount);
if(getCount > 1){
gs.addErrorMessage('Maximum number of attachments allowed are 1');
current.setAbortAction(true);
}
}
}
})(current, previous);
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-12-2019 12:22 AM
actually i've written similar code but still it is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-11-2019 10:28 PM
Hi Rohith,
use below script and show message to user
use before insert business rule on sys_attachment table; with condition as current.table_name == 'incident'
script below
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', current.table_sys_id);
gr.query();
var rowCount = gr.getRowCount();
if(rowCount > 5){
gs.addErrorMessage('Only 5 attachments are allowed');
current.setAbortAction(true);
}
screenshot below
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-12-2019 12:22 AM
No it is not working