Create Custom Services in D365 F&O and Test using Postman Part-1

Greetings,

I am pleased to present to you my latest blog post on creating a custom web service in Dynamics 365 Finance & Operations and accessing it through Postman. This tutorial will be divided into two parts, in which we will discuss the steps required to create and expose web services to external systems using Dynamics 365 Finance & Operations.

In the first part, we will delve into the Dynamics 365 Finance & Operations platform and discuss the steps required to create web services. As an example scenario, we will consider a situation where a third-party system needs to retrieve information about a specific vendor, such as their name, vendor group, and balance, from D365 for a given legal entity.

In the second part, we will introduce Postman and its usage in testing web services.

This approach serves as a starting point for integrating a third-party system with Dynamics 365 Finance & Operations.

Kindly Note: This is one of the approaches to achieve the above requirement.

So let's start...

Step 1: Create a new contract class to take the parameter from the third party.

[DataContract('VendorIDContract')]
class VendorIDContract
{
    VendAccount            vendorId;
    DataAreaId               legalEntity;
    [DataMemberAttribute('vendorId')]
    public VendAccount parmVendorId(VendAccount _vendorId = vendorId)
    {
        vendorId = _vendorId;
        return vendorId;
    }
 
    [DataMemberAttribute('legalEntity')]
    public VendAccount parmLegalEntity(DataAreaId _legalEntity = legalEntity)
    {
        legalEntity = _legalEntity;
        return legalEntity;
    }
}

Step 2: Create a new contract class that will return the Vendor data.

[DataContract('VendorMasterContract')]
class VendorMasterContract
{
    VendAccount                 vendorId;
    Name                             vendorName;
    VendGroupId                 vendorGroup;
    Amount                          vendorBalance;
    str                                   StatusMessage;
 
    [DataMemberAttribute('VendorId')]
    public VendAccount parmVendorId(VendAccount _vendorId = vendorId)
    {
        vendorId = _vendorId;
        return vendorId;
    }
 
    [DataMemberAttribute('VendorName')]
    public Name parmVendorName(Name _vendorName = vendorName)
    {
        vendorName = _vendorName;
        return vendorName;
    }
 
    [DataMemberAttribute('VendorGroup')]
    public VendGroupId parmVendorGroup(VendGroupId _vendorGroup = vendorGroup)
    {
        vendorGroup = _vendorGroup;
        return vendorGroup;
    }
 
    [DataMemberAttribute('VendorBalance')]
    public Amount parmVendorBalance(Amount _vendorBalance = vendorBalance)
    {
        vendorBalance = _vendorBalance;
        return vendorBalance;
    }
 
    [DataMemberAttribute('StatusMessage')]
    public str parmStatusMessage(str _StatusMessage = StatusMessage)
    {
        StatusMessage = _StatusMessage;
        return StatusMessage;
    }
}

Step 3: Create a Service Operation class and add the business logic.

class VendorService
{
    [AifCollectionTypeAttribute('_keys', Types::Class, classStr(VendorIDContract))]
    public VendorMasterContract getVendorBalance(List _keys)
    {
        VendorMasterContract            vendorMaster;
        VendorIDContract                   vendorId;
        ListEnumerator                       listEnumerator = _keys.getEnumerator();
        VendTable                               vendTable;
        VendTrans                               vendTrans;
        DataAreaId                             legalEntity;
 
        vendorMaster = new VendorMasterContract();
        listEnumerator.moveNext();
        vendorId = listEnumerator.current();
        legalEntity = vendorId.parmLegalEntity();
 
        if(legalEntity)
        {
            changecompany(legalEntity)
            {
                select vendTable where vendTable.AccountNum == vendorId.parmVendorId();
                if(vendTable)
                {
                    select sum(AmountMST) from vendTrans where vendTrans.AccountNum == vendTable.AccountNum;
 
                    vendorMaster.parmVendorId(vendTable.AccountNum);
                    vendorMaster.parmVendorName(vendTable.name());
                    vendorMaster.parmVendorGroup(vendTable.VendGroup);
                    vendorMaster.parmVendorBalance(vendTrans.AmountMST);
                    vendorMaster.parmStatusMessage("Vendor found");
                }
                else
                {
                    vendorMaster.parmStatusMessage("Vendor not found");
                }
            }
        }
        else
        {
            vendorMaster.parmStatusMessage("Invalid Legal Entity");
        }
        return vendorMaster;
    }
}

Step 4: Create a New Service.

Right Click on the Project and click on the “Add” à “New Item” under Services à Service. Give a logical name in my case its “DemoVendorAPI

DemoVendorAPI

Change the below properties of the Service and add your method.

AddMedthod to the service

Step 5: Create a Service Group.

Right Click on the Project and click on the “Add” à “New Item” under Services à Service. Give a logical name in my case its “DemoVendorAPIGroup

Set “Auto Deploy” as “Yes” and add the Service to the Service Group.

So your final projects should have the below objects. Build your project.

Testing your Service on the Web browser

URL: https://{Your website name}/api/services/{Service-Group}/{Service}/{Method}

Example: https://mydemoEnvironment.axcloud.dynamics.com/api/services/DemoVendorAPIGroup/DemoVendorAPI/getVendorBalance

If you get below schema, then your service is good to go.

Please Visit below link for Part 2

https://vikasaxaptandd365fno.blogspot.com/2022/12/create-cusotm-services-in-d365-f-and_21.html



Comments

Popular posts from this blog

SSRS Report error "The parameter panel layout for this report contains more parameters than total cells available D365" Finance and Operation

Simplifying the Connection of Your Dev VM to Tier 2 Environment in D365D

Increase Your Storage in Azure Dev VM to fix disk size issue while D365 F&O DB restore.