Integration with Finance and Operations - From Basics (Part 2)

Introduction

Finance and Operations provides two major ways to interact with tables (or data entities) for external system using APIs; namely Custom Services and Data Entities.

Data entities in D365 Finance and Operations simplify data management by grouping data from multiple tables.
They make it easier to import, export, and integrate data with other systems.

Custom services in D365 Finance and Operations allow developers to create web services for specific business needs.
They enable external systems to interact with D365 F&O by exposing custom logic and operations.
This helps in integrating and automating processes with other applications.

In the previous blog, we saw how we can use Data Entities to create APIs.
In this blog, we'll see how we can use Custom Services to create APIs.

References

Custom Service Development
Exposing an X++ Class as a Data Contract

Pre-requisites

  • A Finance and Operations Environment
  • Postman connected to FnO Enviroment (If you want to do this follow the blog here)

Configuration

Right click on the project and click on "Add" and then "New Item"


Click on Services and select the "Service Group."

Add the appropriate name for your Service Group.
Do note that this will be a part of your endpoint url.

Once that is done, we'll need to create a new Service as well.
Repeat the same steps but this time select the "Service" object and add the appropriate name.


Once both the Service Group and Service objects are created, we'll need to create request, response and request processing objects.

For that, click on Right Click on Project > Add > New Item > Code > Class.

Add the appropriate name and click on "Add".



In the Request object, set the attribute [DataContract] at the class level and add Global variables which will be used to send data to the processing object.


In the Response object, set the attribute [DataContract] at the class level and add Global variables which will be used to return data from the processing object.



In the processing object, write the necessary logic.

Here, I'm writing the logic to pull the data from the request object into local variables and then create a Customer record along with an address entry for that customer and if everything is completed successfully, I'll return a "Success" status along with the customer Id else a "Failed" status along with the Customer ID.




If there is any logic for logging, that can be added to our processing class after the main operation has completed.
You can do that in the following way - 


Once this is done, we can now add our processing class to our Service object.

Open the "Service" object and set the "Class" field to the processing class you have created.


Right click on the Service object in the designer and click on "New Service Operation"


In the new Service Operation that is created, set the method from the processing class that you want to call in the "method" field.
Set the appropriate name for that method. (This will be part of the endpoint)
Set the operational domain, whether it will only work for a particular company or accross the companies.
Set the Access Level (Access level increases as you go down the list)


Now after this, we'll assign our Service object to the Service Group.

Open the Service Group in the designer, right click it and then click on "New Service"


 In the newly created "ServiceGroupService" entry set your "Service"


Then after rebuild, Sync database and deploy; open postman and add the following URL template.

<base_url>/api/services/<ServiceGroup>/<Service>/<Method>

Now, if I trigger the "Post" request, I'll get a "Success" status along with the CustomerId.
If I try to recreate the same customer, I'll get a "Failed" status along with the CustomerId.

If you are not sure whether your API exists or not then you can simply call a "Get" request on the URL - <base_url>/api/services


This returns a list of all the "Service Groups" present in the system.

We can then call a "Get" request including this "Service Group" into our URL.


This returns a list of all the "Services" present in the system for that "Service Group".

We can then call a "Get" request including this "Services" into our URL.


This returns a list of all the "Operations" present in the system for that "Service Group".

We can then call a "Get" request including this "Operation" into our URL.


This returns the Request and Response objects for this Service Operation.

Conclusion

Thus we saw how to create APIs using Custom Services in Finance and Operations.
In the next blog, we'll see some advanced API funtionalities that are present in Finance and Operations.

Comments

Popular posts from this blog

Using Notifications in Business Central via AL

Actionable Error Messages in Business Central

Configure Gmail SMTP service in Business Central without "Allow Less Secure" setting