Return the Previous field value using GlideRecord Query

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2012 09:58 AM
In a Business Rule, we have access to the current values in a field (current.fieldname) as well as the previous values (previous.fieldname). The fields for which we can access the current and previous values are those on the table against which the Business Rule is running.
That's all well and good, but what if I'm running a business rule on one table (e.g. Incident) which queries records in another table (e.g. cmdb_ci_computer)? Assuming the query returns one or more records from the Computer table, is there any way I can get the previous values that existed in the returned Computer record(s)?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2012 10:10 AM
I am not aware of any query. But you can get data you are looking for by querying "sys_audit" table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2012 10:10 AM
When you say "previous" for the returned Computer record(s), do you mean their audited values? That is stored in the sys_audit table and you would have to run a gliderecord query to get that. However that table is large usually, and this might result in performance degradation.
Or do you mean for "previous", the previous computer record you just updated in the Business Rule? Are you trying to store that somewhere else or display it in a message? If you are trying to do that, store the computer record in a variable before you update it.
Or do you mean for previous, there are two business rules, one is updating the Computer Record, the other is updating incident. Maybe you combine those Business Rules.
It might help if you post your Business Rule here.
Mike

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2012 10:37 AM
I was hoping to get away without querying the sys_audit table to get the previous values in the fields on the computer table, but I suspected that might be the only way to do it. In my perfect Service-now world, there would be a method like gs.getPreviousValue() that would return the value which existed in the field prior to the current value in that field. So, something like this:
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('hardware_status', 'active');
gr.query();
while(gr.next()) {
var currSN = gr.serial_number; //get the current Serial Number
var prevSN = gr.serial_number.getPreviousValue(); //get the previously recorded Serial Number
}
I suppose it's nice to dream...
Thanks again.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2012 12:26 PM
Querying the sys_audit table isn't a good option for performance reasons. At least in the current release of ServiceNow.
However, you could copy the last value into another field with a Business Rule, however that is a slippery slope. You would probably be asked to copy all kinds of fields into "lastvalue" fields. Then you are kind of replicating the sys_audit functionality. If it is a small collection of fields then it might be ok though.