Lets make a service
Custom services in D365 is a nice way to integrate with D365. In this post I will show how one can set up to receive JSON data and store it in a Table.
To create a service you need these elements to make it operational
class TestService
{
public System.Object testService(Newtonsoft.Json.Linq.JArray _data)
{
TestTable testTable;
int counter;
System.String netString = '';
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(stringWriter);
counter = this.processData(_data);
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName('processed lines');
jsonWriter.WriteValue(counter);
jsonWriter.WritePropertyName('company');
jsonWriter.WriteValue(curExt());
jsonWriter.WriteEndObject();
jsonWriter.Close();
return Newtonsoft.Json.JsonConvert::DeserializeObject(stringWriter.ToString());
}
private int processData(Newtonsoft.Json.Linq.JArray data)
{
int counter = 0;
System.Collections.IEnumerator enumerator;
Newtonsoft.Json.Linq.JObject line;
TestTable testTable;
#define.String1('string1')
#define.String2('string2')
enumerator = _data.GetEnumerator();
ttsbegin;
while(enumerator.MoveNext())
{
line = enumerator.get_Current();
testTable.clear();
testTable.initValue();
testTable.String1 = line.GetValue(#String1).ToString();
testTable.String1 = line.GetValue(#String2).ToString();
testTable.insert();
counter++;
}
ttscommit;
return counter;
}
public str sayHelloToTheWorld()
{
return 'Hello world';
}
}
Service is listed in the list, specify the name of the service group and service name in the URL to access.
To create a service you need these elements to make it operational
- A service class
- A service
- A service group
1. Create your service class.
I created this simple service class that takes JSON as a parameter and inserts the data into a table.
class TestService
{
public System.Object testService(Newtonsoft.Json.Linq.JArray _data)
{
TestTable testTable;
int counter;
System.String netString = '';
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(stringWriter);
counter = this.processData(_data);
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName('processed lines');
jsonWriter.WriteValue(counter);
jsonWriter.WritePropertyName('company');
jsonWriter.WriteValue(curExt());
jsonWriter.WriteEndObject();
jsonWriter.Close();
return Newtonsoft.Json.JsonConvert::DeserializeObject(stringWriter.ToString());
}
private int processData(Newtonsoft.Json.Linq.JArray data)
{
int counter = 0;
System.Collections.IEnumerator enumerator;
Newtonsoft.Json.Linq.JObject line;
TestTable testTable;
#define.String1('string1')
#define.String2('string2')
enumerator = _data.GetEnumerator();
ttsbegin;
while(enumerator.MoveNext())
{
line = enumerator.get_Current();
testTable.clear();
testTable.initValue();
testTable.String1 = line.GetValue(#String1).ToString();
testTable.String1 = line.GetValue(#String2).ToString();
testTable.insert();
counter++;
}
ttscommit;
return counter;
}
public str sayHelloToTheWorld()
{
return 'Hello world';
}
}
2. Create service
Create a service an set the parameters to the class you just created. Add the service operations you want to be available, these are the methods specified within the class.
3. Create a service group
Add your service to a service group.
Now compile and synchronize to make it available from your devbox. Verify that you can view your newly created service by browsing it, see next point for how to.
List services available.
You can navigate your services by adding /api/services/ to your URL, you can further specify the name and method call from the.Now to test it with PostMan
Set up
To test it with postman there are essential setups in D365FO and Azure portal that needs to be set up. Below is a short version on how to divided into Azure portal and D365.
In Azure portal:
- Create an application registration
- In this application portal create a secret
- Note client id and secret.
In D365FO:
Register azure application
- Register your client id as an azure application
- Connect it to a valid user
- Open the chosen user and set the default company. This company will be the operating company of the service.
Register the authentication provider:
Run test in postman
To run service in postman you need two things, fetch an access token and post data to service
Get token
See below for actual example
- New pane in Postman of type Get.
- Add the URL to your Authentication provider
- Typical : https://login.microsoftonline.com/[Your Tenant ID]/oauth2/token
- In the body, choose type of x-www-form-urlencoded.
- Resource: this is the URL to your D365FO environment
- client_id:the client Id of the application created in above step
- client_secret: the secret from the application you created in above step
- grant_type: this should be of value: client_credentials
- Press the send button and your response should give you a long string that is your token, copy the long string, this is your validated access into the environment, valid for 30 minutes.
Copy the text that is within the quotes of value access_token.
Run the postman test
- Create new pane in postman of type Post (from now refered to as Post).
- Add your environment URL pluss service path:
- Example:https://[your environment URL]/api/services/TestServiceGroup/TestService
- In the authorization pane of your Post specify accordingly
- Type: OAuth 2.0
- Add Authorization data to: Request Headers.
- Paste inn your authorization token in the access token field.
- In the body part of your Post specify
- Type: Raw
- Paste in your JSON. In this example it is something like JSON below
- Press send.
{
"data": [
{
"string1":"test string 1-1",
"string2":"test string 1-2";
}
{
"string1":"test string 2-1",
"string2":"test string 2-2";
}
]
}
Comments
Post a Comment