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

On which version is the demo instance you were able to see the the current.operation() logged.


Okay this works .. Just verified on my personal instance


function onAfter(current, previous) {


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


  if(current.operation()=='insert')


  {


  gs.addInfoMessage("insert here");


  }


  if(current.operation()=='update')


  {


  gs.addInfoMessage("update here");


  }


}



Not sure why you are getting null but the above code prints the messages as intended.


I tried Pradeep's code on   demo ServiceNow and on my personal developer instance provided from https://developer.servicenow.com/app.do#!/dashboard and also my company's another instance.In all three instances I could see only one blue info message as attached.None for the operation info message :


Pradeep and Kalaiarasan,


Did you guys create the BS for cmn_location table?


Was the demo instance same as mine?


Thanks for your time.



loc-bs-3.png


I could make this work by creating three separate business rules one each for 'insert', 'update' and 'delete' as Robert Pastore said.I could see that after each action only the right one BS is being triggered.


I assume the versions Kalaiarasan and Pradeep were using were different .I could see a lot of scripts with current.operation(). Could not see anywhere in the document that current.operation() is not available with 'after' BS.Lost one whole day over this.


Thanks guys! I believe in this community!!