Hi Folks,
I am writing this blog to create custom web services in D365 FO. It is same like previous version 2012. All you need to do is to create a class and services. Follow the below process to create a custom web service.
Step 1 // Create a contract class for parameters
[DataContractAttribute('GSCustContract')]
class GSCustContract
{
AccountNum custNum;
[DataMemberAttribute('custNum')]
public AccountNum parmCustNum(AccountNum _custNum = custNum)
{
custNum = _custNum;
return custNum;
}
[DataMemberAttribute('AmountPayable')]
public str parmAmountPayable(str _amountPayable = amountPayable)
{
amountPayable = _amountPayable;
return amountPayable;
}
[DataMemberAttribute('LastMonthPaid')]
public str parmLastMonthPaid(str _lastMonthPaid = lastMonthPaid)
{
lastMonthPaid = _lastMonthPaid;
return lastMonthPaid;
}
}
Step 2:
// Create a service class for business logic
class GSCustServiceClass
{
[AifCollectionTypeAttribute('return', Types::Class, classStr(GSCustContract)), SysEntryPointAttribute(true)]
public List customerInfo(CustNo _custNo, DataAreaId _dataAreaId)
{
List result;
GSCustContract contract;
CustTable custTable;
AmountMST getBalance()
{
CustTransOpen custTrans;
select sum(AmountMST) from custTrans
where custTrans.AccountNum == custTable.AccountNum;
if (custTrans.AmountMST < 0)
{
return 0;
}
else
{
return abs(custTrans.AmountMST);
}
}
str getLastPaymMonth()
{
CustTransOpen custTrans;
select firstonly AccountNum, TCMBillingType, TransDate from custTrans
where custTrans.AccountNum == custTable.AccountNum;
return subStr(date2Str(custTrans.TransDate, 123, 2, 0, 2, 0, 2), 3, 6);
}
changecompany(_dataAreaId)
{
result = new List(Types::Class);
custTable = CustTable::find(_custNo);
contract = new GSCustContract();
contract.parmCustNum(_custNo);
contract.parmAmountPayable(strReplace(num2Str(getBalance()*100, 10, 0, 0,0), " ", "0"));
contract.parmLastMonthPaid(getLastPaymMonth());
}
result.addEnd(contract);
}
return result;
}
Step 3: Now you need to create a element of service type

go to properties and select the service class which we have created in the above step.
Right click the service operations node and click on new service operations button, go to properties of the service operation node and select the method name.
Step 4: Create a service group element.
add a service to the group and select the service name (created in step 3) in the property of the service node.
Step 5: Build your project
Now your web service is ready to use. use below urls for respective services
<D365FO URL>/api/services/<Service group name>/<Service Name>/<Service operation name>
<D365FO URL>/soap/services/<Service group name>?wsdl