Business rule script not working- help !

Tal
Kilo Contributor

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");
}

 

 

 

1 ACCEPTED SOLUTION

Smriti8
Tera Expert

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.

 

View solution in original post

4 REPLIES 4

Smriti8
Tera Expert

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.

 

Tal
Kilo Contributor

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: 

find_real_file.png

 

 

 

The thing is after the setAbortAction , i can see in the debugger , that it continue to this code (add attachment) , my question is- why?

find_real_file.png

 

Smriti8
Tera Expert

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.

 

Tal
Kilo Contributor

Thanks! its working