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 the Logic-App, we aim to make use of Inline JS action to validate PAN against a Reg-Ex expression and create the CE Contact record only if it is in the right 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.
{
"properties": {
"PAN": {
"type": "string"
},
"businessPhone": {
"type": "string"
},
"city": {
"type": "string"
},
"email": {
"type": "string"
},
"firstname": {
"type": "string"
},
"lastname": {
"type": "string"
}
},
"type": "object"
}
- 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.
var receivedPan = workflowContext.actions.Parse_JSON.outputs.body.PAN;
var panPattern = /^[A-Z]{5}[0-9]{4}[A-Z]{1}$/; //RegEx for a PAN (example: DDDPP5678K)
return panPattern.test(receivedPan)
- 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 contact record.
- 8. Map the fields from the output of the “Parse JSON” action to D365 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-application to validate the D365 CE field.
We hope this will be helpful.