- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2019 03:19 AM
Hi there guys,its my first time here.
I made a business rule , when someone try to add an attachment to record, the rule check if the attachment already exist in the 'sys_attachment' table, and if alreay exist, it abort the action and not inserting the new attachment.
The condition (in the code below) is working , but it seems that after current.setAbortAction(true) - it continue to the the next code (number 2 , below) and not prevent the attachment to be added to the table.
I will glad to get some help- tahnks!
Code num 1 (my advanced script)
(function executeRule(current, previous /*null when async*/) {
var attach = new GlideRecord('sys_attachment');
attach.addQuery('table_sys_id', current.table_sys_id);
attach.addQuery('table_name', current.table_name);
attach.addQuery('file_name', current.file_name);
attach.addQuery('content_type', current.content_type);
attach.addQuery('size_bytes', current.size_bytes);
attach.query();
if(attach.next()){
current.setAbortAction(true);
}
})(current, previous);
Code num 2 (built-in script which apparently add an attachment )
var att = new Attachment();
att.setRecord(current);
if (att.isIndexed()) {
var key = current.table_name + '-' + current.table_sys_id + '-' + current.sys_id;
gs.eventQueue("text_index.attachment", null, current.operation(), key, "text_index");
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2019 12:14 AM
Hi Tal,
I just noticed that the application of BR is not "Global" but "Programs".
SetAbortAction does not work if the BR is in Scoped App and it is executed against the Global table. sys_attachment is a global table.
So my suggestion would be to create the BR in global application. This should solve your issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2019 10:51 PM
Hi Tal,
Make sure that the business rule(Code num 1) is a before Business Rule and the order is less than 100 or empty. This would ensure that your code runs before the second business rule and thus doesn't add attachment.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2019 12:55 AM
Hi Smirti , thanks for the replay! but it still not working
The Business Rule was already set to Before as you can see in the image below:
The thing is after the setAbortAction , i can see in the debugger , that it continue to this code (add attachment) , my question is- why?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2019 12:14 AM
Hi Tal,
I just noticed that the application of BR is not "Global" but "Programs".
SetAbortAction does not work if the BR is in Scoped App and it is executed against the Global table. sys_attachment is a global table.
So my suggestion would be to create the BR in global application. This should solve your issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2019 07:14 AM
Thanks! its working