I have a batch job that is running regularly to adjust items.  Since this is running during working hours I want to stop the batch if it is blocking another user. This is the loop that my batch does.
[code htmlscript=”false”]FOR i := 1 TO 50 DO BEGIN
IF AmILocking(LockingForUserID) THEN BEGIN
Text001 is “Locking for user id %1, quitting !” and AmILocking function is
[code htmlscript=”false”]AmILocking(VAR LockingForUserID : Text[30]) Locking : Boolean
Locking := Session.FINDFIRST;
LockingForUserID := Session."User ID";[/code]
where Session is the virtual table Session as a local variable.

5 thoughts on “Batch Job to Quit if it is blocking

  1. Peter says:

    I don’t get it 🙁
    The code tries to call AdjustItems 50 times, unless someone is getting blocked by anything??

    I’ll recommend that you in the AdjustItem add a COMMIT everytime it is safe to do, i.e. per Item. If that still blocks for too long, you could add a call to the function once in a while. But the function should only throw an ERROR if there exists sessions that have been waiting for your session-id for at specific time – i.e. 3 seconds.
    (First find “My Session” and then use “Blocking Connection ID” and “Wait Time (ms)”).

    1. Each time it calls AdjustItems I only adjust 10 Items. That number can be modified.

      IF InvtToAdjustExist(TempItem) THEN BEGIN
      InvtAdjmt.SetProperties(TRUE,InvtSetup.”Automatic Cost Posting”);

      InvtToAdjustExist(VAR ToItem : Record Item) : Boolean
      WITH Item DO BEGIN
      SETCURRENTKEY(“Cost is Adjusted”,”Allow Online Adjustment”);
      SETRANGE(“Cost is Adjusted”,FALSE);
      i := i + 1;
      UNTIL (NEXT = 0) OR (i = 10); // Maximum 10 Items

    2. I agree that it is possible to study the blocking further, but it was my choice to have this simple and fast since this job is executed by NAS every 10 minutes.

  2. tulasiaryal says:

    Hi Gunnar,
    I have a problem similar to this. I have created a scheduling job which will import the sales orders and post it. Everything will be done under NAS. it repeats in every 10 minutes and it takes about 1-5 minutes to complete this process. Can You suggest where can I insert this type of statement so that other Users will not be locked for Item Ledger Entry table. I appreciate your comments.

    1. Hi

      I would suggest that you move all the invoice posting to the job queue. I have had great success with that functionality.

Leave a Reply

%d bloggers like this: