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

  1. A service class
  2. A service 
  3. 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.



Service is listed in the list, specify the name of the service group and service name in the URL to access.


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:
  1. Create an application registration
  2. In this application portal create a secret
  3. Note client id and secret.

In D365FO:

Register azure application
  1. Register your client id as an azure application
  2. Connect it to a valid user
  3. 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
  1. New pane in Postman of type Get.
  2. Add the URL to your Authentication provider
    1. Typical : https://login.microsoftonline.com/[Your Tenant ID]/oauth2/token
  3. In the body, choose type of x-www-form-urlencoded.
    1. Resource: this is the URL to your D365FO environment
    2. client_id:the client Id of the application created in above step
    3. client_secret: the secret from the application you created in above step
    4. grant_type: this should be of value: client_credentials
  4. 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

  1. Create new pane in postman of type Post (from now refered to as Post).
  2. Add your environment URL pluss service path: 
    1. Example:https://[your environment URL]/api/services/TestServiceGroup/TestService
  3. In the authorization pane of your Post specify accordingly

    1. Type: OAuth 2.0
    2. Add Authorization data to: Request Headers.
  4. Paste inn your authorization token in the access token field.
  5. In the body part of your Post specify
    1. Type: Raw
    2. Paste in your JSON. In this example it is something like JSON below
    3. Press send.

{
   "data": [
{
"string1":"test string 1-1",
"string2":"test string 1-2";
}
{
"string1":"test string 2-1",
"string2":"test string 2-2";
}
]
}


Comments

Popular posts from this blog

Call a simple Logic app from X++

SysOperationFramework with use of Query

Retail Attributes on sales lines