web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Business Central forum

What is the best way to handle concurrent issue

(0) ShareShare
ReportReport
Posted on by

Hi All,

I noticed that during a method is processing. The 365 will lock the related table. In other words, all the change will in staging and push the change when the method is finished.  So, another user will see the error said some table is locked by another user. I think "commit" the change immediately could solve this issue. However, it need to roll back the change if error happened and it is very difficult to catch the error throw by 365. So, my question is what is the best way to handle concurrent issue during extesnion development.

Thanks in advance.

Categories:
I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at

    Hi Ruining,

    Well just to give you an idea how to catch error (or avoid raising an error blocking your processing task) is to use the OnRun method of a CodeUnit:

    CLEARLASTERROR();  
    If Not Codeunit.RUN Then begin  
      MESSAGE('The last error was: ' + GETLASTERRORTEXT);
      logTable.ErrMsg := GETLASTERRORTEXT;
      logTable.Insert; 
    end;


    So putting all your code that could generate an error inside the onRun function of a codeunit and using the code above permits you to avoid blocking your processing task. The GETLASTERRORTEXT as well permit you to get the last error generated in the Run function. You can use this for example if like keep a trace of errors i a log table,...

    --

    Even if you use this method it won't resolve your locking table problem because you could have many tansactions (inserting, modifying,..) of much tables inside the Run method that makes it very complicated to roll back.

    --

    What I have in mind now that I can suggest you to use is the Sleep function. Let's imagine that when you execute this code:

    ............
    ItemRec.insert();


    you get the error saying the table Item is locked. So to avoid that I suggest you to use this code instead:

    ..........
    If not ItemRec.Insert() then begin
     Sleep(3000); // equivalent to 3 seconds
    
     If not ItemRec.Insert() then begin
      Sleep(3000); // equivalent to 3 seconds
    
       If not ItemRec.Insert() then begin
        Sleep(3000); // equivalent to 3 seconds
        ItemRec.Insert(); // this will raise the error if unable to insert, to avoid it use If condition
       end;
     end;
    
    end;


    I hope this could help you and give you an idea.

    --

    Don't hesitate If you need more details or explanations.

    -------------

    Please verify my answer if you find my answer helpful. Doing so you'll show other community members that there was found a solution and you credit my help.

    -------------

    Best Regards,

  • Community Member Profile Picture
    on at

    Thank you for your replying, I will try that. Much appreciate!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Business Central

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans