Hi Guys
I'm still trying to find better ways to do stuff in BC AL. I did a mistake in a loop and loop didn't stop so I had to delete records using something like below:
SalesHeader.SETRANGE("Document Type",SalesHeader."Document Type"::Invoice);
IF SalesHeader.FINDSET THEN
SalesHeader.DELETEALL(TRUE);
Now the I got an error saying table is locked and all got stuck. Now I have 2 questions:
1. Is there a statement to delete efficiently without locking table or some kind of NOLOCK functions?
2. On the loop, how do you set an upper limit for the loop if my code is bad and I know it will never go to 100000 records
0
Answers
OnDelete triger Locking Sales lines table SalesLine.LOCKTABLE; plus its lot of records it will Lock Sales Header anyway . You need delete 100 000 records ?
SalesHeader.SETRANGE("Document Type",SalesHeader."Document Type"::Invoice);
LoopQty:=0; //Integer variable
IF SalesHeader.FIND('-') THEN
REPEAT
LoopQty+=1;
SalesHeader.DELETE(TRUE);
UNTIL (SalesHeader.NEXT= 0) OR (LoopQty=100) ;
RIS Plus, LLC
If a very large number of orders needed to be deleted, consider build a process that deletes each order as a separate transaction. It should be able to skip any that result in errors (perhaps logging the error) and continue with the next. It should also be able to be restarted where it left off in the event of a failure.
NOTE: Since the table has an OnDelete trigger being called, the two code examples are basically the same.
1. Not Locking tables for too long irrespective of good code
2. Not blowing up the system due to bad code - this I can use a limit to the loop as per Audrius but in code that create the orders in the first place so we don't end up with 100,000 records
I think it's Murphys law - if something can go wrong it will
Normally if I have a custom tables I use SQL Query to delete objects.
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/
If I don't run the table triggers then I get orphaned sales lines when deleting sales header ..looks like when deleting un-posted sales invoice you need to run table triggers