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,
- Access to Dynamics 365 CE environment
- Azure subscription in the same tenant
- 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.
Step 2
Select Azure Timer Trigger and pay attention to Azure Function version; it should be V1, currently V2 is not supported.
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.
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.
Step 5
Now let’s add the following references to our logic class
Step 6
Now we need to add the following code in our log class
- private static OrganizationServiceProxy GetDynamicsCRMService() {
- ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
-
- IServiceManagement < IOrganizationService > orgServiceManagement = ServiceConfigurationFactory.CreateManagement < IOrganizationService > (new Uri("https://Dynamics365OrgName.crm8.dynamics.com/XRMServices/2011/Organization.svc"));
-
- AuthenticationCredentials authCredentials = new AuthenticationCredentials();
- authCredentials.ClientCredentials.UserName.UserName = "Dynamics365UserName";
- authCredentials.ClientCredentials.UserName.Password = "Password";
-
- AuthenticationCredentials tokenCredentials = orgServiceManagement.Authenticate(authCredentials);
-
- OrganizationServiceProxy service=new OrganizationServiceProxy(orgServiceManagement, tokenCredentials.SecurityTokenResponse);
-
- service.Timeout = new TimeSpan(1, 30, 0);
-
- return service;
-
- }
- public static void GenerateAccountRecords() {
- OrganizationServiceProxy service = GetDynamicsCRMService();
- for (int i = 0; i <= 10; i++) {
- Entity _account = new Entity("account");
- _account["name"] = "Sample Account" + i;
-
- service.Create(_account);
-
- }
-
- }
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.
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,
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.