Developing Scheduler For Dynamics 365 CE Using Azure Function - Part One

Introduction


Sometimes we need to develop a 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 an Azure time trigger to develop scheduler for Dynamics 365 CE. Let’s say we have  a requirement to generate entity record on 1 day of every month. Let's see how we can do this step by step.
 
Here are the things you need,
  1. Access to Dynamics 365 CE environment
  2. Azure subscription in the same tenant
  3. Visual Studio 2017 or later

Solution


First 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 it here.
 
Step 1
 
Start Visual Studio (We are using Visual Studio 2017) ->New Project and select Azure Function. Provide name for your Azure Function and click on Ok.
 
Developing Scheduler For Dynamics 365 CE Using Azure Function
 
Step 2
 
Select Azure Timer Trigger and pay attention to Azure Function version; it should be V1, currently V2 is not supported.
 
Developing Scheduler For Dynamics 365 CE Using Azure Function
 
Note
Make sure to check your Azure Function project Target framework, it should be 6.2 or later.
 
Step 3
 
We have modified our Azure Function project like following, we re-named function class, added a new folder and added a new class where we will be writing our logic to connect to Dynamics 365 CE.
 
Developing Scheduler For Dynamics 365 CE Using Azure Function
 
Step 4
 
Now let’s add required assemblies to our Azure Function, right click on the project and select Manage Nuget Packages, we need to add the following two references.
 
 
Developing Scheduler For Dynamics 365 CE Using Azure Function
 
Step 5
 
Now let’s add the following references to our logic class
 
Developing Scheduler For Dynamics 365 CE Using Azure Function
 
Step 6
 
Now we need to add the following code in our log class 
  1. private static OrganizationServiceProxy GetDynamicsCRMService() {  
  2.  ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;  
  3.    
  4.  IServiceManagement < IOrganizationService > orgServiceManagement = ServiceConfigurationFactory.CreateManagement < IOrganizationService > (new Uri("https://Dynamics365OrgName.crm8.dynamics.com/XRMServices/2011/Organization.svc"));  
  5.    
  6.  AuthenticationCredentials authCredentials = new AuthenticationCredentials();  
  7.  authCredentials.ClientCredentials.UserName.UserName = "Dynamics365UserName";  
  8.  authCredentials.ClientCredentials.UserName.Password = "Password";  
  9.    
  10.  AuthenticationCredentials tokenCredentials = orgServiceManagement.Authenticate(authCredentials);  
  11.    
  12.  OrganizationServiceProxy service=new OrganizationServiceProxy(orgServiceManagement, tokenCredentials.SecurityTokenResponse);  
  13.    
  14.  service.Timeout = new TimeSpan(1, 30, 0);  //for longer run  
  15.    
  16.  return service;  
  17.    
  18. }  
  19. public static void GenerateAccountRecords() {  
  20.  OrganizationServiceProxy service = GetDynamicsCRMService();  
  21.  for (int i = 0; i <= 10; i++) {  
  22.   Entity _account = new Entity("account");  
  23.   _account["name"] = "Sample Account" + i;  
  24.    
  25.   service.Create(_account);  
  26.    
  27.  }  
  28.    
  29. }  
In the first method we created we connected to Dynamics 365 CE organization using hard coded user name and password; this is for the demo purposes. We can use application's user as well to connect to Dynamics 365 CE, please refer our detailed article on how to use application user to connect to Dynamics 365 CE.
 
Now we need to call this method from our Azure function, we can use the following to call it.
 
Developing Scheduler For Dynamics 365 CE Using Azure Function 
If you will notice that we are using RunOnStartup = true which runs Azure Function once after as soon as it is started.

Now 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,
 
Developing Scheduler For Dynamics 365 CE Using Azure Function
 

Summary


In this article we learned how we can integrate Azure with Dynamics 365 CE and can built scheduler for Dynamics 365 CE. Keep in mind that by default Dynamics 365 CE API times out after 10 minutes, 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.