Can we use update function multiple time in BR ?

shaik_irfan
Tera Guru

Hello,

I had a requirmeent where i written a customize code and used multiple updates for differnet tables whichis working fine but just want to know is this is a best practise ?

function ba_renderTaskApprovalsMoot() {

current.approval = 'cancelled';

current.update(); // this is to update my current table

var gr = new GlideRecord('sysapproval_approver');

  gr.addQuery('sysapproval', current.sys_id);

  var qc = gr.addQuery('state', 'requested');

  qc.addOrCondition('state', 'not requested');

qc.addOrCondition('state', 'approved');

  gr.query();

  while (gr.next()) {

      gr.state = 'cancelled';

      gr.update(); // this is to update my approval table

}


}

9 REPLIES 9

Sabrina10
Kilo Guru

Best practice is that when updating the current record, you should do it in a BEFORE business rule when possible, that way you don't need to use the current.update() statement.



When updating another table, you should use an AFTER business rule, to ensure that the update to the primary record is committed before making updates to related records.






jegadeesh
ServiceNow Employee
ServiceNow Employee

Best practice to update the record


----------------------------------------------


1) In Before business rule, we don't need to update the current record


2) In After business rule, current.update( ) can be used to update the current record as well as other tables also.


Hello Shyam,,



To add, option2 if used in special circumstances should have setWorkflow() method to prevent recursion.


Hello Pradeep,



If it goes in recursion only, then setWorkFlow() is needed.


Can you please share the use case when to use and when not to and also point to the docs link if any.