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




No comments:

Post a Comment