{"id":39587,"date":"2024-10-30T16:18:29","date_gmt":"2024-10-30T10:48:29","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=39587"},"modified":"2024-10-30T16:26:44","modified_gmt":"2024-10-30T10:56:44","slug":"formload-to-formloaded-enhancing-form-functionality-with-formloaded-in-power-apps","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2024\/10\/formload-to-formloaded-enhancing-form-functionality-with-formloaded-in-power-apps\/","title":{"rendered":"FormLoad to FormLoaded: Enhancing Form Functionality with formLoaded in Power Apps"},"content":{"rendered":"<p><img decoding=\"async\" class=\"alignnone size-full wp-image-39591\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded.png\" alt=\"Enhancing Form Functionality with formLoaded\" width=\"2800\" height=\"1600\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded.png 2800w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-300x171.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-1024x585.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-768x439.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-1536x878.png 1536w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-2048x1170.png 2048w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-660x377.png 660w\" sizes=\"(max-width: 2800px) 100vw, 2800px\" \/><\/p>\n<p style=\"text-align: justify;\">As developers, we understand that effective form handling is important for delivering a good user experience. Traditionally, many of us depend on <strong>\u201cformLoad\u201d,<\/strong> but this approach has significant drawbacks, such as <strong>inconsistent loading data and insufficient feedback<\/strong> during asynchronous data fetching, which can lead to user frustration.<\/p>\n<p style=\"text-align: justify;\">In many projects, the <strong>formLoaded<\/strong> method has proven to be incredibly useful. It makes sure that every part of the form is <strong>completely loaded<\/strong> before users can start using it, which improves both reliability and performance. Research shows that using <strong>addOnLoad<\/strong> can create challenges with completing tasks afterward, especially when trying to set up data right as the form loads.<\/p>\n<p style=\"text-align: justify;\">Let&#8217;s say John is working on an Employee Management System where he manages employee information. He ran into a scenario where he needed to load all employee data through an API when the form opens and initialize a field called <strong>&#8220;Salary.&#8221;<\/strong> Afterward, he needed to perform an additional step to calculate the <strong>&#8220;Monthly Tax.&#8221;<\/strong><\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-39588\" style=\"border: 1px solid #000000; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-1.png\" alt=\"Enhancing Form Functionality with formLoaded \" width=\"1586\" height=\"599\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-1.png 1586w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-1-300x113.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-1-1024x387.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-1-768x290.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-1-1536x580.png 1536w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-1-660x249.png 660w\" sizes=\"(max-width: 1586px) 100vw, 1586px\" \/><\/p>\n<p style=\"text-align: justify;\">While working on this, John ran into two main issues:<\/p>\n<ol style=\"text-align: justify;\">\n<li><strong>Data Retrieval Time:<\/strong> With over 20,000 employee records, <strong>pulling <\/strong>in all the <strong>data<\/strong> took a <strong>long time<\/strong>. The &#8220;Salary&#8221; field didn\u2019t get filled until all the data was loaded, <strong>causing delays<\/strong>.<\/li>\n<li><strong>Timing for Tax Calculation:<\/strong> During the form load, he couldn\u2019t get the salary details quickly enough for the tax calculation step. The <strong>initial setup<\/strong> took about <strong>5.23 seconds<\/strong>, and by the time it finished, the tax calculation process had already started, <strong>leading to empty or missing values<\/strong> for the salary.<\/li>\n<\/ol>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-39590\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-2.png\" alt=\"Enhancing Form Functionality with formLoaded \" width=\"814\" height=\"235\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-2.png 814w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-2-300x87.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-2-768x222.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-2-660x191.png 660w\" sizes=\"(max-width: 814px) 100vw, 814px\" \/><\/p>\n<p style=\"text-align: justify;\">In trying to achieve this scenario, John also used the <strong>addOnLoad <\/strong>method, but he encountered the same issue with the delayed initialization of the field.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-39589 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-3-e1730284784892.png\" alt=\"Enhancing Form Functionality with formLoaded\" width=\"1585\" height=\"251\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-3-e1730284784892.png 1585w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-3-e1730284784892-300x48.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-3-e1730284784892-1024x162.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-3-e1730284784892-768x122.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-3-e1730284784892-1536x243.png 1536w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2024\/10\/Enhancing-Form-Functionality-with-formLoaded-3-e1730284784892-660x105.png 660w\" sizes=\"(max-width: 1585px) 100vw, 1585px\" \/><\/p>\n<p style=\"text-align: justify;\">During his research, John came across the newly introduced <strong>formLoaded<\/strong> event in Microsoft\u2019s documentation. This event <strong>enables<\/strong> extra API calls once the <strong>form has fully loaded,<\/strong> making it ideal for cases where follow-up actions depend on fields that are set up during the initial form load.<\/p>\n<p style=\"text-align: justify;\">By using the <strong>formLoaded<\/strong> event, we can apply methods like <strong>addLoaded <\/strong>and <strong>removeLoaded<\/strong>, which help attach or detach functions based on specific needs. In John&#8217;s case, he used <strong>formLoaded<\/strong> to link the tax calculation function after the &#8220;Salary&#8221; field was fully initialized.<\/p>\n<p style=\"text-align: justify;\">Here\u2019s a reference code snippet that illustrates how I implemented this functionality.<\/p>\n<pre class=\"lang:css gutter:true start:1\"> \r\nasync function getAllEmployeeData(executionContext) {\r\n    try {\r\n\t  \/\/Retrieving the Employee's data using the external API \r\n        \/\/ Process each employee's data\r\n        const formContext = executionContext.getFormContext();\r\n        const managerNameData = formContext.getAttribute(\"new_manager\").getValue()[0];;\r\n        if (managerNameData == null) {\r\n            console.log(\"managerNameData is empty\");\r\n            return;\r\n        }\r\n        const managerName = managerNameData.name;\r\n        console.log(\"Manager name: \" + managerName);\r\n\r\n        const updatedEmployeeData = allEmployeeData.map((employee) =&gt; {\r\n            if (employee.new_name === managerName) {\r\n                formContext.getAttribute(\"new_salary\").setValue(employee.new_employee_salary);\r\n                console.log(\"Manager salary updated successfully\");\r\n            }\r\n            return employee;\r\n        });\r\n\r\n        \/\/ Update the monthly tax field\r\n        formContext.ui.addLoaded(calculateTax);\r\n    } catch (error) {\r\n        console.log(\"Error retrieving employee data: \" + error.message);\r\n    }\r\n    const endTime = performance.now();\r\n    console.log(`getAllEmployeeData execution time: ${endTime - startTime} milliseconds`);\r\n\r\n\r\n}\r\n\/**\r\n * Calculates the tax amount based on the employee salary.\r\n *\/\r\nfunction calculateTax() {\r\n    \/\/declaring the function level variables\r\n    const startTime = performance.now();\r\n    var taxAmount;\r\n    try {\r\n        const formContext = Xrm.Page.data;\r\n        const employeeSalary = formContext.entity.attributes.get(\"new_salary\").getValue();\r\n        if (employeeSalary !== null &amp;&amp; employeeSalary !== \"\" &amp;&amp; employeeSalary !== undefined) {\r\n            const taxRate = 0.25; \/\/ 25% tax rate\r\n            taxAmount = employeeSalary * taxRate;\r\n            formContext.entity.attributes.get(\"new_monthlytax\").setValue(taxAmount);\r\n            console.log(\"Tax calculation successful\");\r\n        } else {\r\n            console.log(\"Employee salary data is not present\");\r\n        }\r\n    } catch (error) {\r\n        console.error(\"Error in tax calculation:\", error.message);\r\n    }\r\n    const endTime = performance.now();\r\n    console.log(`calculateTax execution time: ${endTime - startTime} milliseconds`);\r\n}\r\nend<\/pre>\n<p style=\"text-align: justify;\">This code clearly demonstrates how we can efficiently perform post operations on data initialized during the formLoaded event.<\/p>\n<h3 style=\"text-align: justify;\">Advantages of Using the FormLoaded Event:<\/h3>\n<ol style=\"text-align: justify;\">\n<li><strong>Improved User Experience:<\/strong> Ensures all fields are ready for interaction.<\/li>\n<li><strong>Efficient Data Handling:<\/strong> Handles dependencies effectively, allowing for accurate post calculations.<\/li>\n<li><strong>Reduced Wait Times:<\/strong> Minimizes delays by managing data loading more efficiently.<\/li>\n<\/ol>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p style=\"text-align: justify;\">In conclusion, adopting the <strong>formLoaded<\/strong> event has greatly improved our form handling processes. Initializing all data prior to user interaction has enhanced the reliability and performance of our applications. Now developers can easily consider this approach for optimizing their form handling strategies and delivering a superior user experience.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/services\/microsoft-power-platform\/microsoft-power-apps-model-driven-apps\/\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"alignnone wp-image-35629 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/08\/Model-Driven-App.gif\" alt=\"Model Driven App\" width=\"700\" height=\"200\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As developers, we understand that effective form handling is important for delivering a good user experience. Traditionally, many of us depend on \u201cformLoad\u201d, but this approach has significant drawbacks, such as inconsistent loading data and insufficient feedback during asynchronous data fetching, which can lead to user frustration. In many projects, the formLoaded method has proven\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2024\/10\/formload-to-formloaded-enhancing-form-functionality-with-formloaded-in-power-apps\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":11,"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":[44],"tags":[2959],"class_list":["post-39587","post","type-post","status-publish","format-standard","hentry","category-power-apps","tag-formloaded-function"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/39587","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\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=39587"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/39587\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=39587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=39587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=39587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}