{"id":27627,"date":"2021-04-22T12:42:23","date_gmt":"2021-04-22T12:42:23","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=27627"},"modified":"2021-09-02T06:39:01","modified_gmt":"2021-09-02T06:39:01","slug":"details-about-new-async-onsave-events-postsave-async-onsave-event-in-dynamics-365-crm-dataverse-powerapps","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2021\/04\/details-about-new-async-onsave-events-postsave-async-onsave-event-in-dynamics-365-crm-dataverse-powerapps\/","title":{"rendered":"Know Details about new OnPostSave Event in Dynamics 365 CRM \/ Dataverse (PowerApps)"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>I am sure you may have used OnSave event several times in your projects. We generally use this event and write code in it to do some validation on the record to restrict the user from saving the record (using prevent Defaults method).<\/p>\n<p>The nature of OnSave event is synchronous. It is not the right place to call asynchronous code because it does not wait for it and we may see unsaved changes. If we make a synchronous network request in OnSave event, it can cause a slow experience and an unresponsive page.<\/p>\n<p>Microsoft has introduced a new Post Save event. This event is called after save event is complete and it supports after Save actions when the save event is successful or failed.<\/p>\n<p>We will see how this event is useful over save event. On load of Account form, I have added two events as shown below.<\/p>\n<p>FormLibrary.prototype.onLoad = function (execContext, attributes) {<br \/>\n\/\/\/ &lt;summary&gt;<br \/>\n\/\/\/ This function will load crm web api on load of form<br \/>\n\/\/\/ &lt;\/summary&gt;<br \/>\nvar functionName = &#8220;onLoad&#8221;;<br \/>\ntry {<\/p>\n<p>var formContext = execContext.getFormContext();<\/p>\n<p>formContext.data.entity.addOnSave(Account_FormLibrary.SaveFunction);<br \/>\nformContext.data.entity.addOnPostSave(Account_FormLibrary.PostSaveFunction);<\/p>\n<p>}<br \/>\ncatch (ex) {<br \/>\nconsole.log(ex.message);<br \/>\n}};<\/p>\n<h2><strong>Post Save Event function\u00a0<\/strong><\/h2>\n<p>FormLibrary.prototype.PostSaveFunction = function (executionContext) {<br \/>\nvar formContext = executionContext.getFormContext();<br \/>\nvar RecordId = formContext.data.entity.getId();<br \/>\nvar fetchXml = &#8220;&lt;fetch version=&#8217;1.0&#8242; output-format=&#8217;xml &#8211; platform&#8217; mapping=&#8217;logical&#8217; distinct=&#8217;false&#8217;&gt;&#8221; +<br \/>\n&#8220;&lt;entity name=&#8217;account&#8217;&gt;&#8221; +<br \/>\n&#8220;&lt;attribute name=&#8217;name&#8217; \/&gt;&#8221; +<br \/>\n&#8220;&lt;filter type=&#8217;and&#8217;&gt;&#8221; +<br \/>\n&#8220;&lt;condition attribute=&#8217;accountid&#8217; operator=&#8217;eq&#8217; value='&#8221; + RecordId + &#8220;&#8216; \/&gt;&#8221; +<br \/>\n&#8220;&lt;\/filter&gt;&#8221; +<br \/>\n&#8220;&lt;\/entity&gt;&#8221; +<br \/>\n&#8220;&lt;\/fetch&gt;&#8221;;<br \/>\nqueryString = {<br \/>\nFetchXml: fetchXml,<br \/>\nFormattedValues: true<br \/>\n};<br \/>\nvar accountCollction = Account_FormLibrary._crmAPI.CustomFetch(&#8220;accounts&#8221;, queryString, null);<br \/>\n};<\/p>\n<h2><strong>Save Event function<\/strong><\/h2>\n<p>FormLibrary.prototype.SaveFunction = function (executionContext) {<br \/>\nvar formContext = executionContext.getFormContext();<br \/>\nvar RecordId = formContext.data.entity.getId();<br \/>\n};<\/p>\n<p>Now when you click on the OOB save button, save event will trigger, and below will be the result.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/1-async-OnSave-events-PostSave.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-27622 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/1-async-OnSave-events-PostSave.png\" alt=\"async OnSave events PostSave\" width=\"847\" height=\"112\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/1-async-OnSave-events-PostSave.png 847w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/1-async-OnSave-events-PostSave-300x40.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/1-async-OnSave-events-PostSave-768x102.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/1-async-OnSave-events-PostSave-660x87.png 660w\" sizes=\"(max-width: 847px) 100vw, 847px\" \/><\/a><\/p>\n<p>Here the record id is coming blank because save event is not completely done. So I am not able to find related contacts. But when I check the Post save event result, I am able to get record id because it is calling once save event is completely done.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/2-async-OnSave-events-PostSave.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-27623 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/2-async-OnSave-events-PostSave.png\" alt=\"async OnSave events PostSave\" width=\"868\" height=\"253\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/2-async-OnSave-events-PostSave.png 868w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/2-async-OnSave-events-PostSave-300x87.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/2-async-OnSave-events-PostSave-768x224.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/2-async-OnSave-events-PostSave-660x192.png 660w\" sizes=\"(max-width: 868px) 100vw, 868px\" \/><\/a><\/p>\n<p>Here are some client API Microsoft added which can be useful while save or post save event \u2013<\/p>\n<p><strong>executionContext.getEventArgs().getEntityReference();<\/strong><\/p>\n<p>If save succeeds, it returns entity ID, and entity name.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/3-async-OnSave-events-PostSave.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-27624 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/3-async-OnSave-events-PostSave.png\" alt=\"async OnSave events PostSave\" width=\"942\" height=\"266\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/3-async-OnSave-events-PostSave.png 942w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/3-async-OnSave-events-PostSave-300x85.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/3-async-OnSave-events-PostSave-768x217.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/3-async-OnSave-events-PostSave-660x186.png 660w\" sizes=\"(max-width: 942px) 100vw, 942px\" \/><\/a><\/p>\n<p><strong>executionContext.getEventArgs().getIsSaveSuccess();<\/strong><\/p>\n<p>You can use this client API to check whether save operation is successful or failed.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/4-async-OnSave-events-PostSave.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-27625 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/4-async-OnSave-events-PostSave.png\" alt=\"async OnSave events PostSave\" width=\"473\" height=\"221\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/4-async-OnSave-events-PostSave.png 473w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/4-async-OnSave-events-PostSave-300x140.png 300w\" sizes=\"(max-width: 473px) 100vw, 473px\" \/><\/a><\/p>\n<p><strong>executionContext.getEventArgs().getSaveErrorInfo();<\/strong><\/p>\n<p>This will provide the error info on save event. If we have plugin or action on create\/update of record and if it is failing then this method shows the error information thrown from the plugin or action.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/5-async-OnSave-events-PostSave.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-27626 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/5-async-OnSave-events-PostSave.png\" alt=\"async OnSave events PostSave\" width=\"1437\" height=\"245\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/5-async-OnSave-events-PostSave.png 1437w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/5-async-OnSave-events-PostSave-300x51.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/5-async-OnSave-events-PostSave-768x131.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/5-async-OnSave-events-PostSave-1024x175.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/5-async-OnSave-events-PostSave-660x113.png 660w\" sizes=\"(max-width: 1437px) 100vw, 1437px\" \/><\/a><\/p>\n<h2>Conclusion<\/h2>\n<p>You can use this information to use the new OnPostSave Event in Dynamics 365 CRM \/ Dataverse (PowerApps).<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/kanban-board-dynamics-365-crm\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-27629 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/kanban-board-1.jpg\" alt=\"\" width=\"800\" height=\"200\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/kanban-board-1.jpg 800w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/kanban-board-1-300x75.jpg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/kanban-board-1-768x192.jpg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2021\/04\/kanban-board-1-660x165.jpg 660w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction I am sure you may have used OnSave event several times in your projects. We generally use this event and write code in it to do some validation on the record to restrict the user from saving the record (using prevent Defaults method). The nature of OnSave event is synchronous. It is not the\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2021\/04\/details-about-new-async-onsave-events-postsave-async-onsave-event-in-dynamics-365-crm-dataverse-powerapps\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2354,44],"tags":[2171,1882],"class_list":["post-27627","post","type-post","status-publish","format-standard","hentry","category-dataverse","category-power-apps","tag-dataverse","tag-powerapps"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/27627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=27627"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/27627\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=27627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=27627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=27627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}