{"id":2077,"date":"2015-11-30T18:26:17","date_gmt":"2015-11-30T12:56:17","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=2077"},"modified":"2021-12-06T15:04:34","modified_gmt":"2021-12-06T09:34:34","slug":"identify-the-trigger-for-an-on-load-event-for-sub-grid-in-dynamics-crm","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2015\/11\/identify-the-trigger-for-an-on-load-event-for-sub-grid-in-dynamics-crm\/","title":{"rendered":"Identify the trigger for an On-load event for Sub-grid in Dynamics CRM"},"content":{"rendered":"<p><strong>Introduction:<\/strong><\/p>\n<p>With CRM 2015 SP1, the Client API was extended to allow attaching events to sub-grid to manage the sub-grid data\/operations.<\/p>\n<p>The OnLoad event however executes on all of the following operations<\/p>\n<ol>\n<li>Load of parent form.<\/li>\n<li>Save of parent form.<\/li>\n<li>Add a new record in sub-grid<\/li>\n<li>Remove a record from sub-grid<\/li>\n<li>Navigating to prev\/next page in sub-grid.<\/li>\n<\/ol>\n<p>When we register a method on the OnLoad event, it does not indicate in any way which of the above actually fired the onload event.<\/p>\n<p><strong>Workaround to Identifying the operation <\/strong><\/p>\n<p>We cannot register an event when the record is added\/removed from the sub-grid. The only event available is onload, so one way to identify if it was one of the add\/remove operations that caused the grid to call the onload event, we can keep a check on the count of records in the grid.<\/p>\n<p>We can register the onload event using the following code<\/p>\n<p>\/\/This function is executed onload event of form<\/p>\n<pre class=\"lang:default decode:true \">function onLoad() {\r\n\r\n\u00a0\u00a0\u00a0 var funtionName = \"onLoad\";\r\n\r\n\u00a0\u00a0\u00a0 try {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/setting timeout beacuse subgid take some time to load after the form is loaded\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 setTimeout(function () {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/validating to check if the sub grid is present on the form\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (Xrm.Page != null &amp;&amp; Xrm.Page != undefined &amp;&amp; Xrm.Page.getControl(\"contact_subgrid\") != null &amp;&amp; Xrm.Page.getControl(\"contact_subgrid\") != undefined) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/stores the row count of subgrid on load event of CRM Form\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0_rowCount = Xrm.Page.getControl(\"contact_subgrid\").getGrid().getTotalRecordCount();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/registering refreshform function onload event of subgrid\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Xrm.Page.getControl(\"contact_subgrid\").addOnLoad(onGridLoad);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }, 5000);\r\n\r\n\u00a0\u00a0\u00a0 } catch (e) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Xrm.Utility.alertDialog(functionName + \"Error: \" + (e.message || e.description));\r\n\r\n\u00a0\u00a0\u00a0 }\r\n\r\n}<\/pre>\n<p>The rowCount is defined as global variable and is updated here to store the count of records in the grid when the form is loaded initially.<\/p>\n<p>\/\/This function calls the intended function on load of subgrid<\/p>\n<pre class=\"lang:default decode:true \">function onGridLoad() {\r\n\r\n\u00a0\u00a0\u00a0 var functionName = \" onGridLoad \";\r\n\r\n\u00a0\u00a0\u00a0 var currentRowCount = null;\r\n\r\n\u00a0\u00a0\u00a0 try {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/setting timeout beacuse subgrid take some time to load after the form is loaded\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 setTimeout(function () {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/validating to check if the sub grid is present on the form\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (Xrm.Page != null &amp;&amp; Xrm.Page != undefined &amp;&amp; Xrm.Page.getControl(\"contact_subgrid\") != null &amp;&amp; Xrm.Page.getControl(\"contact_subgrid\") != undefined) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/stores the row count of subgrid on load event of CRM Form\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 currentRowCount = Xrm.Page.getControl(\"contact_subgrid\").getGrid().getTotalRecordCount();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0if (currentRowCount &gt; _rowCount) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/call the intended function which we want to call only when records are added to the grid\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 dosomething();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/set current row count to the global row count\r\n\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0_rowCount = currentRowCount;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 else if (currentRowCount &lt; _rowCount) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/call the intended function which we want to call only when records are removed from the grid\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 dosomethingelse();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/set current row count to the global row count\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 _rowCount = currentRowCount;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }, 2000);\r\n\r\n\u00a0\u00a0\u00a0 } catch (e) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Xrm.Utility.alertDialog(functionName + \"Error: \" + (e.message || e.description));\r\n\r\n\u00a0\u00a0\u00a0 }\r\n\r\n}<\/pre>\n<p><strong>Conclusion:<\/strong><\/p>\n<p>In this way we can identify if a record is added or removed from sub grid in Dynamics CRM 2015 SP1.<\/p>\n<h2 style=\"text-align: left;\"><div class=\"su-heading su-heading-style-default su-heading-align-center\" id=\"\" style=\"font-size:15px;margin-bottom:5px\"><div class=\"su-heading-inner\">One Pic = 1000 words! Analyze data 90% faster with visualization apps!<\/div><\/div><\/h2>\n<p style=\"text-align: left;\"><em>Get optimum visualization of Dynamics 365 CRM data with &#8211;<\/em><br \/>\n<em><strong><a href=\"https:\/\/bit.ly\/3lYvozZ\" target=\"_blank\" rel=\"noopener noreferrer\">Kanban Board<\/a> <\/strong>\u2013 Visualize Dynamics 365 CRM data in Kanban view by categorizing entity records in lanes and rows as per their status, priority, etc.<\/em><br \/>\n<em><strong><a href=\"https:\/\/bit.ly\/3lCSBaA\" target=\"_blank\" rel=\"noopener noreferrer\">Map My Relationships<\/a><\/strong> \u2013 Map My Relationships \u2013 Visualize connections and relationships between Dynamics 365 CRM entities or related records in a Mind Map view.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: With CRM 2015 SP1, the Client API was extended to allow attaching events to sub-grid to manage the sub-grid data\/operations. The OnLoad event however executes on all of the following operations Load of parent form. Save of parent form. Add a new record in sub-grid Remove a record from sub-grid Navigating to prev\/next page\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2015\/11\/identify-the-trigger-for-an-on-load-event-for-sub-grid-in-dynamics-crm\/\">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":[19,22,23,24,25,33],"tags":[1678],"class_list":["post-2077","post","type-post","status-publish","format-standard","hentry","category-dynamics-crm","category-dynamics-crm-2015","category-dynamics-crm-2015-update-1","category-dynamics-crm-2016","category-dynamics-crm-2016-update-1","category-javascript","tag-sub-grid-in-dynamics-crm"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/2077","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=2077"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/2077\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=2077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=2077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=2077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}