Recently, I've got an requirement to send the vendor payments from D365FO to third party system. For that I have created custom service and exposed it to external application.
Steps to create custom service.
Step 1. Create a contract class for request.
Whenever third party system accessing our custom service they will provide invoice number as input. Based on the input will be sending the payment data against it.
Class CSVendTransRequestContract
{
private InvoiceID InvoiceId;
[DataMember("Invoice number")]
public InvoiceId parmInvoiceNumber(InvoiceId _value = InvoiceId)
{
InvoiceId = _value;
return InvoiceId;
}
}
Step 2. Create another contract class for Response.
Response for our CS would be below columns form response contract class.
[DataContractAttribute,Newtonsoft.Json.JsonObject(IsReference = false)]
class CSVendTransResponseContract
{
private Voucher paymentVoucher;
private Date Pdate;
private Amount amount,tdsAmount;
private str number;
private str paymentReference;
[DataMember("Payment voucher")]
public Voucher parmPaymentVoucher(Voucher _value = paymentVoucher)
{
paymentVoucher = _value;
return paymentVoucher;
}
[DataMember("Date")]
public date parmDate(date _value = Pdate)
{
Pdate = _value;
return Pdate;
}
[DataMember("Amount")]
public Amount parmAmount(Amount _value = amount)
{
amount = _value;
return Amount;
}
[DataMember("TDS Amount")]
public Amount parmTDSAmount(Amount _value = tdsAmount)
{
tdsAmount = _value;
return tdsAmount;
}
[DataMember("Payment reference")]
public Name parmPaymentReference(Name _value = paymentReference)
{
paymentReference = _value;
return paymentReference;
}
}
Step 3: Create a service class to run the business logic and return the response.
class CSVendTransService { [AifCollectionType('return', Types::Class, classStr(CSVendTransResponseContract))] public List SendResponse(CSVendTransRequestContract _invoices)
{ VendTrans vendTrans; VendSettlement vendSettlement; List resultSet = new List(Types::Class); while select SettleAmountMST,OffsetTransVoucher,TransDate from vendSettlement join PaymReference from vendTrans where vendTrans.recid == vendSettlement.TransRecId && vendTrans.AccountNum == vendSettlement.AccountNum && vendTrans.DataAreaId == vendSettlement.TransCompany && vendTrans.Invoice == _invoices.parmInvoiceNumber( && vendTrans.TransType == LedgerTransType::Purch { CSVendTransResponseContract responseContract = new CSVendTransResponseContract(); responseContract.parmPaymentVoucher(vendSettlement.OffsetTransVoucher); responseContract.parmAmount(vendSettlement.SettleAmountMST); responseContract.parmDate(vendSettlement.TransDate); responseContract.parmTDSAmount(VendTrans_W::findByVendTrans(vendTrans.RecId).TDSAmount_IN); responseContract.parmPaymentReference(LedgerJournalTrans::find(vendtransLoc.JournalNum,vendtransLoc.Voucher,false).PaymReference); resultSet.addEnd(responseContract); } } return resultSet; }
Step 4: Create a service and open its properties and assign the service class Name (CSVendTransService). Refer below image
Next step is to create service operation and give a name and assign method name(SendResponse) in properties as below image.
Right click on service group, create service and in its properties select the service created in step 4.
Refer below image.
Refer below image.
URL :
https://xxxxx.sandbox.operations.dynamics.com/api/services/service group name/service name/service operation name
Sample request JSON
{
_invoices :
{
"Invoice number" :"Test"
}
}
Note : If you want to send the list of invoices in request body. in request contract create parm method which returns list.
We have to create new contract class and parm method which returns list.
Sample JSON for List of invoices
{
_invoices : [
{
"Invoice number" :"Test"
}
]
}
[DataContractAttribute]
public class CSVendTransInvoicesListRequest
{
private List Invoices;
[DataMemberAttribute('Contract'),
DataCollection(Types::Class, classStr(CSVendTransResponseContract)),
AifCollectionTypeAttribute('_contractClass', Types::Class, classStr(CSVendTransResponseContract)),
AifCollectionTypeAttribute('return', Types::Class, classStr(CSVendTransResponseContract))]
public List parmInvoices(List _contractClass = Invoices)
{
Invoices = _contractClass;
return Invoices;
}
}
Retrieving the list values in service class method.
class CSVendTransService
{
[AifCollectionType('return', Types::Class, classStr(CSVendTransResponseContract))]
public List SendResponse(CSVendTransInvoicesListRequest _invoices)
{
list invoices = _invoices.Parminvoices();
listEnumerator enumerator = invoices.getEnumerator();
while(enumerator.MoveNext())
{
CSVendTransRequestContract contract = new CSVendTransRequestContract();
contract = enumerator.current();
// DO your operation
}
}
}
Keep Learning!!
No comments:
Post a Comment