Scheduled Script is updating only 1 record each runtime, not mutliple records

subhadeep1618
Tera Guru

I have the below scheduled script with the objective of updating multiple records that match the given criteria.

It is scheduled to execute every 10 seconds.

In Run As field, I have given my name (I have admin access).

 

subhadeep1618_0-1690693667712.png

The script text of above screenshot is also given below:

var gr = new GlideRecord("cmdb_ci_service");
gr.addEncodedQuery(
    "service_classification=Project^end_date<javascript&colon;gs.beginningOfToday()"
);
gr.query();
while (gr.next()) {
    gr.setValue('operational_status', '6');
    gr.update();
}

But what is happening is, each time only 1 record is getting updated.

If I manually click Execute Now, then also only 1 record (1st record of GR object) is getting updated, but the rest are remaining with old value of the operational status field.

Please help me to identify what is the issue here.


Please mark this post as a solution and also as helpful, if this resolves your issue or query.

Thanks,
Subhadeep Ghosh.
6 REPLIES 6

Niklas Peterson
Mega Sage
Mega Sage

Hi,

What does the service_classification and end_date values look like for the records you expect to be returned?

 

Regards,
Niklas

OlaN
Giga Sage
Giga Sage

Hi,

Did you check if the query returns more than one result?

Also, if your use case is to update records that is before a given date, it seems excessive to run the script every 10 seconds, once a day should be enough?

On another note, you can do this with a Flow that runs on a schedule, no scripting required. Ask if you need help with examples to get started with Flow Designer.

Hi @OlaN 

Query is returning more than 1 result - I have done the checking with script include.

But the issue is every 10 seconds only 1 row is getting updated.

Say for example, query returns 4 records.

In 1st execution (after 10 secs) - 1st record gets updated, 2nd execution (after 20 secs) - 2nd record gets updated.

But I want all 4 records to get updated at once - this is happening in my PDI, but in customer platform, this weird issue I'm facing.

I know I have to run it daily once, but this testing I am doing in non-prod environment, hence 10 secs.

If possible, pls give me an example of flow designer - that will be really of great help.


Please mark this post as a solution and also as helpful, if this resolves your issue or query.

Thanks,
Subhadeep Ghosh.

Sure thing, here you go, some smaller modifications will be needed since I don't have the options that exist in your instance, but I'm sure you get the idea.

After you've created the Flow, test it, and examine all records have been changed as intended.

Flow-update-service-records.png

 

Step 1 expanded, this will require some change in the query to fit your needs.

Flow-update-service-records-step1-expanded.png