Event handling - Need to run a script when the event is fired

gautamchawak
Kilo Explorer

There is an event created on discovery_device_history table. Want to take some action when the event is fired.
I have already performed the following steps:
1. Register the event (event is registered in table : sysevent_register)
2. Fired the event in the business rule (gs.eventQueue("discovery.device.complete", current, current.sys_id, current.status));
3. Handle the event that is fired (Don't want to send notifications but have to run a script that performs certain actions)

Am stuck with step3. How do I run a script that performs certain action when a event is fired?

11 REPLIES 11

CapaJC
ServiceNow Employee
ServiceNow Employee

You want to use a Script Action. Aside from Notifications, it's the only other thing that reacts to evernts. You can then execute arbitrary script.

http://wiki.servicenow.com/index.php?title=Script_Actions


Thanks for providing this solution. Only question I have is when would the script inside the script action get executed? Would it get executed when the record gets inserted into sysevent table (like a business rule) or would it get queued up and get executed asynchronously.


CapaJC
ServiceNow Employee
ServiceNow Employee

It's async, like Notifications, run when the event processor does its job (it runs every 30 seconds and processes all queued events).

If you need it immediate, you could try an on-insert Business Rule on the sysevent table. Only possible catch is I don't know if events get inserted in such a way that Business Rules will get run. I've not tested that.


As you have mentioned in your comment, script action runs after 30 seconds and is performing the activity that I intend to do.
Thank you very much for the quick help.
Really appreciate it.


My script action is updating some columns in cmdb table but there are some business rules which restricts updation of those columns.
Can I stop the workflow to disable business rules when my script action is running.

This was the code I had found in one of the business rule when discovery schedule is run:

g_device.stopworkflow(true); //Disable all business rules
cmdb_ci.update();
g_device.stopworkflow(false); //Enable all business rules

Is there something similar for script action?