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

Roll back business events if creation/update/delete fails

(0) ShareShare
ReportReport
Posted on by 1,965

Hi,

What's the best place to put code inside the transaction of creating the customer/updating the customer and deleting the customer? 

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Hi IntegrationBeginner,

    I would use Chain of Command for insert/update/delete methods of CustTable.

    This way you can wrap the next() call and your business event creation in the same transaction, presumably making them fail or succeed together.

  • .. Profile Picture
    1,965 on at

    Hi Nikolaos,

    But calling the event after the next() of insert/update and delete will be after the transaction finishes.

    Also what if i want to call the event based on the update of certain fields of custTable.. Where should i call the event so that it is rolled back

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Hi IntegrationBeginner.

    I think you might have missed part of my suggestion. I suggested to wrap your code and the next call in the same transaction.

    Also, if you want to catch what fields were updated, update() method is the correct place your code as well.

    If you have any further questions, could you please try first my suggestion, and then share your code (which you wrote based on the suggestion) if you have issues with it. This way we can be sure that you didn't miss any details. Thanks!

  • .. Profile Picture
    1,965 on at

    Hi Nikolas,

    May i know what you mean by wrapping my code and next with the same transaction?

    Do you mean the following? (let's say I made an extension the insert method of the custTable table)

    Ttsbegin;
    
    Next();
    
    CallEvent() ;
    
    Ttscommit;

    If yes, Wouldn't the next already contain a transaction which will be commited even if the event calling fails. and if the insert fails wouldn't an error be thrown and the code won't reach my event piece of code? so what's the difference if I add the ttsbegin/ttscommit or not?

    2. For the update, how can i determine on which fields to send the notification.. How can i know that their values changed and sent event accordingly?

    3.For the delete, if i'm going to fire the event.. Wouldn't the record be already deleted? So how can i send the customer account value in the event? 

  • Verified answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Hi,

    1. The transaction is committed when the last ttscommit is called. You can easily test and play around after reading the documentation: docs.microsoft.com/.../transaction-integrity

    This topic is very important to understand so that you can write code that will not leave behind orphan data.

    2. Check standard update methods, for example CustTable.update(). You can access the original table buffer with this.orig() and compare to the values of the current "this".

    3. The record is deleted in super() call of the delete method, hence your logic should run before it while you can still access the buffer.

  • Suresh M. Parmar Profile Picture
    340 on at

    Hi There,

    If you want to track the record updates history, like for customers on create/update/delete transaction will be captured.

    You can use database log functionality in the system, I think your requirement will be fulfilled.

    Path: System Administration > Setup> Database log

    Please check the reference: Database log setup

    Thanks,

    Suresh Parmar

  • .. Profile Picture
    1,965 on at

    Hi Nikolas,

    I've done the following:

    [ExtensionOf(tableStr(CustTable))]
    final class CustTableX_Extension
    {
        public void insert(DirPartyType _partyType, Name _name,boolean _updateCRM)
        {
            ttsbegin;
    
            next insert(_partyType, _name, _updateCRM);
            CustChangesBusinessEvent::newFromCustTable(this,'insert').send();
    
            ttscommit;
        }
    
        public void delete()
        {
            ttsbegin;
    
            next delete();
            CustChangesBusinessEvent::newFromCustTable(this,'delete').send();
    
            ttscommit;
        }
    
        public void update(boolean _updateSmmBusRelTable, boolean _updateParty)
        {
            ttsbegin;
    
            next update(_updateSmmBusRelTable, _updateParty);
            CustTable custTableOld = this.orig();
            if(custTableOld.Currency != this.Currency ||
                custTableOld.PaymTermId != this.PaymTermId ||
                custTableOld.TaxGroup != this.TaxGroup ||
                custTableOld.name() != this.name() ||
    
            {
                CustChangesBusinessEvent::newFromCustTable(this, 'update').send();
            }
    
            ttscommit;
        }
    
    }
    

    I think i did what u suggested. However,

    1. For the delete, you mentioned that i should send the event before the super, but i called it after the super with wrapping it with the transaction and it works

    2. As you noticed, I'm using the same business event, But i added a new flag to the contract that specifies the action type. Is this a good practice, or should i build a new event for each action (create/update delete)?

    3. Let's say i now want to detect changes for the following

       3A: customer primary delivery postal address (Create,update and delete)

       3B: customer primary electronics address (create update and delete)

       3C: when we create a new contact for the customer , or delete it , or update certain field in it
       3D: when we create a new primary electronics address  for the customer contact or delete it , or update certain field in it

    what would be the best place for each one and  should i create a new business event for one of those 4 cases? 

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Hi IntegrationBeginner,

    I believe we have now found answers to your original question. Do you agree?

    Now you have new questions around many different topics (best practices around business event contract design, change tracking of customer contact info...). I suggest you start with the usual path: search for documentation, discussions etc, and if you can't find information, post new questions on this forum.

  • .. Profile Picture
    1,965 on at

    Hi Nikolas,

    Thank you so much for the help. I posted a new question

    community.dynamics.com/.../change-tracking-for-customer-contacts-and-their-primary-electronic-address-using-business-events

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 663 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 348 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans