- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2016 06:36 AM
In all of my business rules, I just discovered that current.sys_id is not being sent to the script include the business rule triggers. Here's an example of the business rules I'm using.
function onBefore(current, previous) {
//This function will be automatically called when this rule is processed.
var id = current.sys_id;
gs.debug("business rule - update invoice id : " + id);
if (current.state == "draft" || current.state.nil() ) {
var myInvoice = new InvoiceUtility();
myInvoice.updateFields( id );
}
}
I also tried with the .toString() method:
function onBefore(current, previous) {
//This function will be automatically called when this rule is processed.
var id = current.sys_id.toString();
gs.debug("business rule - update invoice id : " + id);
if (current.state == "draft" || current.state.nil() ) {
var myInvoice = new InvoiceUtility();
myInvoice.updateFields( id );
}
}
the gs.debug returns "business rule - update invoice id : "
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2016 07:09 AM
I tested this on Fuji and on Helsinki and don't believe that before/query business rules actually know about current. I don't think they should either. The thing to note about query rules is they run before EVERY hit to the database for that table. lists, forms, reference fields, you name it, if the system is going to do a select statement from that table, the before query rule is run first. Normally this is used to do some filtering on the records before they are retrieved, not processing as in your case.
You may want to uncheck the QUERY checkbox and check INSERT or UPDATE instead? Perhaps?
Business Rules Best Practices - ServiceNow Wiki

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2016 07:09 AM
I tested this on Fuji and on Helsinki and don't believe that before/query business rules actually know about current. I don't think they should either. The thing to note about query rules is they run before EVERY hit to the database for that table. lists, forms, reference fields, you name it, if the system is going to do a select statement from that table, the before query rule is run first. Normally this is used to do some filtering on the records before they are retrieved, not processing as in your case.
You may want to uncheck the QUERY checkbox and check INSERT or UPDATE instead? Perhaps?
Business Rules Best Practices - ServiceNow Wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2016 07:19 AM
It looks like you're right. I'll need to figure out how the records will be kept correct in relation to referenced records going forward.
Thank you for your help.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2016 07:21 AM
Yeah, you probably don't want to do that every time a record is read (query)... Update sounds like a better approach.
Let me know if that answered your question. If so, please mark it as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.
If you are viewing this discussion from your "inbox", use the "mark as correct" option under actions. If you are viewing it directly from the thread use the Correct Answer link (red with a star).
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2016 06:51 AM
Michael - it looks like you are running it before insert. I have never tested this - but does the sys_id exist before insert, I always thought it was created at insert, but maybe I am wrong? try running it after insert or on an existing record and it should work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2016 06:58 AM
It's running before a query.