Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to lock a record in a script?

David165
Mega Expert

Hi

I need to write a script that is executed whenever a record is inserted into my "main" table. Based on some criteria the user has selected in the form I need to

  1. Query a "counters" table
  2. Increment the counter value
  3. Update the counters table with the new maximum value
  4. Use the counter value in the record I'm inserting

All of the above I'm OK with. What I want to know is; how do I lock the row in the counters table to prevent another user from performing the same update at the same time and getting the same counter value as me?

In pure SQL terms I would use a "select for update" on my counter table to lock the row. But I have been unable to find an analogous gliderecord method to do something similar.

I could create a workflow and use a workflow lock, but this seems like overkill for something I should be able to do in a single small script.

Anyone have any suggestions?

Regards

David

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi David,

If you are saying that single field only 1 user should update.

What if 2 users sessions are running and 2 BRs run at same time both the user session will try to update that field

This way you cannot lock that field I believe so.

Try using workflow lock script once.

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi David,

If you are saying that single field only 1 user should update.

What if 2 users sessions are running and 2 BRs run at same time both the user session will try to update that field

This way you cannot lock that field I believe so.

Try using workflow lock script once.

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

David165
Mega Expert

Thanks for your reply.

I have proceeded down the workflow route using a workflow lock to ensure only one user at a time updates the record. It seems a bit of a "sledgehammer to crack a nut", but if that's the accepted way to do it, who am I to argue.

 

Regards

David

Hi David,

We don't know whether that's the only way to do that because this solution would incur extra performance impact on the instance since for every record a workflow context will run

May be you can check with ServiceNow team on better approach

Regards

Ankur

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