The CreatorCon Call for Content is officially open! Get started here.

How to set a previous value of a field through Scheduled jobs

Mishu
Tera Expert

I am running a job to check for a state value that is on On Hold and the date given in a particular field matched todays date, then set the state field back to the previous value (i.e. state value which was before On Hold).

Please suggest.

1 ACCEPTED SOLUTION

Hi,

A simple solution would be to create custom field 'Previous state' on your table and set the value of this field using BR on State change.

A BR would be:

When - Before Insert/Update (condition = state changes)

use below line in script part 

current.u_previous_state = previous.state;

 

This will track the previous state and you can use it in your scheduled job.

 

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

8 REPLIES 8

Hi,

You can use History walker in script part.

You can use something like this:

var incGr = new GlideRecord('incident');
incGr.get('number', 'INC0000015');  // replace your query here
var currentState = incGr.state;
var oldState =incGr.state;
var hw = new sn_hw.HistoryWalker(incGr.getTableName(), incGr.getUniqueValue());
hw.walkTo(incGr.sys_mod_count);
do {
  oldState   = hw.getWalkedRecord().state; 
} while (hw.walkBackward() && currentState !=oldState )

 

this will give you old state.

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

@Anil Lande - Use this script in Flow designer or Scheduled Job?

 

Will it have any performance issues? As I checked HistoryWalker API uses the audit/history tables to generate a historical version of an existing record, and this table is really really huge, and query to this table is a real threat to the performance.

 

Hi,

If you are using flow the you can create Flow variable and set the value of Flow variable using above script.

If you are using scheduled job then you can use above script in your scheduled job to get previous value of incident.

Now there is no other way to get previous state, This is official API and we can use it. I don't see any issue. You can try running some script for limited no of tickets and see if it cause any performance issue.

 

https://developer.servicenow.com/dev.do#!/reference/api/rome/server/sn_hw-namespace/c_HistoryWalkerS...

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

@Anil Lande - Thanks for this info, I am opting for the first approach you suggested which is creating a custom field.