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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

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

(0) ShareShare
ReportReport
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?
 
I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    237,878 Most Valuable Professional on at
    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.
  • Shakoor Taj Profile Picture
    32 on at
    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
    237,878 Most Valuable Professional on at
    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; 

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 646 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 529 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans