{"id":10867,"date":"2017-12-06T17:54:46","date_gmt":"2017-12-06T12:24:46","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=10867"},"modified":"2022-07-01T14:59:08","modified_gmt":"2022-07-01T09:29:08","slug":"create-month-or-year-only-field-in-dynamics-365","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2017\/12\/create-month-or-year-only-field-in-dynamics-365\/","title":{"rendered":"Create Month or Year Only Field in Dynamics 365"},"content":{"rendered":"<h2 style=\"text-align: justify;\"><strong>Introduction: <\/strong><\/h2>\n<p style=\"text-align: justify;\">A lot of time we come across the situations where we would need to have one field to store just the <em>Month<\/em> part of the date and another one to store the <em>Year<\/em> part of the date. For example, Credit or Debit Card validity period where the user has to select Month and Year. In such cases, we could create one <em>optionset field<\/em> with options to select Month and number field to enter the year. In some scenarios, we create one <em>Date only<\/em> field and write a script or workflow to take the month and year part out from the selected date and store it in a separate Month and Year fields.<\/p>\n<p style=\"text-align: justify;\">We have now found a better approach to tackle this kind of situation.<\/p>\n<p style=\"text-align: justify;\">We can use the <em>\u2018Auto-Complete\u2019<\/em> control for this purpose. The Auto-Complete control was introduced in Dynamics CRM 2016. This control provides suggestions to the user as soon as the user starts typing in the text fields.<\/p>\n<p style=\"text-align: justify;\"><em>Refer this <\/em><a href=\"https:\/\/www.inogic.com\/blog\/2015\/12\/design-auto-complete-text-controls-in-dynamics-crm-2016\/\" target=\"_blank\" rel=\"noopener noreferrer\"><em>article<\/em><\/a><em> to know more about Auto-Complete control introduced in Dynamics CRM 2016.<\/em><\/p>\n<h2 style=\"text-align: justify;\"><strong>Steps to create a Month and Year only fields using the Auto-Complete control:<\/strong><\/h2>\n<p style=\"text-align: justify; padding-left: 30px;\"><strong>1<\/strong>. First, create two text fields namely, <em>\u2018Month\u2019<\/em> and <em>\u2018Year\u2019<\/em>.<\/p>\n<p style=\"text-align: justify; padding-left: 30px;\"><strong>2<\/strong>. Place these two fields on the entity form<\/p>\n<p style=\"text-align: justify; padding-left: 30px;\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-10868\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2017\/12\/Create-Month-or-Year-Only-Field-in-Dynamics-CRM1.png\" alt=\"Create Month or Year Only Field in Dynamics CRM\" width=\"516\" height=\"203\" \/><\/p>\n<p style=\"text-align: justify; padding-left: 30px;\"><strong>3<\/strong>. Now, write a script to make these two fields act as an autocomplete and bind month and year collection to Month and Year field respectively.<\/p>\n<p>Month collection:<\/p>\n<pre class=\"lang:default decode:true \">var months = [\r\n    { name: 'Jan', code: '01' },\r\n    { name: 'Feb', code: '02' },\r\n    { name: 'Mar', code: '03' },\r\n    { name: 'Apr', code: '04' },\r\n    { name: 'May', code: '05' },\r\n    { name: 'June', code: '06' },\r\n    { name: 'Jul', code: '07' },\r\n    { name: 'Aug', code: '08' },\r\n    { name: 'Sep', code: '09' },\r\n    { name: 'Oct', code: '10' },\r\n    { name: 'Nov', code: '11' },\r\n    { name: 'Dec', code: '12' }\r\n      ];\r\n<\/pre>\n<p>Year collection :<\/p>\n<pre class=\"lang:default decode:true \">\/\/get the start year as current year \u2013 2 year\r\n    var startYear = (new Date()).getFullYear() - 2;\r\n    \/\/get the end year as current year + 15\r\n    var endYear = (new Date()).getFullYear() + 15;\r\n\r\n    var yearList = [];\r\n    \/\/loop though endyear\r\n    for (i = startYear; i &lt;= endYear ; i++) {\r\n        var obj = {};\r\n        \/\/set name for object array\r\n        obj[\"name\"] = i.toString();\r\n        \/\/set code for object array\r\n        obj[\"code\"] = i.toString();\r\n        \/\/ insert into object array\r\n        yearList.push(obj);\r\n    }\r\n<\/pre>\n<p>This list is going start from the last two years from the current year and will end with next 15 years from the current year.<\/p>\n<p style=\"text-align: justify;\">For example, if the current year is 2017 then the autocomplete suggestion list will show the list years start from 2015 to 2024.\u00a0You can use your own logic to create the year list.<\/p>\n<p style=\"padding-left: 30px; text-align: justify;\"><strong>4<\/strong>. Call suggestMonth function on a load of form.<\/p>\n<pre class=\"lang:default decode:true\">var suggestMonths = function (attributeName) {\r\n\r\n    var    months = [\r\n  { name: 'Jan', code: '01' },\r\n  { name: 'Feb', code: '02' },\r\n  { name: 'Mar', code: '03' },\r\n  { name: 'Apr', code: '04' },\r\n  { name: 'May', code: '05' },\r\n  { name: 'June', code: '06' },\r\n  { name: 'Jul', code: '07' },\r\n  { name: 'Aug', code: '08' },\r\n  { name: 'Sep', code: '09' },\r\n  { name: 'Oct', code: '10' },\r\n  { name: 'Nov', code: '11' },\r\n  { name: 'Dec', code: '12' }\r\n    ];\r\n\r\n    var keyPressFcn = function (ext) {\r\n        try {\r\n            var userInput = Xrm.Page.getControl(attributeName).getValue();\r\n            resultSet = {\r\n                results: new Array(),\r\n                commands: {\r\n                    id: \"sp_commands\",\r\n                    label: \"Learn More\",\r\n                    action: function () {\r\n                    }\r\n                }\r\n            };\r\n\r\n            var userInputLowerCase = userInput.toLowerCase();\r\n            for (i = 0; i &lt; months.length; i++) {\r\n                if (userInputLowerCase === months[i].name.substring(0, userInputLowerCase.length).toLowerCase()) {\r\n                    resultSet.results.push({\r\n                        id: i,\r\n                        fields: [months[i].name]\r\n                    });\r\n                }\r\n                if (resultSet.results.length &gt;= 10) break;\r\n            }\r\n\r\n            if (resultSet.results.length &gt; 0) {\r\n                ext.getEventSource().showAutoComplete(resultSet);\r\n            } else {\r\n                ext.getEventSource().hideAutoComplete();\r\n            }\r\n        } catch (e) {\r\n            \/\/ Handle any exceptions. In the sample code,\r\n            \/\/ we are just displaying the exception, if any.\r\n            console.log(e);\r\n        }\r\n    };\r\n\r\n    Xrm.Page.getControl(attributeName).addOnKeyPress(keyPressFcn);\r\n};<\/pre>\n<p style=\"padding-left: 30px;\">\u00a0<strong>5<\/strong>. Call suggestYear function on load of form.<\/p>\n<pre class=\"lang:default decode:true \">var suggestYear = function (attributeName) {\r\n    \/\/ var getCurrentYear = +15;\r\n    \/\/get the current year -2 i.e starting year\r\n    var startYear = (new Date()).getFullYear() - 2;\r\n    \/\/get the current year +15 i.e End year\r\n    var endYear = (new Date()).getFullYear() + 15;\r\n\r\n    \/\/ List of sample account names to suggest\r\n    var yearList = [];\r\n    \/\/loop though endyear\r\n    for (i = startYear; i &lt;= endYear ; i++) {\r\n        var obj = {};\r\n        \/\/set name for object array\r\n        obj[\"name\"] = i.toString();\r\n        \/\/set code for object array\r\n        obj[\"code\"] = i.toString();\r\n        \/\/ insert into object array\r\n        yearList.push(obj);\r\n    }\r\n\r\n\r\n\r\n    var keyPressFcn = function (ext) {\r\n        try {\r\n            \/\/get input value\r\n            var userInput = Xrm.Page.getControl(attributeName).getValue();\r\n            resultSet = {\r\n                results: new Array(),\r\n                commands: {\r\n                    id: \"sp_commands\",\r\n                    label: \"Learn More\",\r\n                    action: function () {\r\n                        \r\n                    }\r\n                }\r\n            };\r\n            \/\/get user input value and convert inyo lower case\r\n            var userInputLowerCase = userInput.toLowerCase();\r\n            \/\/loop thought the array list\r\n            for (i = 0; i &lt; yearList.length; i++) {\r\n                \/\/match the list with inputed value\r\n                if (userInputLowerCase === yearList[i].name.substring(0, userInputLowerCase.length).toLowerCase()) {\r\n                    resultSet.results.push({\r\n                        id: i,\r\n                        fields: [yearList[i].name]\r\n                    });\r\n                }\r\n                \/\/display matching list upto 10\r\n                if (resultSet.results.length &gt;= 10) break;\r\n            }\r\n\r\n            \/\/set list if greater then 0\r\n            if (resultSet.results.length &gt; 0) {\r\n                \/\/set auto complete list\r\n                ext.getEventSource().showAutoComplete(resultSet);\r\n            } else {\r\n                ext.getEventSource().hideAutoComplete();\r\n            }\r\n        } catch (e) {\r\n            \/\/ Handle any exceptions. In the sample code,\r\n            \/\/ we are just displaying the exception, if any.\r\n            console.log(e);\r\n        }\r\n    };\r\n\r\n    Xrm.Page.getControl(attributeName).addOnKeyPress(keyPressFcn);\r\n};\r\n<\/pre>\n<p style=\"padding-left: 30px;\"><strong>6<\/strong>. This way our standard text fields becomes auto-complete fields and will start showing the suggestion for month and year as soon as user starts typing as seen in the screenshot below;<\/p>\n<p style=\"text-align: justify;\">Month:<\/p>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-10869\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2017\/12\/Create-Month-or-Year-Only-Field-in-Dynamics-CRM2.png\" alt=\"Create Month or Year Only Field in Dynamics CRM\" width=\"406\" height=\"218\" \/><\/p>\n<p style=\"text-align: justify;\">Year:<\/p>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-10870\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2017\/12\/Create-Month-or-Year-Only-Field-in-Dynamics-CRM3.png\" alt=\"Create Month or Year Only Field in Dynamics CRM\" width=\"371\" height=\"358\" \/><\/p>\n<p style=\"padding-left: 30px; text-align: justify;\"><strong>7<\/strong>. Then finally add the validation code on change of these two fields so that the user cannot enter values other than the values present in the auto-complete\u00a0list.<\/p>\n<p style=\"padding-left: 30px; text-align: justify;\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-10871\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2017\/12\/Create-Month-or-Year-Only-Field-in-Dynamics-CRM4.png\" alt=\"Create Month or Year Only Field in Dynamics CRM\" width=\"417\" height=\"105\" \/><\/p>\n<blockquote><p><strong><em>Note: Supported versions of this solution are CRM 2016 and above. <\/em><\/strong><\/p><\/blockquote>\n<h2><strong>Conclusion:<\/strong><\/h2>\n<p>Using the above-mentioned steps, the user can make use of auto-complete control feature of Dynamics 365 to create Month and Year only field.<\/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\">Cut short 90% of your manual work and repetitive data entry!<\/div><\/div><\/h2>\n<p style=\"text-align: left;\"><em>Get 1 Click apps and say goodbye to all repetitive data entry in CRM &#8211;<\/em><br \/>\n<em><strong><a href=\"https:\/\/bit.ly\/3oH7dYw\" target=\"_blank\" rel=\"noopener noreferrer\">Click2Clone<\/a> <\/strong>\u2013 Clone\/Copy Dynamics 365 CRM records in 1 Click<\/em><br \/>\n<em><strong><a href=\"https:\/\/bit.ly\/3EPjAYc\" target=\"_blank\" rel=\"noopener noreferrer\">Click2Export<\/a><\/strong> \u2013 Export Dynamics 365 CRM Report\/CRM Views\/Word\/Excel template in 1 Click<\/em><br \/>\n<em><strong><a href=\"https:\/\/bit.ly\/3EN8h2v\" target=\"_blank\" rel=\"noopener noreferrer\">Click2Undo<\/a><\/strong> \u2013 Undo &amp; Restore Dynamics 365 CRM data in 1 Click<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: A lot of time we come across the situations where we would need to have one field to store just the Month part of the date and another one to store the Year part of the date. For example, Credit or Debit Card validity period where the user has to select Month and Year.\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2017\/12\/create-month-or-year-only-field-in-dynamics-365\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":10877,"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":[16,19],"tags":[],"class_list":["post-10867","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dynamics-365","category-dynamics-crm"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/10867","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=10867"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/10867\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/10877"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=10867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=10867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=10867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}