Scheduled job to update state field

Service Manager
Kilo Guru

Hi All,

I need help to create a scheduled job to update state field on task table as per the Request state field on Sc_request table.

Suppose if the Request state on Request table is closed complete then update the State field on task to closed completed too.

Scheduled job is to check what all request are not updated correctly and update them.

For instance : we have few Requests records with "Request state" as closed complete but the state field on task table remain "Pending", those request records need to be updated

Thanks!

1 ACCEPTED SOLUTION

1) Before Business Rule

2) Set the conditions as per your query i.e state = -5 and request state is completed in condition builder of business rule

find_real_file.png

3) Try the below script, update as per requirement.

var app=new GlideRecord('YOURTASKTABLENAME');
app.addQuery('number',current.number);
app.query();
if(app.next())
{
//Make use of setWorkflow false if required.
app.state = '3';
app.update();
}
}

Hope this helps!

Please Mark as ✅ Correct if this solves your issue and also mark (y) Helpful if it helps resolve your problem.

Thanks,
Saji

View solution in original post

10 REPLIES 10

BryanS413339635
Tera Guru

Is there a reason you're wanting to use a scheduled job? I'm assuming this occurred due to some error that has since been corrected (this happened to us before too). The better solution in my opinion would be to create business rule(s) to run on close of the record to keep things in sync. Then perform a 1 time cleanup to fix those that are currently incorrect. 

 

If my reply has helped in any way, kindly mark it correct/helpful. Thanks!

Yes, our final solution is to create Business Rule and scheduled job will be one time clean-up.

Can you share the business rule and Background script.

Sajilal
Mega Sage

Please share your script here for assitance, and maybe make use of Business rule instead of Scheduled Jobs.

Thanks,

Saji

We are planning to create Business Rule but for the one time clean-up we need Background script/scheduled job.

Background script worked as expected.

var app=new GlideRecord('sc_request');
app.addQuery('state','-5');
app.addQuery('request_state','closed_complete');
app.setLimit(2);
app.query();
while(app.next())
{
gs.print('total '+app.getRowCount());
gs.print(app.number);
if (app.request_state == "closed_complete") {

app.state = '3';

app.update();
}
}

Need Help with a Business Rule now.

Thanks!