Thursday, January 19, 2017

Dynamics 365 and Logic Apps–Part 2


In the first part of the blog, I had created a Logic App that serves as a callable endpoint to create a record in CRM. In this blog, we will look at adding some exception handling to the same app. Please read the first part Using Logic Apps to create records in Dynamics 365 - Part 1 to understand this part.

Current Solution

The process for the current solution in depicted in the following diagram

2017-01-13_16-57-42

If the Create a new record is successful the Response action will return the customer else the Logic app will through an error message.

What will happen when CRM throws an error

For this blog, I have created a synchronous workflow that will throw an error message if the Last Name is not provided in the request.

Error 1 (ResponseTimeOut)

I am sending the following request without the last name.
 {  
  "customer": {  
   "firstName": "MSCRMShop",  
   "lastName":,  
   "streetAddress": "21 2nd Street",  
   "city": "New York"  
  }  
 }  
The Logic App returned the following error message.
 {  
  "error": {  
   "code": "ResponseTimeout",  
   "message": "The server did not received a timely response from the upstream server. Request tracking id '08587170410911854011608943569'."  
  }  
 }  

Reason

The reason behind the timeout error is that there is timeout limit of 90 seconds for a single http request in Logic Apps. The other question is that why CRM would take so long to return the error message. The reason is that CRM is not taking that long to throw the error message. It is the default retry policy of the Logic Apps that will try 5 times before throwing the error message.

For verification, open the Logic App in Azure portal and look at the failed run. The action will display the message shown in the following screen shot.

2017-01-16_23-00-46

The App run is showing that the execution duration is 1.92 minutes. The error captured in Create a new record is the actual error thrown by CRM
Now log on to CRM and check the execution history of the workflow. It will display that it has been executed 5 times.

2017-01-16_23-21-03

Solution

The solution in this scenario is to set Retry Policy to None for the action Create a new record. Switch to code view of the app and add the retry policy section as shown in the following screen shot.

2017-01-17_14-44-26

Error 2 (NoResponse)

After fixing the Error 1, call the Logic App again by passing same body as in Error 1. This time the app will return the following error.
 {  
  "error": {  
   "code": "NoResponse",  
   "message": "The server did not received a response from an upstream server. Request tracking id '08587169819578381630235279815'."  
  }  
 }  

Reason

The reason is that there is no response defined in our current solution to handle the error. The only response defined in the Logic App is to return the customer Id on successful creation of the record in CRM. Every action in Logic Apps have a section named “runAfter” which defined when this action will be executed.

2017-01-17_15-03-14

Solution

The solution in this scenario is to add another Response action that will be executed after the failed execution of Create a new record action.
  1. Add a new Response action to the app and set the properties as shown in the screen shot below.

    2017-01-17_22-24-44

    Status Code is set to output of Create a new Record, Headers section contains the content-type and Body is set to body of Output of Create a new Record.
  2. At this stage Response 2 will be fired on successful execution of Response action.
  3. Switch to the code view of the app. Scroll down to Response 2 definition.
  4. Change the runAfter section as shown below.

    2017-01-17_16-46-49
  5. Save the app.
  6. Switch back to designer mode and if there is no error in the app. It will look like the following screen.

    2017-01-17_22-29-39
  7. Test the Logic App again without passing the last name in the request body. This time Logic app will return the following message.

    2017-01-17_22-34-10

This solution will only capture the errors thrown by CRM (Create_a_new_record action). If the error occurs before the call that won’t be captured by the solution.


More Resources

Check the following resources for advanced error handling information.https://docs.microsoft.com/en-us/azure/app-service-logic/app-service-logic-exception-handling
https://docs.microsoft.com/en-us/azure/app-service-logic/app-service-logic-scenario-error-and-exception-handling




Monday, January 9, 2017

Using Logic Apps to create records in Dynamics 365 - Part 1

According to Microsoft documentation, Logic Apps is a service to simplify and implement scalable integration and workflows in the cloud. As a CRM professional I was aware of Microsoft Flow but not of Logic Apps. Microsoft Flow is actually built on Logic App Most of the features and API connections are the same.

The major difference is that Microsoft Flow is targeted for business users whereas Logic Apps is targeted towards IT professionals and developers. Logic Apps can have some additional connections like BizTalk APIs. The other major difference is that Microsoft Flow workflows are created in production but Logic Apps provides the DevOps and security assurance.

The following link details the comparisons between Microsoft Flow and Logic Apps
https://docs.microsoft.com/en-us/azure/azure-functions/functions-compare-logic-apps-ms-flow-webjobs#flow-vs-logic-apps
For this Blog I am building a Logic App that will act as endpoints to create records in Dynamics 365. The good thing is that we don’t need to write code to create the endpoints.

Create a Logic App

  1. Logon to the Microsoft Azure Portal.
  2. Create a new Logic App by Navigating to New>>Web +Mobile>>Logic App as shown in the screen shot below.

    2017-01-05_16-01-49
  3. Enter the app name and other information as required and click Create. (I generally checked “Pin to Dashboard” to open the app straight from the dashboard)

    2017-01-05_16-05-45
  4. Open the newly created app.

    2017-01-09_21-37-24
  5. Click on Edit to open the designer.

    image
  6. Select Request from the list of the managed APIs as shown in the screen shot.

    2017-01-07_22-35-44
  7. Enter the JSON schema for the request. I have created a customer object with first name, last name, street address and city.

    2017-01-08_17-41-31
  8. Click Save. It will populate the URL field of the request. This URL will be used as endpoints to call the Logic App.
  9. Click New step and choose Add an action.

    2017-01-08_17-44-13
  10. Select Dynamics 365 – Create a new record from the managed APIs list.

    2017-01-08_17-46-17
  11. Choose the Organisation Name, choose the Contacts as the entity name and map the first name, last name, street address and city with the JSON schema added in step 7.

    2017-01-08_17-47-52
  12. Click New step and choose Add an action as shown in the screen shot below.

    2017-01-08_17-49-19
  13. Select Response from the list.
  14. Enter the following information as shown in the screen shot below
    • Status Code of 200 (Which represents successful call).
    • Enter the “content-type” in the Headers section.
    • Enter the JSON in the body section. I am returning the contactid of the newly created contact.2017-01-08_17-52-28
  15. Save the app and test it.

Testing the Logic App

For testing we need to call Logic App using URL created for Request step 8 of Create a Logic App.

2017-01-09_22-31-29

For this blog, I am not creating a new application to call the endpoints(Logic App). I am using PostMan to create and send the request. Here are the steps.

  1. Open the PostMan application.

    2017-01-09_22-38-28
  2. Enter the URL copied from the Request.
  3. Enter the Headers as shown in the screen shot below. The endpoints are expecting JSON.

    2017-01-09_22-43-28
  4. Enter the Body and Click Send. The  Logic App will return the customer id as shown in the screen shot below.

    2017-01-09_22-50-39
  5. Open the CRM and check the contact is created.
  6. If there is an error, go to Azure portal and look the app summary and troubleshoot the error.

    2017-01-09_22-56-07


That is it . You got your Logic App as callable endpoints.