{"id":1642,"date":"2015-08-20T15:01:25","date_gmt":"2015-08-20T09:31:25","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=1642"},"modified":"2021-08-25T10:55:06","modified_gmt":"2021-08-25T10:55:06","slug":"apply-custom-filter-on-lookup-field-in-dynamic-crm-using-script","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2015\/08\/apply-custom-filter-on-lookup-field-in-dynamic-crm-using-script\/","title":{"rendered":"Apply Custom Filter on Lookup Field in Dynamics CRM using Script"},"content":{"rendered":"<h2><strong>Introduction:<\/strong><\/h2>\n<p>In this blog, we are going to see <em>how to apply a custom filter to the lookup field using the JavaScript functions<\/em>.<\/p>\n<p>Microsoft Dynamics CRM allows us to filter a lookup field on the form using the Fetch XML condition and \u201caddPreSearch()\u201c method.<\/p>\n<h2><strong>Example:<\/strong><\/h2>\n<p>On the Contact Entity, there is a lookup field named <em>&#8216;Account Name&#8217;<\/em> and a text field <em>&#8216;Address1: City&#8217;<\/em> as shown in the below screenshot;<a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/08\/custom_filter6.png\"><img decoding=\"async\" class=\"aligncenter wp-image-3600 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/08\/custom_filter6.png\" alt=\"Apply Custom Filter on Lookup Field in Dynamics CRM using Script\" width=\"629\" height=\"428\" \/><\/a><\/p>\n<p>So, if we want to filter Account records in lookup view by city having value equal to field Address1: City. We can do this by writing the below code in JScript.<\/p>\n<p>Here we have written two functions <em>&#8216;filterLookup()&#8217;<\/em> and <em>&#8216;addCustomeLookupFilter()&#8217;<\/em>\u00a0 as shown in the below code snippets;<a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/08\/custom_filter1.png\"><img decoding=\"async\" class=\"aligncenter wp-image-1644 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/08\/custom_filter1.png\" alt=\"Apply Custom Filter on Lookup Field in Dynamics CRM using Script\" width=\"629\" height=\"254\" \/><\/a><\/p>\n<p>We have created CRM webresource for the javascript and called <em>&#8216;filterLookup&#8217;<\/em>\u00a0function on change event for the field <em>&#8216;Address1: City&#8217;<\/em> field as below for contact entity form.<a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/08\/custom_filter2.png\"><img decoding=\"async\" class=\"aligncenter wp-image-1645 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/08\/custom_filter2.png\" alt=\"Apply Custom Filter on Lookup Field in Dynamics CRM using Script\" width=\"630\" height=\"316\" \/><\/a><\/p>\n<p>Function <em>&#8216;filterLookup&#8217;<\/em>\u00a0will be triggered on the change of field <em>&#8216;Address1: City&#8217;<\/em>. This binds <em>&#8216;addPreSearch&#8217;<\/em>\u00a0event to lookup control <em>&#8216;parentcustomerid&#8217;<\/em>.<a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/08\/custom_filter3.png\"><img decoding=\"async\" class=\"aligncenter wp-image-1646 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/08\/custom_filter3.png\" alt=\"Apply Custom Filter on Lookup Field in Dynamics CRM using Script\" width=\"629\" height=\"261\" \/><\/a><\/p>\n<p>Open the contact entity record, Before entering the <em>&#8216;Address1:City&#8217;<\/em> field value the lookup field shows all the account records as below screenshot;<a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/08\/custom_filter41.png\"><img decoding=\"async\" class=\"aligncenter wp-image-3601 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/08\/custom_filter41.png\" alt=\"Apply Custom Filter on Lookup Field in Dynamics CRM using Script\" width=\"630\" height=\"438\" \/><\/a><\/p>\n<p>Enter the value for <em>&#8216;Address1: City&#8217;<\/em> here it is <em>&#8216;US&#8217;<\/em>\u00a0as below;<a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/08\/custom_filter51.png\"><img decoding=\"async\" class=\"aligncenter wp-image-3602 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/08\/custom_filter51.png\" alt=\"Apply Custom Filter on Lookup Field in Dynamics CRM using Script\" width=\"629\" height=\"250\" \/><\/a><\/p>\n<p>Then Check for the suggested options for the Account lookup. Only those accounts records will be available to select which have the city as <em>&#8216;US&#8217;<\/em>.<\/p>\n<p>Hope this helps!<\/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>Introduction: In this blog, we are going to see how to apply a custom filter to the lookup field using the JavaScript functions. Microsoft Dynamics CRM allows us to filter a lookup field on the form using the Fetch XML condition and \u201caddPreSearch()\u201c method. Example: On the Contact Entity, there is a lookup field named\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2015\/08\/apply-custom-filter-on-lookup-field-in-dynamic-crm-using-script\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":10685,"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":[15,19,21,22,24,33],"tags":[446,529],"class_list":["post-1642","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","category-dynamics-crm","category-dynamics-crm-2013","category-dynamics-crm-2015","category-dynamics-crm-2016","category-javascript","tag-custom-filter-on-lookup-field","tag-dynamcis-crm-2015"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/1642","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=1642"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/1642\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/10685"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=1642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=1642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=1642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}