sys_attachment delete Business Rule runs only once when multiple attachments deleted

Bryan13
Tera Expert

I extended the User (sys_user) table and added some attachment related columns (to allow for download/deletion via the portal). I created a Business Rule to clear these fields when an attachment is deleted. This works fine when only one attachment is deleted. However if you delete multiple via "Manage Attachments", the business rule runs only once and only one of the attachments related columns are cleared.

Any ideas on what I need to do to catch each deletion?

bus-rule1.PNG

bus-rule2.PNG

bus-rule3.PNG

1 ACCEPTED SOLUTION

Hi Bryan,



Please use few code in your addQuery below.



gr.addQuery('table_sys_id', current.table_sys_id);



You are using if condition to check if sys_id matches and then update the record, that means you will be taking care only one record since var file_sys_id = current.sys_id.toString() will give you sys_id of current record. So, i think if you want to delete all the record based upon the table_sys_id then while loop should work without if/else condition in that.



In your else if condition you are using gr.gr.<variable name>. two times gr is not correct.



In file_sys_id == gr.resume_sys_id, is resume_sys_id a reference field which gives sys_id?



Hope this helps.


View solution in original post

5 REPLIES 5

Sharique Azim
Mega Sage

HI Bryan,



Two things here,



1. You can first query the sys_attachment table and see if there are no records for the same ticket. This could also work   for single as well as multiple   attachments..



So,if you   do something like  



var att=....


..


if(!att.hasNext()){



main code



}




2. Avoid   using .update() functions in your BR, specially for After and Before types.. Simply commenting them would do.


Sharique,



I am afraid I am not following you. I read that you should not update the current object (i.e. sys_attachment in this case). However, I thought it is okay to update related objects (the extended sys_user record).



Avoid using current.update() in a business rule script. The update() method triggers business rules to run on the same table for insert and update operations, leading to a business rule calling itself over and over. Changes made in before business rules are automatically saved when all before business rules are complete, and afterbusiness rules are best used for updating related, not current, objects. When a recursive business rule is detected, the system stops it and logs the error in the system log. However, current.update() causes system performance issues and is never necessary.



I do not know how I would accomplish my goal without using update().


Hi Bryan,



Please use few code in your addQuery below.



gr.addQuery('table_sys_id', current.table_sys_id);



You are using if condition to check if sys_id matches and then update the record, that means you will be taking care only one record since var file_sys_id = current.sys_id.toString() will give you sys_id of current record. So, i think if you want to delete all the record based upon the table_sys_id then while loop should work without if/else condition in that.



In your else if condition you are using gr.gr.<variable name>. two times gr is not correct.



In file_sys_id == gr.resume_sys_id, is resume_sys_id a reference field which gives sys_id?



Hope this helps.


Okay, I'll give that a try. I did add a gs.info message and see that the business rule is getting called twice. I'm wondering if the record is being locked on the first update and therefore the second update fails. I will research the logs and also review your suggestion. Thanks.



file_sys_id.PNG