adding an attachment to an existing record is not triggering a business rule

cdgaefke
Kilo Expert

Hello all, I discovered a while back that business rules only trigger on updates if servicenow believes the record has changed.

 

That appears to be causing me a problem with attachments.   I have a field on my table that has to be zero if there are no attachments, and has to be non zero if there are attachments.   Easy enough to do with a business rule, and it works great when the attachments (or lack thereof) are added when the record is created.

 

However, if I create the record, save it, then add an attachment but don't change anything else, the business rule isn't firing, because the parent record didn't change.

 

Any thoughts on how I can address this?

 

Thanks.

9 REPLIES 9

solutioningnow
Giga Guru

Hi,



You can write business rule on Attachment table instead of base table like incident.



Let me know if you need more information.



Regards,


Solutioner


Kumar Tella
Mega Expert

Hi Carles,



You will need a Business rule on sys_attachment table



Example:


Name: update incident attach flag


Table: sys_attachment


run at : Server


condition: current.table_name == "incident"


when: after insert, update



Script:


var gr = new GlideRecord("incident");


gr.addQuery("sys_id", current.table_sys_id);


gr.query();


if (gr.next()) {


gr.yourfieldname = "xx"


gr.update();


}



Hope this helps.



Thanks


Kumar


In the above script, I used incident table as example , please change it to the table name you want to update.


Hi @Kumar Tella ,
I tried with the same approach as mentioned above but the business rule is not fired.

condition = current.table_name == "incident"

Operation : After Insert/Update

(function executeRule(current, previous /*null when async*/ ) {
    var gr = new GlideRecord("incident");
    gr.addQuery("sys_id", current.table_sys_id);
    gr.query();
	gs.log("===>record exists", gr.next());
    while(gr.next()) {
		gs.log("===>record glide", gr);
		//fi = gr.getElement('isAttachmentUpdated'); 
		gr.setValue('isAttachmentUpdated', 'Updated'); //custom field isAttachmentUpdated
        gr.update();
    }

    // Add your code here

})(current, previous);

Note: I'm able to receive the "record exists" debug as "true" but not able to get the log inside while loop. Please help me understand what is the issue fix as the custom field (isAttachmentUpdated) on incident is also not getting updated.