Events that refer to deleted records, don't seem to work.

klassa
Kilo Explorer

I'm working through the "Module: Events and script actions" part of the "Coding on ServiceNow" self-paced tutorials.

I have a business rule that fires events whenever an "attendee" record is added to the database, changed, or removed.   The business rule and the events are tied to the attendee table.   The business rule fires after insert, update, or delete.

The script action that In the script action that gets triggered for adds, seems to work.   (It sets about to counting up some related information, and modifying a table with the result.)

The one that handles deletes, doesn't.   It flat out doesn't seem to run (i.e. even my log messages don't show up), and instead, I get messages like "getEventTarget() called with invalid record reference ... could have been deleted".

Well, yes, the record was deleted.   That's kind of the point. 🙂   A record was deleted, and now I need to act on that deletion (by updating a count that will get stored into another table).   Though this is just a warning message, it seems to cause my script action to abort, as I've got a gs.info() call right at the top that never shows up in the log.

I've scoured Google for answers, but I see none.

I tried to work around this by splitting out the logic (supplied by the tutorial, mind you; I didn't come up with this stuff) so that the delete portion is triggered by a second business rule that fires before the record is deleted.   Because the events are handled asynchronously (it seems), it didn't make much difference because the record was still gone by the time the script action kicked in.

6 REPLIES 6

Joe McCarty1
ServiceNow Employee
ServiceNow Employee

The event is placed on the queue (sysevent table) with a reference to the record that generated it (instance field) and the table it came from (table field).   When the script action fires it queries the record to provide a context for current.   Since the record has been deleted it cannot provide the current record which is why you are getting the warning.



You could fire the event with a different glide record or even null with the deleted sys_id in one of the parameters.   The parameter would be available as event.parm1 or event.parm2.   But you won't be able to see the details of the record when it is deleted.   But depending on what you are doing the sys_id may be enough.



If you need the context of current you should probably use a business rule vs a script action.


Thanks for the background info...   As I mentioned, this is from the "Module: Events and script actions" part of the "Coding on ServiceNow" self-paced tutorials.   I don't think I need the context of "current" (at least not yet), but the next exercise or two may show otherwise.   I guess this warning is to be expected, then.   I'm about 98% sure I've done the exercise correctly (I've checked back through all my work a few times now), but the desired behavior (which is for the delete event to trigger a recount, and then update a related table) isn't happening.   I guess I'll keep checking.



Thanks again for the reply!


I just experimented a bit more...   I was able to confirm that if I leave in the references to the deleted records (in the gs.eventQueue calls), such that I wind up with "getEventTarget() called with invalid record reference ... could have been deleted" warning, the script action actually does abort (or maybe doesn't run at all; I can't tell, given what I see).   That is, even though there's only a warning (and I don't see anything labeled as "error" in the logs), the script action doesn't happen.



If I change the references to "null", the script action works as expected.



This seems like it might possibly be a bug, since the script action doesn't attempt to do anything with the record (i.e. I'm not asking for the sys_id to be dereferenced), and because having an "after delete" business rule seems like a pretty reasonable thing to want to use.



At the very least, it's something that ought to be addressed in this "Module: Events and script actions" part of the "Coding on ServiceNow".



Anyway, again, thanks for the reply.


Joe McCarty1
ServiceNow Employee
ServiceNow Employee

Just one point of clarification:   There is an after delete business rule (different from an event driven script action) where the current record is still in scope.   If you check the advanced button on the business rule page and select When 'after' and check the 'Delete' option.   Is this what you were looking for?



Screen Shot 2017-02-17 at 11.19.04 AM.png