Description
Recently Microsoft introduced a provision for Azure Logic-Apps wherein the developers can add a built-in Inline JavaScript code action as a step in our Logic-App’s workflow. This action runs the code snippet and returns its output as a token named Result.
In this blog, we will try to create a D365 CE Contact record from a JSON message passed from Service Bus Explorer via a Logic-App.
We will first add a new CE Contact text field called PAN that accepts a Permanent Account Number (Indian Format). In Logic-App, we aim to use the Inline JS action to validate PAN against Reg-Ex expressions and create CE contact records only if they are in the correct format.
Steps
1. Login to D365 CE and create a new text field – PAN (Permanent Account Number) for the Contact entity. Place it on the main form. Save and Publish.
2. Create a new Azure Logic-App with the Trigger – “When a message is received in a Service Bus queue”.
3. Establish the connection to your Service-Bus and specify the queue name.
4. Add the next action in the Logic-App – Parse JSON. Set the *Content to json(decodeBase64(triggerBody()[‘ContentData’])). Set the schema as follows.
5. Next, add the action – Execute JavaScript Code. NOTE – Make sure an Integration Account is linked to your Azure Logic App. For more information - https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-add-run-inline-code.
6. Add the following code to this Action.
7. After it, add a new condition to check the Result of the above action. If it is False, Terminate the Logic-App with the Message – “PAN is in the incorrect format”. If the Result is True, create a new CE
8. Map the fields from the output of the “Parse JSON” action to the Dynamics 365 CE contact. Save all these changes.
Unit-Testing
We move to the Service Bus Explorer and send the following JSON message with PAN in the correct format.
We verify that the Logic-App has run correctly, and the CE Contact record is created as expected.
We now send a JSON message with an incorrect PAN and verify that the Logic-App is terminated with the expected message.
Conclusion
In this blog, Dynamics CRM Consultants describe step by step how we can use inline JS code action in logic applications to validate the D365 CE field.
We hope this will be helpful.