Advertisements

Archive

Archive for December 20, 2018

[Logic App and HTTP Request Trigger] Create a record in D365

December 20, 2018 Leave a comment

We got an ask from client that they should be able to Insert contacts in D365 from their Java application and requested our help.

To fulfill the ask, one option would be configuring S2S authentication so that Java application can connect to D365 using ADAL library and insert contacts.

Another option is using “Logic App” with “Http Request Trigger”.

High level approach:

  • Create a Logic App with ‘When a HTTP request is received’ trigger
  • Pass Contact record information in Json format as Request
    • Use ObjGen site to generate desired Json data.
    • This site seamlessly generates Json data based on our input. As I am going to create a Contact record, I generated below JSon data.

LogicApp_3

  • From Logic App, read the Json data and create Contact record using D365 connector
  • Finally capture the created Contact GUID in ‘Response’

Lets see the step by step approach to create Logic App and test.

Pre-requisites:

  • Dynamics 365 instance. Subscribe for trail if you have’t already
  • Azure Subscription.Subscribe for trail if you have’t already

Once you got the D365 and Azure Portal subscriptions below are the steps to create LogicApp.

Create a Logic App:

  • Connect to your Azure Portal and create a new ‘Logic Apps’. Refer article for steps to create Logic App.
  • From the list select ‘When a HTTP request is received’ trigger

LogicApp_4

  • In the next screen, click on “Use sample payload to generate schema” to generate the Json schema.

LogicApp_5

  • Paste the Json format prepared in ObjGen site

LogicApp_6

  • Click on ‘Done’ so that LogicApp generates “Request Body jSON Schema”

LogicApp_7

  • Next, we need to connect to D365 to save the Contact

LogicApp_8

  • Click ‘New Step’ and select ‘Dynamics 365’ connector. From the ‘Actions’ select ‘Create a new record’ and provide credentials and connect to your Instance

LogicApp_9

  • From the ‘Entity Name’, select ‘Contacts’
  • Map the Contact entity fields with the fields from ‘Dynamic content’ popup.
    • As ‘Company Name’ is a Look up to ‘Account’ entity, make sure you map
      • ‘Company Name Type’ = ‘accounts’
      • ‘Company Name’ = ‘id’ passed from Json

LogicApp_10

  • Next we need to capture the ‘Response’, choose ‘Response’ from ‘Actions’

LogicApp_11

  • In the ‘Response’ control,
    • In the ‘Headers’, add ‘Content-Type’ as key and ‘application/json’ as Value.
    • In the ‘Body’, form your output string and ‘Contact’ field from ‘Dynamic Content’ to capture the GUID

LogicApp_12

  • Save the Logic App

Test the Logic App:

  • Copy the “HTTP POST URL” from the “When a HTTP request is received” control.

LogicApp_13

Using Post Man:

  • Create a ‘POST’ request and paste the URL copied from Logic APP
  • In the ‘Headers’, add ‘Content-Type’ as key and ‘application/json’ as Value.

LogicApp_14

  • In the ‘Body’, paste the Json (You can copy the Json format prepared in ObjGen site)
  • Click ‘Send’ to call Logic App and capture the Response.

LogicApp_2

Using jScript and HTML:

  •  Below the jScript to call the Logic APP URL by passing JSon and capture the response

<html lang=”en” xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<meta charset=”utf-8″ />
<title>Test Logic App</title>

function TestLogicApp() {
try {
var xhr = new XMLHttpRequest();
var url = “Logic App URL“;
xhr.open(“POST”, url, true);
xhr.setRequestHeader(“Content-Type”, “application/json”);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
alert(xhr.responseText);
}
};
var data = JSON.stringify({
“firstname”: “Rajeev”,
“lastname”: “Pentyala”,
“Account”: {
“id”: “A16B3F4B-1BE7-E611-8101-E0071B6AF231”,
“Name”: “A Datum Corporation”
}
});
xhr.send(data);
} catch (e) {
alert(“Error – ” + e.description);
}
}

</head>
<body>
<input type=”button” value=”Post” onclick=”TestLogicApp()” />
</body>
</html>

  • Open the HTML page and click the ‘Post’ button to capture the Response returned from LogicApp

LogicApp_1

Troubleshoot and track the history:

  • To troubleshoot and track the Requests, from the ‘Logic App’, click on ‘Overview’ and check under ‘Run history’

LogicApp_15

🙂

Advertisements