{"id":889,"date":"2014-09-08T12:05:34","date_gmt":"2014-09-08T06:35:34","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=889"},"modified":"2021-08-25T11:09:27","modified_gmt":"2021-08-25T11:09:27","slug":"add-custom-view-in-lookup-and-disable-all-the-other-views","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2014\/09\/add-custom-view-in-lookup-and-disable-all-the-other-views\/","title":{"rendered":"Add Custom View in Lookup and Disable all the other Views"},"content":{"rendered":"<p style=\"text-align: justify;\">We had a requirement in which we were supposed to use a <strong>addCustomView<\/strong> method in order to filter the Lookup. We came across a situation in which we were able to set the custom view(The view with filtered records) as a default view to restrict the users from choosing the other records, but if a user wants, he\/she could choose the different view from the Lookup window dialog and select the other records too, which the users were not supposed to choose.<\/p>\n<p style=\"text-align: justify;\">Let us first introduce you to the situation, on the Quote form we have a Lookup field called <strong>Project Template<\/strong>. And, on the Quote Product form we have another Lookup field called <strong>Project Template Items<\/strong> which should be filtered as per the value chosen for the <strong>Project Template<\/strong>.<\/p>\n<p style=\"text-align: justify;\">We`ll start from the point where we have the <strong>ID <\/strong>of the chosen <strong>Project Template<\/strong>(to get the ID, you need to write a script on the Load of the Quote Product form).<\/p>\n<p style=\"text-align: justify;\">Initially, what we started off was adding our custom view, but it still allowed the user to switch between different views, as a result users were able to choose records from other views.<\/p>\n<p style=\"text-align: justify;\">We`ll explain you both the situations, the <strong>Initial Situation<\/strong>(which was adding the custom view) and the <strong>Ideal Situation<\/strong>(which was adding the custom view and restricting users from selecting other view) .<\/p>\n<p style=\"text-align: justify;\">First and foremost, for the Lookup that`ll have filtered values, you need to change the below properties. These are the necessary steps.<\/p>\n<p style=\"text-align: justify;\"><strong>Step 1: <\/strong><\/p>\n<p style=\"text-align: justify;\">From the form editor, go to the properties of the Lookup field.<\/p>\n<p style=\"text-align: justify;\"><strong>Step 2:<\/strong><\/p>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2014\/09\/1.jpg\"><img decoding=\"async\" class=\"alignnone size-full wp-image-890\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2014\/09\/1.jpg\" alt=\"1\" width=\"447\" height=\"170\" \/><\/a><\/p>\n<p>In the<strong> Additional Properties <\/strong>section,<\/p>\n<p>Default View &#8211; Select a View.<\/p>\n<p>View Selector &#8211; Choose &#8220;<strong>Show Selected Views<\/strong>&#8220;.<\/p>\n<p>System Views\u00a0 &#8211; Only Select the view which was selected in the Default view.<\/p>\n<p><strong>Initial Situation<\/strong><strong>:<\/strong><\/p>\n<p>In this situation, we were doing everything right but, we missed something that stopped us from getting the ideal output.<\/p>\n<p><strong>Code<\/strong><\/p>\n<p>function setCustomView(projectTemplateId) {<\/p>\n<p>var functionName = &#8220;setCustomView&#8221;;<\/p>\n<p>try {<\/p>\n<p>\/\/Check whether the field exist or not<\/p>\n<p style=\"text-align: justify;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (isValid(Xrm.Page.getControl(&#8220;new_projecttemplateitemid&#8221;))) {<\/p>\n<div style=\"border: 2px solid; padding: 10px 40px; background: #ffffff; width: 400px; border-radius: 0px;\">\n<p>\/\/ add the randomly generated GUID for the view id<br \/>\nvar viewId = &#8216;{00000000-0000-0000-0000-000000000001}&#8217;;<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>\/\/add the entity name<\/p>\n<p>var entityName = &#8220;new_projecttemplateitem&#8221;;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ add the view display name<\/p>\n<p>var viewDisplayName = &#8220;Project Template Item Lookup View&#8221;;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ fetch xml for filtered Project Template Item records<\/p>\n<p>var fetchXml = &#8220;&lt;fetch version=&#8217;1.0&#8242; output-format=&#8217;xml-platform&#8217; mapping=&#8217;logical&#8217; distinct=&#8217;false&#8217;&gt; &#8221; +<\/p>\n<p>&#8220;&lt;entity name=&#8217;new_projecttemplateitem&#8217;&gt;&#8221; +<\/p>\n<p>&#8220;&lt;attribute name=&#8217;new_name&#8217; \/&gt;&#8221; +<\/p>\n<p>&#8220;&lt;filter type=&#8217;and&#8217;&gt;&#8221; +<\/p>\n<p>&#8220;&lt;condition attribute=&#8217;new_projecttemplate&#8217; operator=&#8217;eq&#8217; value='&#8221; + projectTemplateId + &#8220;&#8216; \/&gt;&#8221; +<\/p>\n<p>&#8220;&lt;\/filter&gt;&#8221; +<\/p>\n<p>&#8220;&lt;\/entity&gt;&#8221; +<\/p>\n<p>&#8220;&lt;\/fetch&gt;&#8221;;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/Grid Layout for filtered records<\/p>\n<p>var layoutXml = &#8220;&lt;grid name=&#8217;resultset&#8217; object=&#8217;1&#8242; jump=&#8217;productid&#8217; select=&#8217;1&#8242; icon=&#8217;1&#8242; preview=&#8217;1&#8242;&gt;&#8221; +<\/p>\n<p>&#8220;&lt;row name=&#8217;result&#8217; id=&#8217;new_projecttemplateitemid&#8217;&gt;&#8221; +<\/p>\n<p>&#8220;&lt;cell name=&#8217;new_name&#8217; width=&#8217;150&#8242; \/&gt;&#8221; +<\/p>\n<p>&#8220;&lt;cell name=&#8217;createdon&#8217; width=&#8217;150&#8242; \/&gt;&#8221; +<\/p>\n<p>&#8220;&lt;\/row&gt;&#8221; +<\/p>\n<p>&#8220;&lt;\/grid&gt;&#8221;;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ add the custom view for the lookup field<\/p>\n<p>Xrm.Page.getControl(&#8220;new_projecttemplateitemid&#8221;).addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);<\/p>\n<p>}<\/p>\n<p>} catch (e) {<\/p>\n<p>throwError(e, functionName);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>In the above code everything was right but,<\/p>\n<p style=\"text-align: justify;\">on selecting the Custom view, we were getting the below results:<\/p>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2014\/09\/2.jpg\"><img decoding=\"async\" class=\"alignnone size-full wp-image-892\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2014\/09\/2.jpg\" alt=\"2\" width=\"402\" height=\"304\" \/><\/a><\/p>\n<p>And, on selecting the System View, we were getting the below results.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2014\/09\/3.jpg\"><img decoding=\"async\" class=\"alignnone size-full wp-image-893\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2014\/09\/3.jpg\" alt=\"3\" width=\"396\" height=\"365\" \/><\/a><\/p>\n<p>Both the views were giving us different results, which we never wanted.<\/p>\n<p>&nbsp;<\/p>\n<p>So, in the search of an ideal situation we found the below solution.<\/p>\n<p><strong>Ideal Situation<\/strong><strong>:<\/strong><\/p>\n<p>Replace the code in the box with the below code.<\/p>\n<p><strong>Code<\/strong><\/p>\n<p>\/\/ add the default View GUID for the view id<\/p>\n<p>var viewId = Xrm.Page.getControl(&#8220;new_projecttemplateitemid&#8221;).getDefaultView();<\/p>\n<p>This single piece of line works like a charm.<\/p>\n<p>Now, we got the below results on selecting both the views.<\/p>\n<p><strong>Custom View<\/strong>:<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2014\/09\/4.jpg\"><img decoding=\"async\" class=\"alignnone size-full wp-image-894\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2014\/09\/4.jpg\" alt=\"4\" width=\"308\" height=\"230\" \/><\/a><\/p>\n<p><strong>System View:<\/strong><\/p>\n<p><a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2014\/09\/5.jpg\"><img decoding=\"async\" class=\"alignnone size-full wp-image-895\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2014\/09\/5.jpg\" alt=\"5\" width=\"318\" height=\"249\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>And, this is how we got the desirable result with a small tweak in the code.<\/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\">Free 70% of storage space in CRM with Attachment Management Apps!<\/div><\/div><\/h2>\n<p><em><strong><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/attach-2-dynamics-365-crm-upload-multiple-files-sharepoint-cloud-storage\" target=\"_blank\" rel=\"noopener noreferrer\">Attach2Dynamics<\/a> &#8211; Store and manage documents\/attachments in cloud storage of your choice &#8211; SharePoint, Dropbox or Azure Blob Storage from within Dynamics 365 CRM.<\/em><br \/>\n<em><strong><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/dynamics-365-crm-sharepoint-security-metadata-sync\" target=\"_blank\" rel=\"noopener noreferrer\">SharePoint Security Sync<\/a><\/strong> \u2013 Robust and secure solution to integrate Dynamics 365 CRM and SharePoint Security Sync thereby ensuring secure access to confidential documents stored in SharePoint.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We had a requirement in which we were supposed to use a addCustomView method in order to filter the Lookup. We came across a situation in which we were able to set the custom view(The view with filtered records) as a default view to restrict the users from choosing the other records, but if a\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2014\/09\/add-custom-view-in-lookup-and-disable-all-the-other-views\/\">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],"tags":[456,1012,1790],"class_list":["post-889","post","type-post","status-publish","format-standard","hentry","category-dynamics-crm","category-dynamics-crm-2013","category-dynamics-crm-2015","category-dynamics-crm-2016","tag-custom-view","tag-lookup","tag-views"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/889","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=889"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/889\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}