- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2024 01:39 AM
Hi,
I have created an after business rule to check for the record count. If the record count excides more than 5 then i need to abort the attached record from inserting into the table record. I see that the current.setAbortAction(true); is not working. I need to do this using after business rule. It is working fine for before business rule as i also have other custom functionality being implemented. Please let me know If there is any other way to remove the attachment.
Code:
var chkAttDiv1 = new GlideAggregate('sys_attachment');
chkAttDiv1.addAggregate('count', 'table_name');
chkAttDiv1.addQuery('table_sys_id', current.table_sys_id);
chkAttDiv1.query();
if (chkAttDiv1.next()) {
var getCount1 = chkAttDiv1.getAggregate('count', 'table_name')
gs.info("Attach:Backend Table Count getCount1 : " + getCount1 );
if (getCount1 >= 5) {
gs.addErrorMessage('You cannot attach more than 6 attachments');
current.setAbortAction(true);
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2024 01:52 AM - edited 06-06-2024 02:06 AM
Hi @Hari1,
Can I ask why this logic and Business Rule needs to be executed in an after business rule? As the naming suggests, it will run after the insert / update so therefore the abort will not work.
As per the SN Docs (https://developer.servicenow.com/dev.do#!/learn/learning-plans/washingtondc/new_to_servicenow/app_st...
"After Business Rules execute their logic immediately after a database operation occurs and before the resulting form is rendered for the user. Use after Business Rules when no changes are needed to the record being accessed in the database. For example, use an after Business Rule when updates need to be made to a record related to the record accessed. If a record has child records use an after Business Rules to propagate a change from the parent record to the children."
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2024 01:52 AM - edited 06-06-2024 02:06 AM
Hi @Hari1,
Can I ask why this logic and Business Rule needs to be executed in an after business rule? As the naming suggests, it will run after the insert / update so therefore the abort will not work.
As per the SN Docs (https://developer.servicenow.com/dev.do#!/learn/learning-plans/washingtondc/new_to_servicenow/app_st...
"After Business Rules execute their logic immediately after a database operation occurs and before the resulting form is rendered for the user. Use after Business Rules when no changes are needed to the record being accessed in the database. For example, use an after Business Rule when updates need to be made to a record related to the record accessed. If a record has child records use an after Business Rules to propagate a change from the parent record to the children."
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2024 02:00 AM
Hi @Hari1,
You need to validate before the database operation is performed. in after business rule it is happening, it will not work.
Please accept my solution if it works for you and thumps up.
Thanks
Jitendra