onAfter Business rule: current.operation() is null

tantony
Mega Guru

I have come to a road block here.I have a onAfter business rule which entirely depends on current.operation() and it is null. The business rule is firing.The first gs.info message logs:Business rule running: null 4f65a2676f9a0200b5919e0cbb3ee43b. Does not go beyond that.I am also attaching the image of business rule.Any direction will be helpful.I am on Fuji Patch 7.

location_bs_1.pnglocation_bs_2.png

function onAfter(current, previous) {

  //This function will be automatically called when this rule is processed.

  gs.info("Business rule running: {0} {1}", current.operation(),current.sys_id);

  if( current.operation() == "insert" ) {

  gs.log('Insert rule running:', 'Location Sync Remote App Table');

  insertToRemoteTable(current);

  }

  else if( current.operation() == "update") {

  gs.log('Update rule running:', 'Location Sync Remote App Table');

  updateOnRemoteTable(current);

  }else if ( current.operation() == "delete") {

  gs.log('Delete rule running:' , 'Location Sync Remote App Table');

  deleteFromRemoteTable(current);

  }

}

1 ACCEPTED SOLUTION

rob_pastore
ServiceNow Employee
ServiceNow Employee

I don't believe that operation is available for an 'after' business rule.



I would suggest creating two separate business rules, with the insert and update only checked respectively.



(Other option is make it a before, but it could be a performance hit.)


View solution in original post

13 REPLIES 13

Thanks for your interest in my question.I am not creating this BR as part of a scoped application.It is for   OOB Location table(cmn_location).The table was available for me to select   on create Business Rule   form, 'Table' field. So I guess the table is in same scope???


Tried changing to gs.info.Did not help.


Okay..Last try and this should work. I have tried on demo and it works.


function onAfter(current, previous) {


  //This function will be automatically called when this rule is processed.



  gs.addInfoMessage('Business rule running:'+current.sys_id);


  if( current.operation() == "insert" ) {


  gs.addInfoMessage('Business rule running:'+current.operation());


  insertToRemoteTable(current);


  }


  else if( current.operation() == "update") {


  gs.addInfoMessage('Business rule running:'+current.operation());


  updateOnRemoteTable(current);


  }else if ( current.operation() == "delete") {


  gs.addInfoMessage('Business rule running:'+current.operation());


  deleteFromRemoteTable(current);


  }


}


rob_pastore
ServiceNow Employee
ServiceNow Employee

I don't believe that operation is available for an 'after' business rule.



I would suggest creating two separate business rules, with the insert and update only checked respectively.



(Other option is make it a before, but it could be a performance hit.)


But I do remember using current.operation() in a after rule. Did something change in the latest release?


Hi Robert,



This will work. Please correct me if I m wrong.