{"id":3689,"date":"2020-01-04T19:20:57","date_gmt":"2020-01-04T19:20:57","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=3689"},"modified":"2020-01-04T19:20:57","modified_gmt":"2020-01-04T19:20:57","slug":"developing-scheduler-for-dynamics-365-ce-using-azure-function-part-1","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=3689","title":{"rendered":"Developing Scheduler for Dynamics 365 CE using Azure Function Part 1"},"content":{"rendered":"<p><strong>Introduction<\/strong><br \/>\nSometimes we require to develop scheduler job for Dynamics 365 CE online. Currently we have different options to implement scheduler for Dynamics 365 CE online for example using workflow, Power Automate, Azure etc. In this article we are going to discuss how we can use Azure time trigger to develop scheduler for Dynamics 365 CE. Let&#8217;s say we have requirement to generate entity record on 1 day of every month, let see how we can do this step by step.<\/p>\n<p>Here are the things you need<br \/>\n1. Access to Dynamics 365 CE environment<br \/>\n2. Azure subscription<br \/>\n3. Visual Studio 2017 or later<\/p>\n<p><strong>Solution<\/strong><br \/>\nFirst we are going to create Azure function trigger using Visual Studio. We have written articles about developing Azure integration by writing code in Azure portal, you can check <a href=\"https:\/\/himbap.com\/blog\/?p=3371\">it here<\/a>.<br \/>\n1. Start Visual Studio (We are using Visual Studio 2017) -&gt;<strong>New Project<\/strong> and select <strong>Azure Function<\/strong>. Provide name for your Azure Function and click on Ok.<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3691 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az1-300x181.png\" alt=\"Az1\" width=\"300\" height=\"181\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az1-300x181.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az1-624x376.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az1.png 933w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n2. Select Azure Timer Trigger and pay attention to Azure Function version it should be V1, currently V2 is not supported.<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3692 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az2-300x173.png\" alt=\"Az2\" width=\"300\" height=\"173\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az2-300x173.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az2-624x360.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az2.png 717w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><strong>Note: Make sure to check your Azure Function project Target framework, it should be 6.2 or later<\/strong><\/p>\n<p>3. We have modified our Azure Function project like following, we renammed function class, added a new folder and added a new class where we will be writing our logic to connect to Dynamics 365 CE.<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az3.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3693 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az3-300x239.png\" alt=\"Az3\" width=\"300\" height=\"239\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az3-300x239.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az3.png 390w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>4. Now let&#8217;s add required assemblies to our Azure Function, right click on the project and select <strong>Manage Nuget Packages<\/strong>, we need add following two references<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az4.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3694 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az4-300x66.png\" alt=\"Az4\" width=\"300\" height=\"66\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az4-300x66.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az4-1024x227.png 1024w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az4-624x138.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az4.png 1130w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az5.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3695 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az5-300x62.png\" alt=\"Az5\" width=\"300\" height=\"62\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az5-300x62.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az5-624x129.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az5.png 951w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n5. Now let&#8217;s add following references to our logic class<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az6.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3696 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az6-300x71.png\" alt=\"Az6\" width=\"300\" height=\"71\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az6-300x71.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az6.png 446w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n6. Now we need to add following code in our log class<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n  private static OrganizationServiceProxy GetDynamicsCRMService() {\r\n   ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;\r\n\r\n   IServiceManagement &lt; IOrganizationService &gt; orgServiceManagement = ServiceConfigurationFactory.CreateManagement &lt; IOrganizationService &gt; (new Uri(&quot;https:\/\/Dynamics365OrgName.crm8.dynamics.com\/XRMServices\/2011\/Organization.svc&quot;));\r\n\r\n   AuthenticationCredentials authCredentials = new AuthenticationCredentials();\r\n   authCredentials.ClientCredentials.UserName.UserName = &quot;Dynamics365UserName&quot;;\r\n   authCredentials.ClientCredentials.UserName.Password = &quot;Password&quot;;\r\n\r\n   AuthenticationCredentials tokenCredentials = orgServiceManagement.Authenticate(authCredentials);\r\n\r\n   OrganizationServiceProxy service=new OrganizationServiceProxy(orgServiceManagement, tokenCredentials.SecurityTokenResponse);\r\n\r\n   service.Timeout = new TimeSpan(1, 30, 0);  \/\/for longer run\r\n\r\n   return service;\r\n\r\n  }\r\n  public static void GenerateAccountRecords() {\r\n   OrganizationServiceProxy service = GetDynamicsCRMService();\r\n   for (int i = 0; i &lt;= 10; i++) {\r\n    Entity _account = new Entity(&quot;account&quot;);\r\n    _account[&quot;name&quot;] = &quot;Sample Account&quot; + i;\r\n\r\n    service.Create(_account);\r\n\r\n   }\r\n\r\n  }\r\n<\/pre>\n<p>In the first method we are created connecting to Dynamics 365 CE organization using hard coded user name and password, this is for the demo purpose, we can use application user as well to connect to Dynamics 365 CE, please refer our <a href=\"https:\/\/himbap.com\/blog\/?p=3448\">detailed article<\/a> on how to use application user to connect to Dynamics 365 CE.<\/p>\n<p>Now we need to call this method from our azure function, we can use following to call it<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az7.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3697 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az7-300x81.png\" alt=\"Az7\" width=\"300\" height=\"81\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az7-300x81.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az7-624x169.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az7.png 1005w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>If you will notice that we are using <strong>RunOnStartup = true <\/strong>which runs Azure Function once after as soon as it is started.<br \/>\nNow we are ready to test our Azure Function trigger, press F5 and test, once this is executed we should be able to see sample data created in Dynamics 365 CE like below:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az8.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3698 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az8-300x222.png\" alt=\"Az8\" width=\"300\" height=\"222\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az8-300x222.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/01\/Az8.png 527w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><strong>Summary<\/strong><br \/>\nIn this articles we learned how we can integration Azure with Dynamics 365 CE and can built scheduler for Dynamics 365 CE. Keep in mind by default Dynamics 365 CE API timeout after 10 minute but we can change the timeout limit using Timeout property, for our demo we set it to 30, you can change it based on your requirement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Sometimes we require to develop scheduler job for Dynamics 365 CE online. Currently we have different options to implement scheduler for Dynamics 365 CE online for example using workflow, Power Automate, Azure etc. In this article we are going to discuss how we can use Azure time trigger to develop scheduler for Dynamics 365 CE. Let&#8217;s say we have&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=3689\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":3407,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[491,21,402],"tags":[661,727,726,725],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3689"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3689"}],"version-history":[{"count":7,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3689\/revisions"}],"predecessor-version":[{"id":3712,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3689\/revisions\/3712"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/media\/3407"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}