{"id":1104,"date":"2014-12-19T16:39:46","date_gmt":"2014-12-19T11:09:46","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=1104"},"modified":"2014-12-19T16:39:46","modified_gmt":"2014-12-19T11:09:46","slug":"how-to-show-filtered-lookup-dialog-in-dynamics-crm-through-script","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2014\/12\/how-to-show-filtered-lookup-dialog-in-dynamics-crm-through-script\/","title":{"rendered":"How to show Filtered Lookup Dialog in Dynamics CRM through Script"},"content":{"rendered":"<p>We had a requirement where in we were supposed to show a Filtered Custom Lookup on form load. By Filtered Custom Lookup, it meant that we were supposed to create a Lookup dialog similar to CRM Filtered lookup (Filtered Lookup value changes on the basis of other Lookup on which it is dependent) dialog. The lookup has to be filtered on the basis of a value in the lookup field which was there on the form.<\/p>\n<p>Now the question was, how were we supposed to achieve this? We had already done a Lookup Dialog to show all the records for an entity, but this was something new and enthralling.<\/p>\n<p>We dived into it and after a few hours of R&amp;D, we came up with a solution. This blog is entirely dedicated in explaining and implementing that solution.<\/p>\n<p>Let us begin\u00a0with our\u00a0entity structure which comprised of\u00a0an entity called Contractor and it was in N:1 relation with Project and Payment Schedule where as Payment Schedule was in N:1 relation with Project.<\/p>\n<p>The requirement was to show the Filtered Custom Lookup dialog containing Payment Schedule records, related to the Project (Lookup field) selected on the Contractor, on form load of Contractor.<\/p>\n<p><strong>Screenshots:<\/strong><\/p>\n<p><strong>Note: <\/strong>Lookup Dialog seen in the below screenshots is a Custom Lookup Dialog opened on form load using JavaScript.<\/p>\n<p>Below screenshot shows the Project selected as Test Project having Payment Schedule records for it as a result of which we can see that record in the Lookup Dialog.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2014\/12\/Test-Payment-Schedule.jpg\"><img decoding=\"async\" class=\"alignnone size-full wp-image-1106\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2014\/12\/Test-Payment-Schedule.jpg\" alt=\"Test Payment Schedule\" width=\"626\" height=\"428\" \/><\/a><\/p>\n<p>Below screenshot shows the Project selected as Test Project 2 and Test Project 2 has no Payment Schedule records for it and as a result we cannot see any records in the Lookup Dialog.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2014\/12\/Payment-Schedule-Lookup.jpg\"><img decoding=\"async\" class=\"alignnone size-full wp-image-1107\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2014\/12\/Payment-Schedule-Lookup.jpg\" alt=\"Payment Schedule Lookup View\" width=\"626\" height=\"419\" \/><\/a><\/p>\n<p>Let&#8217;s start with the technical part of it.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<p>\/\/function to open filtered entity lookup to select record<\/p>\n<p>function openFilteredLookup(defaultType, defaultViewId, currentObjectType, currentId, rDependentAttr, rId, rType, relationshipId) {<\/p>\n<p>var functionName = &#8220;openLookup&#8221;;<\/p>\n<p>try {<\/p>\n<p>&nbsp;<\/p>\n<p>if (isValid(currentObjectType) &amp;&amp; isValid(currentId)) {<\/p>\n<p>\/\/prepare lookup url<\/p>\n<p>var url = &#8220;\/_controls\/lookup\/lookupinfo.aspx?AllowFilterOff=0&amp;DefaultType=&#8221; + defaultType + &#8220;&amp;DefaultViewId=%7b&#8221; + defaultViewId + &#8220;%7d&amp;DisableQuickFind=0&amp;DisableViewPicker=1&amp;IsInlineMultiLookup=0&amp;LookupStyle=single&amp;ShowNewButton=1&amp;ShowPropButton=1&amp;browse=false&amp;currentObjectType=&#8221; + currentObjectType + &#8220;&amp;currentid=%7b&#8221; + currentId + &#8220;%7d&amp;dType=1&amp;mrsh=false&amp;objecttypes=&#8221; + defaultType + &#8220;&amp;rDependAttr=&#8221; + rDependentAttr + &#8220;&amp;rId=%7b&#8221; + rId + &#8220;%7d&amp;rType=&#8221; + rType + &#8220;&amp;relationshipid=&#8221; + relationshipId + &#8220;&#8221;;<\/p>\n<p>} else {<\/p>\n<p>\/\/prepare lookup url<\/p>\n<p>var url = &#8220;\/_controls\/lookup\/lookupinfo.aspx?AllowFilterOff=0&amp;DefaultType=&#8221; + defaultType + &#8220;&amp;DefaultViewId=%7b&#8221; + defaultViewId + &#8220;%7d&amp;DisableQuickFind=0&amp;DisableViewPicker=1&amp;IsInlineMultiLookup=0&amp;LookupStyle=single&amp;ShowNewButton=1&amp;ShowPropButton=1&amp;browse=false&amp;dType=1&amp;mrsh=false&amp;objecttypes=&#8221; + defaultType + &#8220;&amp;rDependAttr=&#8221; + rDependentAttr + &#8220;&amp;rId=%7b&#8221; + rId + &#8220;%7d&amp;rType=&#8221; + rType + &#8220;&amp;relationshipid=&#8221; + relationshipId + &#8220;&#8221;;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/Set the Dialog Width and Height<\/p>\n<p>var DialogOptions = new Xrm.DialogOptions();<\/p>\n<p>\/\/Set the Width<\/p>\n<p>DialogOptions.width = 500;<\/p>\n<p>\/\/Set the Height<\/p>\n<p>DialogOptions.height = 550;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/open dialog<\/p>\n<p>Xrm.Internal.openDialog(Mscrm.CrmUri.create(url).toString(), DialogOptions, null, null, CallbackFunction);<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/Set the variable to false<\/p>\n<p>isOnLoad = false;<\/p>\n<p>&nbsp;<\/p>\n<p>} catch (e) {<\/p>\n<p>throwError(e, functionName);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><strong>What are the parameters passed for the above used function?<\/strong><\/p>\n<ul>\n<li>defaultType = Object Type Code of the entity which needs to be shown in the Lookup.<\/li>\n<li>defaultViewId = GUID of the defaultView which will be set for the Look In section of the Lookup Dialog, in order to get the defaultView GUID, you can use the following code :<\/li>\n<\/ul>\n<p>\/\/Get the Default View id<\/p>\n<p>Xrm.Page.getControl(controlName).getDefaultView().replace(&#8220;{&#8220;, &#8220;&#8221;).replace(&#8220;}&#8221;, &#8220;&#8221;).trim();<\/p>\n<ul>\n<ul>\n<li>controlName: It is the name of the Lookup Control.<\/li>\n<\/ul>\n<\/ul>\n<ul>\n<li>currentObjectType = Object Type Code of the current entity.<\/li>\n<li>currentId = GUID of the current record.<\/li>\n<li>rDependentAttr = new_contractor.new_projectid<br \/>\nAs the name suggest it`ll give the attribute on the basis of which lookup is filtered.<br \/>\nThe above rDependentAttr value is used in our current demo.<\/p>\n<ul>\n<li>new_contractor: It is the name of the current entity.<\/li>\n<li>new_projectid: It is the control\/attribute name on the basis of which the Lookup is filtered.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li>rId = It is the GUID of the record selected in the new_projectid field.<\/li>\n<li>rType = It is the Object Type Code of the new_projectid.<\/li>\n<li>relationshipId = new_new_project_new_paymentschedule.<\/li>\n<\/ul>\n<p>It is the relationship name of how the Project and Payment Schedule entities are related.<\/p>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li>We can retrieve Entity Metadata to get the Object Type Code of any entity.<\/li>\n<li>The above method is an unsupported method to get the Lookup.<\/li>\n<li><a href=\"https:\/\/www.inogic.com\/blog\/2014\/09\/how-to-get-lookup-dialog-in-crm-2013\">How to use Xrm.Internal.openDialog method?<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>We had a requirement where in we were supposed to show a Filtered Custom Lookup on form load. By Filtered Custom Lookup, it meant that we were supposed to create a Lookup dialog similar to CRM Filtered lookup (Filtered Lookup value changes on the basis of other Lookup on which it is dependent) dialog. The\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2014\/12\/how-to-show-filtered-lookup-dialog-in-dynamics-crm-through-script\/\">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,21,22,24,33],"tags":[592,740,816,817,1012],"class_list":["post-1104","post","type-post","status-publish","format-standard","hentry","category-dynamics-crm","category-dynamics-crm-2013","category-dynamics-crm-2015","category-dynamics-crm-2016","category-javascript","tag-dynamics-crm","tag-entity","tag-filtered","tag-filtered-lookup-dynamics-crm","tag-lookup"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/1104","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=1104"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/1104\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=1104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=1104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=1104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}