All Content
Timespan
explore our new search
Preventing Default on Async onSave with External Call
Image Source: Shutterstock.com
Developer Tools
Sep 29, 2023 7:11 AM

Preventing Default on Async onSave with External Call

by HubSite 365 about Benedikt Bergmann [MVP]

Microsoft Business Applications MVP | Power Platform Consultant at CRM-Konsulterna

Citizen DeveloperDeveloper ToolsM365 Hot News

Learn how to create an async onSave preventDefault with an external call for seamless Dynamics 365 cloud migration.

Benedikt Bergmann’s recent blog post revolves around Async onSave preventDefault with an external call in migrating Dynamics 365 from onPrem to the Cloud. As a part of the refactoring process of existing JavaScript implementation, he encountered and tackled an onSave function that previously prevented the save process due to a call to the WebAPI.

In the Power Platform, all WebAPI calls are executed asynchronously. Therefore, if one needs to acquire additional data to make a decision on whether to cancel the current save event, an async onSave is required. However, the first approach - using “preventDefault” didn’t yield expected results because the save was executed either way. As per Microsoft documentation, in such cases, use preventDefaultOnError instead.

preventDefaultOnError function suspends the saving operation if there is a scripting error in the event handler, if a rejected promise returns for an async event handler, or if the operation times out. Unfortunately, simply “awaiting” on the Xrm.WebApi call doesn't cancel the save event.

More details on developer tools can be found here

Implementation Details

The blog post shares an example of a simple onSave function that inhibits the save process when there is no account in the name of “Benedikt”. Interestingly, the functional sample is shared in both TypeScript and JavaScript. Initially, the function preventDefaultOnError is executed, which is expecting a promise to be rejected in order to prevent the save.

Subsequently, a promise is returned where the request to the WebAPI is performed. If the outcome yields more than one result, the promise is resolved. In contrast, if there is no result or an error is encountered, the promise is rejected, thus prohibiting the saving of the form.

Conclusion

Bergmann's blog post provides valuable insights into how to create an async onSave preventDefault with an external call. Once preventDefaultOnError function's expectations are understood, it is rather straightforward to satisfy them.

Bergmann welcomes any questions or feedback on his methods and findings.

Read the full article Async onSave preventDefault with external call

Developer Tools - Preventing Default on Async onSave with External Call

Learn about Async onSave preventDefault with external call

While carrying out our ongoing projects, we often come across technical challenges that push us to seek alternative solutions. One such challenge is migrating an on-premise Dynamics 365 environment to the Cloud. Refactoring existing JavaScript implementation is part of this process and it involves the use of an onSave function. This function prevents the process from saving if it needs to call the WebAPI.

Understanding the Concept

In the Power Platform milieu, all calls made to the WebAPI are done asynchronously. This implies that if you need to fetch additional data to decide whether you'd like to cancel the current save event, you will need an async onSave. When initial experimentation with the preventDefault method did not yield the expected outcome, preventDefaultOnError emerged as the viable alternative following guidelines prescribed in the Microsoft documentation.

The preventDefaultOnError function operates on a promise and awaits an outcome of either success or rejection. Simply doing an 'await' on the call to the WebApi will not cancel the save event.

Implementation

Moving onto the implementation, a simple onSave function can potentially prevent the saving when there is no account with the name "Benedikt" (the function is shown in both JavaScript and TypeScript formats). Initially, we execute the preventDefaultOnError function. This function anticipates a promise to be rejected in order to inhibit the saving.

The cue to save is derived from a promise, wherein we do our appeal to the WebApi (this could involve any other service of your preference). In situations where there is more than a single result, we resolve the promise, and in all other instances (be it, no result or an error), we reject it. If the promise gets rejected, the form's saving mechanism is canceled altogether.

Conclusion

I hope this brief elucidation helps you grasp the concept of creating an async onSave preventDefault with an external call. Upon comprehending what the function preventDefaultOnErrror anticipates, catering to its demands becomes a rather straightforward task.

If you have any queries, requests, or feedback on this topic or many others involving the Microsoft ecosystem and Development Tools, please feel free to approach me. My objective is to make the world of Dynamics 365, Cloud migration, and similar topics more accessible to all audience levels.

Original post: Async onSave preventDefault with external call

More links on about Async onSave preventDefault with external call

Async onSave preventDefault with external call
Mar 20, 2023 — In this blog post, you will learn how to implement an async onSave preventDefault with an external call to the WebAPI within Dataverse.
Async onSave preventDefault with external call
Mar 20, 2023 — I tried the usual approach of calling “preventDefault“, which has not given the expected result as the save was executed either way.
Async onSave preventDefault with external call
Mar 28, 2023 — Async onSave preventDefault with external call is a technique used to prevent a form from submitting when a user clicks a submit button. This ...

Keywords

Async onSave, preventDefault, external call, JavaScript onSave, async function, preventDefault method, async preventDefault, external API call, onSave event, async external call