If the state is on hold for more than 24 hrs it should be set as In progress.

Saikeerthana
Tera Contributor

Can anyone please tell me what is the mistake with this code.

I am trying to update state field in the Incident table. If the state is on hold for more than 24 hrs it should reset to In progress.

The code is working. I am able to see the log messages in the sys_log table but the state field in the Incident table is not getting updated to In Progress.

This is a before business rule.

**************************************************************************************************************************************

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var gr = GlideRecord('incident');
    gr.addQuery('state', '3');
    gr.query();
    while (gr.next()) {
        var updated_date = gr.sys_updated_on;
        var present_date = new GlideDateTime();
        var gdt1 = new GlideDateTime(updated_date);
        var miliseconds = GlideDateTime.subtract(gdt1, present_date).getNumericValue();
        var total_hours = parseInt(Math.floor(((miliseconds / 1000) / 60) / 60));
        gs.log("state before update = " + gr.state);        //state before update=3
        if (total_hours >= 24) {
            gr.setValue('state', '2');
            gs.log("state updated= " + gr.state);              // state updated=2
        }
    }

})(current, previous);

 

Thanks in Advance.

 

1 ACCEPTED SOLUTION

Saiganeshraja
Kilo Sage
Kilo Sage

For this, you need to use a scheduled job that runs daily:

var s= new GlideRecord("incident");
	s.addEncodedQuery("state=3^sys_updated_on<javascript:gs.beginningOfToday()");
	s.autoSysFields(false); // use if you don't want update system fields 
	s.setWorkflow(false); 
	s.query();
	while(s.next())
		{
			s.setValue("state",2);
			s.update()
		}

Mark Correct and helpful

View solution in original post

2 REPLIES 2

Saiganeshraja
Kilo Sage
Kilo Sage

For this, you need to use a scheduled job that runs daily:

var s= new GlideRecord("incident");
	s.addEncodedQuery("state=3^sys_updated_on<javascript:gs.beginningOfToday()");
	s.autoSysFields(false); // use if you don't want update system fields 
	s.setWorkflow(false); 
	s.query();
	while(s.next())
		{
			s.setValue("state",2);
			s.update()
		}

Mark Correct and helpful

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can also achieve this using flow designer with no script.

Did you explore that?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader