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, ...
Suggested Answer

Problem sending JSON after rollback in SalesInvoiceJournalPost_Extension class

(5) ShareShare
ReportReport
Posted on by 16

Hi community,

 

I'm facing an issue with a JSON integration to an external API. I’ve already implemented the logic, and it works perfectly under normal conditions. The JSON is sent from the SalesInvoiceJournalPost_Extension class once the sales invoice is posted.

 

The problem arises when a user cancels the process at the last step. In that case, the system performs a rollback of the transaction, but the JSON has already been sent to the external service, and there’s no way to cancel or undo that action.

 

I’ve looked into other classes and possible approaches to prevent this behavior, but I haven’t been able to find a suitable solution. I’ve also read that some people resolve this by using a batch job that runs periodically to send the documents. However, my goal is to keep the integration sequential and immediate, meaning the JSON should only be sent after the invoice is fully and successfully posted — avoiding any chance of rollback.

 

Has anyone encountered a similar scenario or can point me in the right direction for handling this properly in D365FO?

 

Any help or suggestions would be greatly appreciated.

 

Thanks in advance!

Categories:
I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at
    This is a common mistake when designing integration. Your requirement is to "sent after the invoice is posted", but your code doesn't do it. Until the transaction is committed, the invoice isn't really posted.
     
    Sending from a transaction is dangerous; not only you must always think about rollbacks, but a problem with the web service can also slow down your ERP system (when the call is slow and prevents the transaction from committing and locks from releasing) or even block the business (invoices can't be posted if the call throws errors).
     
    Therefore calling the web service from a transaction isn't a good idea.
     
    In some cases, you can simply call it after the transaction. It solves all the problems above, but it doesn't guarantee that the message will get send (an error can occur). If you want a more robust solution, use a table to store information about what should be send. Write into it in a transaction; if a rollback occurs, the record will be removed. After committing, take the record and call the web service based on it. It may be done in a batch, but it could also be triggered from code of invoice posting or utilize system code running on transaction commit. This will also allow you to resend the message later if something fails.
     
    By the way, consider using business events. Microsoft already handled these things for you there, therefore you don't have to design a similar thing on your own. You may have a Power Automate flow (or so) getting notified that an invoice was posted, it'll fetch details (e.g. through a virtual entity and the Dataverse connector), compose JSON and send the message.
     
    Moved from Integration, Dataverse, and general topics forum to Finance | Project Operations, Human Resources, AX, GP, SL forum.
  • Suggested answer
    Cyrille Nembot Profile Picture
    149 on at
    To address your issue with JSON integration during a sales invoice posting rollback, you should consider leveraging the event handler
    framework in D365FO to ensure the external API call occurs only after a
    successful commit. Instead of triggering the API call directly in the SalesInvoiceJournalPost_Extension class, you can use the onCommitted event handler (or similar post-commit events) to execute the integration logic after
    the transaction is confirmed. This way, if the user cancels the process
    and a rollback occurs, the API call won’t be sent prematurely.
    Alternatively, you could implement a two-phase approach: first, stage the data in a custom table during posting, then use a asynchronous mechanism
    (like a queue or a batch job) to process staged records only after
    verifying successful posting. This ensures idempotency and avoids
    duplicate or erroneous transmissions. If real-time integration is
    critical, combining the onCommitted event with a retry
    mechanism for failed API calls would provide reliability while
    maintaining immediacy. Testing with manual rollback scenarios is key to
    validating the solution.
  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at
    @Cyrille Nembot Please tell us more about the solution with onCommitted. Correct me if I'm wrong, but I don't think that such an event exist in F&O.
    If (as I suspect) you used AI to generate the answer and you didn't check whether is made any sense or not, please stop doing. Please follow Responsible AI policies of this forum.

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

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans