Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

Rolled back inserted record from table1 if record insertion fail in table2 in D365FO or AX2012

Posted on by 32
Hello Experts,
I am trying to implement a transactional logic in D365FO. I have two tables, table1 and table2, and I want to insert a record in both of them. However, I want to make sure that if the insertion in table2 fails, the insertion in table1 is also rolled back. How can I achieve this using X++ code?
 
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    Rolled back inserted record from table1 if record insertion fail in table2 in D365FO or AX2012
    Yes, that's how you've implemented your code. You can't see it by walking through execution of your code in debugger, if the behaviour isn't obvious to you just by reading your code.
     
    You don't throw any exception if validateWrite() returns false. You just skip the call of custTable.insert() and then you successfully commits the transaction.
     
    If you want to throw an error and get the transaction aborted, use the throw statement. For example:
    ttsBegin; 
    ...
    
    inventTable.insert();
    
    if (!custTable.validateWrite())
    {
        throw error("Operation aborted");
    }
    custTable.insert();
    
    ttsCommit; 
  • Shakoor Taj Profile Picture
    Shakoor Taj 32 on at
    Rolled back inserted record from table1 if record insertion fail in table2 in D365FO or AX2012
    Hi Martin,
     
    Thank you for your quick reply as usual.
     
    I have implemented the code as you suggested, below is my sample code.
     
    InventTable inventTable;
    CustTable   custTable;
    
        ttsBegin; 
        inventTable.initValue();
        inventTable.ItemId = 'Item1';
        custTable.initValue();
        custTable.AccountNum = 'Customer1';
    
        inventTable.insert();
        if(custTable.validateWrite())
            custTable.insert();
        ttsCommit; 
     
    This code run for InventTable and insert the new item "Item1" in the InventTable, but for CustTable insertion it check the validateWrite() and show me the error for Customer Group and not insert the record in the CustTable but it does not rollback the database change (InventTable).
     
    Any suggestion.
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    Rolled back inserted record from table1 if record insertion fail in table2 in D365FO or AX2012
    You need to use a concept called database transactions.
     
    If you run several database operations (e.g. inserts or updates) in a single transaction, they're considered a single operation that must either succeed or, in case of a failure, all changes must be rolled back.
     
    In your case, you can do something like this:
    Table1 t1;
    Table2 t2;
    
    ttsbegin;
    
    ... populate fields...
    t1.insert();
    t2.insert();
    
    ttscommit;
    The statements between ttsbegin and ttscommit form a single transaction. If an exception is thrown anywhere in the block, all database changes are automatically rolled back.
     
    This is a super-important topic, because if you don't use transactions correctly, you may cause data inconsistencies in your (your client's) production database, potentially with serious consequences.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans