Batch Job to Quit if it is blocking

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
COMMIT;
ERROR(Text001,LockingForUserID);
END;
AdjustItems;
END;[/code]
Text001 is “Locking for user id %1, quitting !” and AmILocking function is
[code htmlscript=”false”]AmILocking(VAR LockingForUserID : Text[30]) Locking : Boolean
Session.SETRANGE(Blocked,TRUE);
Locking := Session.FINDFIRST;
LockingForUserID := Session."User ID";[/code]
where Session is the virtual table Session as a local variable.

5 Replies to “Batch Job to Quit if it is blocking”

  1. 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.

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

      InvtToAdjustExist(VAR ToItem : Record Item) : Boolean
      WITH Item DO BEGIN
      RESET;
      SETCURRENTKEY(“Cost is Adjusted”,”Allow Online Adjustment”);
      SETRANGE(“Cost is Adjusted”,FALSE);
      IF FIND(‘-‘) THEN REPEAT
      CopyItemToItem(Item,ToItem);
      i := i + 1;
      UNTIL (NEXT = 0) OR (i = 10); // Maximum 10 Items
      EXIT(NOT ToItem.ISEMPTY);
      END;

  2. 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.

Leave a Reply