How to get previous value of particular field for bulk amount of records using script.

preethiga
Kilo Explorer

I need to get previous value of fields for bulk amount of records in a table using background script.

For example, I want previous value of (managed by) field in cmdb_ci table for some 500+ records.

I tried using audit table to get previous value....and also that query works but I can't get any values for the records which are updated last month....so will the audit table restore every 30 days ?

Is there any other method to get previous value of record using script other than audit table ?

4 REPLIES 4

Allen Andreas
Administrator
Administrator

Hi,

To get the previous value you can script:
 var fieldprev = previous.FIELDNAMEHERE.getDisplayValue();

You haven't said what you want to do with it once you get it, but that's how you could get it and store it as a variable. And with that of course you'd need to be within your table that you're wanting. But you've let out details like...are you only wanting those updated last month? Life time? Want it just printed on the screen so you can copy/paste the results? etc...



Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Thanks for the reply Allen.

How can I use the below code in scripts-background because we can use previous object in only business rule right ?

 var fieldprev = previous.FIELDNAMEHERE.getDisplayValue();

I want previous value for the following reason,

For example, I need to update managed_by field of cmdb_ci table with its previous value  for the records having managed_by with value preethi, because due to some  reasons those cmdb_ci records were wrongly updated with value preethi. So now I need to correct it with  previous value using script.

 

Jay81
Tera Guru

Please refer below link for more info about audit tables.

 

https://docs.servicenow.com/bundle/jakarta-platform-administration/page/administer/time/concept/audited-tables-2.html

Thanks for the reply Jayantha.

Will check the document.