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

Hi

Create a BEFORE UPDATE BR and copy the main part of your script to the script of the BR, where you check the 'advanced' checkbox.

Instead of opening o new GlideRecord, the 'current' variable holds the current record, to set the state to '3'.

Make sure to attach the BR on the 'sc_request' table.

Let me know if that answers your question and mark my answer as correct and helpful. 

BR

Dirk

 

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

Is it possible to make it via client script?

because state and request are available on sc_Request table and we have to update the Request sate based on state.

Suppose if request state is closed cancelled then state is also cancelled and if request state is closed complete then state is also closed complete. Kind of if else conditions.

Thanks

Hi

As a general rule of thumb:

> Do not trust the client, only trust the server!

I recommend to go with your initial idea to have a BR taking care of future records (BR runs on Server side) and do a one-time clean-up of your data using a Server Side Script.

Note:

You can use a Scheduled Job for this clean-up (if you think this will occur once more, that you need to clean-u. Why ever!).

But I you just want to run this ONCE, just create a "FIX SCRIPT" for it, and run it once from there. Alternatively, you can just run it from "Scripts - Background".

But I am going the following way:

> I have a Script Include for "Clean-up Tasks", so that I can put in a new function for each clean-up I need to do. There I can document quite nice, what was fixed, when and why!

I also know exactly, where to search for all the clean-ups done so far.

Then, I call this function for the one-time clean-up, which can be done by a fix-script or Scripts - Background as well.

Let me know, if you already succeeded in solving your issue, or if I can help you in digging more into the details to solve.

If the question is solved, mark the best matching and helpful answer as correct, to close this thread. This will also help others to identify, if you need further help or not.

Have fun & BR

Dirk

 

mr18
Tera Guru

Just wondering request table extends task table and state field is actually on task table. So it should give correct values whether on request or task table.

However, try  below code

var req = new GlideRecord("sc_request");
req.addQuery("state", 3); // 3 is value for closed complete
req.query();
while(req.next()) {
var gr = new GlideRecord("task");
gr.addQuery("sys_id", req.sys_id);
gr.addEncodedQuery("numberSTARTSWITHREQ^state!=3");
gr.query();
while(gr.next()) {
gr.state = req.state;
gr.update();
}
}