web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / i-am-ax-pankaj / Developing and using Custom...

Developing and using Custom Services in D365

I AM AX PANKAJ Profile Picture I AM AX PANKAJ 419

In this article, we are going to learn how to build a custom service in d365 and how to use It


Introduction

So what is a custom service?

It is a feature provided by D365 F&O to expose its functionality outside of the environment to facilitate users using it on other platforms (mobile apps and web apps) to meet their required business process requirements

How it works

A custom service deployed in d365 acts the same as an API does

In D365, a custom service is deployed on the following two endpoints at the same

JSON endpoint

SOAP endpoint

Let’s begin

STEP 1: Request Contract

Firstly, we need a contract class for request deserialization

A sample contract class is here

[DataContract]
public class PurchaseOrderRequest
{
    PurchId purchId;
    DataAreaId dataAreaId;

    [DataMember]
    public PurchId parmPurchId(PurchId _purchId)
    {
        purchId = _purchId;
        return purchId;
    }

    [DataMember]
    public DataAreaId parmDataAreaId(DataAreaId _dataAreaId)
    {
        dataAreaId = _dataAreaId;
        return dataAreaId;
    }
}

If your request has fewer parameters then you can continue by skipping the contract request class

STEP 1: Response Contract

Then, we need the response contract class for response deserialization

Sample response contract class is here

[DataContract]
public class PurchaseOrderResponse
{
    VendAccount vendAccount;
    VendName vendName;
    DlvDate   dlvDate;
    PurchStatus status;
    PurchaseType purchaseType;
  
    [DataMember]
    public VendAccount parmVendAccount(VendAccount _vendAccount)
    {
        vendAccount = _vendAccount;
        return vendAccount;
    }
    [DataMember]
    public VendName parmVendName(VendName _vendName)
    {
        vendName = _vendName;
        return vendName;
    }
    [DataMember]
    public DlvDate parmDlvDate(DlvDate   _dlvDate)
    {
        dlvDate = _dlvDate;
        return dlvDate;
    }
    [DataMember]
    public PurchStatus parmStatus(PurchStatus _status)
    {
        status = _status;
        return status;
    }
    [DataMember]
    public PurchaseType parmPurchaseType(PurchaseType _purchaseType)
    {
        purchaseType = _purchaseType;
        return purchaseType;
    }
   
}

If your request has fewer parameters then you can continue by skipping the contract request class

STEP 1: API Class (The Service Class)

Lastly, we have to develop a service class to participate in API operations

The sample service class is here

public class PurchaseOrderInfo
{
    
 
    public PurchaseOrderResponse getPurchaseOrderData(PurchaseOrderRequest purchaseOrder)
    {
        PurchaseOrderResponse response;
        PurchTable purchTable;
        ;
        changecompany(purchaseOrder.parmDataAreaId())
        {
           purchTable = PurchTable::find(purchaseOrder.parmPurchId());
           response = new PurchaseOrderResponse();
           response.parmVendAccount(purchTable.OrderAccount);
           response.parmVendName(purchTable.PurchName);
           response.parmDlvDate(purchTable.DeliveryDate);
           response.parmStatus(purchTable.PurchStatus);
           response.parmPurchaseType(purchTable.PurchaseType);
        }
        return response;
     }
    
   
}

STEP 1: Service

Create a service element, assign your service class to it, and select the name

add new service operation and select a related method from the service class as below

Press enter or click to view image in full size

STEP 1: Service Group

Create a service group and add your custom service to the service group

Press enter or click to view image in full size

Build and sync your project

You can view your service at both JSON and SOAP endpoints

for JSON endpoint

https://usnconeboxax1aos.cloud.onebox.dynamics.com/api/services/PurchaseOrders/PurchaseOrderInfo

for SOAP endpoint

https://usnconeboxax1aos.cloud.onebox.dynamics.com/services/PurchaseOrders

STEP 1: Testing

Let's test our JSON endpoint

Authentication

To consume our custom service, we need to register an app on the Azure portal

After that, send a request with the POST request method via Postman as below

https://login.microsoftonline.com/your_tenant_id/oauth2/token

In the request, the body selects the form-data pattern and specifies the below parameters

  1. grant_type >> client_credentials
  2. client_id >> your_azure_app_id
  3. client_secret >> your_azure- app-secret
  4. resource >> your_d365_environment_url (https://usnconeboxax1aos.cloud.onebox.dynamics.com)

Send the request It will give the access token in response Copy it or save it to a variable

Press enter or click to view image in full size

Consume the API

Let’s continue with testing

Send a request with the POST Method following the below pattern

https://usnconeboxax1aos.cloud.onebox.dynamics.com/api/services/your_service_group/PurchaseOrderInfo/getPurchaseOrderData

In the Authentication tab, select type Bearer and provide the access token

In the request body select JSON format and provide the data according to your parameter contract class (if any) like below


Note down the response


in the next article, we will learn how to consume our custom service on multiple platforms (web, mobile, desktop) stay tuned

follow for more


source

Comments