Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

after state moved to Resolved need to closed the form state as closed after 30 days

Not applicable

in table table form after state moved to Resolved need to closed the state as close after 30 days, how can i achive ,

i need to use schedule job or what, 

any one provide me the solution or souce code for this.

1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@Community Alums 

You can write a schedule job which runs on a daily basis, below is the sample script you can use:

// Find records in the table where state is "Resolved" and resolved date is more than 30 days ago
var gr = new GlideRecord('your_table_name');
gr.addQuery('state', 'resolved');/ /replace with resolved backed name
gr.addQuery('resolved_date', '<', gs.daysAgoStart(30));
gr.query();

while (gr.next()) {
    // Update state to "Closed" and perform any additional actions if needed
    gr.state = 'closed'; // replace with closed backed name
    gr.update();
    
    // Log the updated record
    gs.info('Closed record ' + gr.getDisplayValue() + ' which was resolved more than 30 days ago.');
}

 

 

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

View solution in original post

3 REPLIES 3

Maddysunil
Kilo Sage

@Community Alums 

You can write a schedule job which runs on a daily basis, below is the sample script you can use:

// Find records in the table where state is "Resolved" and resolved date is more than 30 days ago
var gr = new GlideRecord('your_table_name');
gr.addQuery('state', 'resolved');/ /replace with resolved backed name
gr.addQuery('resolved_date', '<', gs.daysAgoStart(30));
gr.query();

while (gr.next()) {
    // Update state to "Closed" and perform any additional actions if needed
    gr.state = 'closed'; // replace with closed backed name
    gr.update();
    
    // Log the updated record
    gs.info('Closed record ' + gr.getDisplayValue() + ' which was resolved more than 30 days ago.');
}

 

 

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Not applicable

HI @Maddysunil ,

Thanks for your reply,

 

with if condition we willa check and achive right, like this 

 

var rd=gr.getValue('resoved_date');

var days=gs.daysAgoStart(30);

//var days=gs.daysAgo(30);

if(rd <30)

{

//logic

}

@Community Alums 

If you are suing this line 

gr.addQuery('resolved_date', '<', gs.daysAgoStart(30));

then no need of checking in if , But if you are not using above line then inside while loop you can use if.