Monday, November 29, 2021

Create a New custom service in D365FO

 Hi guys, Today we see how to create a custom service in D365FO using X++.


In custom service, we have Inbound and Outbound services.

Inbound service: Get the data from 3rd party application and do some operations in D365Fo.

Outbound service: Send the data from D365Fo to 3rd party application.

components required:

  • Service class
  • Business logic class (Instead of writing the logic in the service class I created a new class)
  • Contract class
  • Service object
  • Service group object
Link : Url/api/services/serviceGpName/serviceName/OpertaionName.

  • OpertaionName(method name) is exposed to the outside systems.
  • HTTP web request. (Java client/Postman/dot net-based application)
  • We can call this API from any App.
  • 3rd party application will send data to D365FO and D365FO will store the data and send a response "Success" or "failed" based on business operation.
  • I took a simple example. I send the customer data to D365FO If the customer exists it will update the customer name otherwise it will insert the data in D365FO. I have used a custom table.


1. Create a contract class and parm methods.

[DataContractAttribute]
class TestCustomServiceDataContract
{
    str customer, name;

    public TestCustomServiceDataContract construct()
    {
        return new TestCustomServiceDataContract();
    }

    [DataMemberAttribute('Customer account')]
    public str parmCustomer(str _customer = customer)
    {
        customer = _customer;

        return customer;
    }

    [DataMemberAttribute('Customer name')]
    public str parmName(str _name = name)
    {
        name = _name;

        return name;
    }

}

2. Create service class and method is in public.

class TestCustomServiceDataService
{
    public str customers(TestCustomServiceDataContract     _contract)
    {
        return TestCustomServiceBussinessLogic::getCustomer(_contract);
    }
} 

3. Bussiness logic class.

class TestCustomServiceBussinessLogic
{
    public static str getCustomer(TestCustomServiceDataContract     _contract)
    {        
        TestCustomers       customer = TestCustomers::find(_contract.parmCustomer(), true);

        if (customer)
        {
            ttsbegin;
            customer.Name = _contract.parmName();
            customer.update();
            ttscommit;

            return 'Success';
        }
        else
        {
            customer.clear();
            customer.Customer   = _contract.parmCustomer();
            customer.Name       = _contract.parmCustomer() + ' ' + _contract.parmName();
            customer.insert();

            return 'Success';
        }

        return 'Failed';
    }

}

4. Create Service.
        




5. Create a Service Group.



Link : Url/api/services/serviceGpName/serviceName/OpertaionName

Url/api/services/TestCustomServiceGroup/TestCustomService/customers

Use the above link in postman.


Remove custom service default parameter: id in D365FO:


Write the below line on top of the contract to remove that "Id".
[DataContractAttribute, Newtonsoft.Json.JsonObjectAttribute(IsReference = false)]

Multiple records:

{ "record": [ { "ID": "140127-000003", "EffectiveDate": "2022-06-05", "IsTrue": "Yes", "DueDate": "2022-06-05T13:49:51.141Z" } ] }

We need to build a method like the below in the service class:

[AifCollectionType('record', Types::Class, classStr(TestDataContract))] [AifCollectionType('return', Types::Class, classStr(Test2DataContract))] public List updateStockOnHand(List record) { }

For the above example code is written in the below link. Please check.



Keep daxing!!


No comments:

Post a Comment