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 :
Dynamics 365 Community / Forums / Finance forum / inner try catch is not...
Finance forum
Unanswered

inner try catch is not being hit

(1) ShareShare
ReportReport
Posted on by 252
Hi,

Let's say i have this code below. In line class1.run() there is code that inserts into different number of tables. But in some cases when this class1.run() gets called, we'll need to call an api in addition to inserting into some tables, and sometimes the api response errors and I don't want to return 404 not found, i want to return what the api actually returns which is
error_code:123
error_message: this input is not found

However, when i put try and catch (webEx) when calling the api, it doesn't get called, because this code here has ttsbegin, ttscommit -- what to do in this case? calling run and updating table in case of success needs to be in the same transaction
try
{
    ttsbegin;
    Class1 class1 = Class1::NewFromType(“Type1”);                 
    class1.run(table1);
    this.updateTable1InSuccess(table1);
  
    ttscommit;
}
catch(ex)
{
    errMsg = ex.Message? ex.Message : this.getInfologData(errLine);
    this.updateTable1InFailure(table1, errMsg);
}

    public  void updateTable1InSuccess(Table1 _table1, common _table)
    {
        ttsbegin;

        _table1.selectForUpdate(true);

        _table1.RefTableId = _table.TableId;
        _table1.RefRecId   = _table.RecId;

        if (_table1.Status == XStatus::New)
        {
            _table1.Status     = XStatus::Processed;
            _table1.Message    = '';
        }

        _table1.update();

        ttscommit;
    }


    public  void updateTable1InFailure(Table1 _table, str _errorMsg)
    {
        ttsbegin;

        _table.selectForUpdate(true);

        _table.Message  = _errorMsg;
        _table.Status   = XStatus::Error;

        _table.update();

        ttscommit;
    }

 
  • Martin Dráb Profile Picture
    241,515 Most Valuable Professional on at
    This is just one of reasons why this design isn't good and should be reworked. Another problem is that the API may be unresponsive and the database transaction will be open (potentially blocking other business processes) until the request times out.
  • Martin Dráb Profile Picture
    241,515 Most Valuable Professional on at
    I'm sorry, but I can't design the business process and the technical solution without knowing anything about the business requirements.
     
    In general, you should try to avoid all web service calls from database transactions. Split the process in some way. For example, you can create records in a special status (e.g. ToBeSent) and have a separate process (e.g. a SysOperation task with RelaliableAsynchronous execution mode) calling the API and updating the status accordingly. That will allow you to implement the exception handling you need, it'll solve the excessive lock duration, you'll get the right status in the end while the DB will be in a consistent state all the time.
  • Martin Dráb Profile Picture
    241,515 Most Valuable Professional on at
    As I said, I don't have enough information to be able to design a solution. All you explained is your particular technical solution, but because it doesn't work, you'll need to analyze business requirements and design a different solution.
     
    All I can give you are some ideas, but without knowing your business requirements, it's impossible for me to decide whether any idea matches those unknown requirements.
     
    A possible way of implementing "i take the values coming from the api and use it to insert values into some tables" is doing exactly what is says: you'll call the web service first and only then you'll begin a transaction and write data to database.
     
    Regarding the locks, insertion of data isn't usually a problem, but the code you showed us updates records as well. More precisely, the code shows only updates (and call of run() method with code unknown to us).

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 > Finance

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans