How to get the current and previous values of field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-27-2022 04:07 AM
I have one requirement
How to get the current and previous values of field
Please Let me know how to do that
- Labels:
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-27-2022 04:29 AM
Hi Venkatesh,
If the table is audited then you can get the details from history table i.e. for particular field what is current value and what is previous value
go to sys_history_line.list
search with lable as your field name
add query for old value as closed complete and new value as open.
OR
You can opt for script as example below just change it as per your requirement:
Sys Audit table stores the old and new values. you can query the incident table and sys audit and can try to get this data. here is the script which you can try at your end.
var gr = new GlideRecord("incident");
//first get all current p3,p4 and p5 incidents from incident table.
gr.addQuery("priority","IN","3,4,5");
gr.query();
while(gr.next()) {
var gr1=new GlideRecord("sys_audit");
gr1.addQuery("tablename","incident"); //filter by incident.
gr1.addQuery("documentkey",gr.sys_id);
gr1.addQuery("fieldname","priority");
gr1.addQuery("oldvalue","IN","1,2"); //check if the odl value of incident is p1 or p2.
gr1.orderBy("created"); //sort by created date
gr1.setLimit(1);
gr1.query();
if(gr1.next()) {
gs.print("Incident number is "+gr.number+"--"+gr.priority);
}
}
Mark my answer correct & Helpful, if Applicable.
Thanks,
Sandeep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-16-2024 01:01 PM
I´ve modified it a little and use for simple metric - our ServiceNow owner needs to see only changes on Priority field (we don´t have PA) so maybe someone could use it too...
var gr1 = new GlideRecord("sys_audit");
gr1.addQuery("tablename","incident"); //filter by incident.
gr1.addQuery("documentkey", current.getUniqueValue());
gr1.addQuery("fieldname","priority");
gr1.orderByDesc('sys_created_on'); //sort by created date
gr1.setLimit(1);
gr1.query();
if(gr1.next()) {
var old_priority = gr1.oldvalue;
var new_priority = gr1.newvalue;
var start = gr1.sys_created_on;
createMetric(old_priority,new_priority,start);
}
function createMetric(old_priority,new_priority,start) {
var mi = new MetricInstance(definition, current);
var gr2 = mi.getNewRecord();
gr2.value = old_priority;
gr2.start = start;
gr2.calculation_complete = true;
gr2.insert();
var gr3 = mi.getNewRecord();
gr3.value = new_priority;
gr3.start = start;
gr3.calculation_complete = true;
gr3.insert();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-27-2022 04:31 AM
Hi,
where do you wish to get those?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader