Example C# code to call AIF Document Service create
As part of AIF Document Service related examples, here you can find an example C# code to call AIF Document Service. I would like to highlight few things in the example.
- Have a look at the error handling. This ‘catch’ will display the real errors from AX even if error logging is off on the port.
- Second is that for some fields it is not enough to specifie the value, but you also need to tell it was specified. like here: _InfoLine.Due = DateTime.Today; _InfoLine.DueSpecified = true;
- Example for getting AX enum values: _InfoLine.AccountType = JADSCONPOInvoiceInServiceReference.AxdEnum_LedgerJournalACType.Vend;
So here is the whole code example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JADSCONPOVendInvoice
{
class Program
{
static void Main(string[] args)
{
try
{
JADSCONPOInvoiceInServiceReference.AxdEntity_JADVendNPOInvTable _InfoTable = new JADSCONPOInvoiceInServiceReference.AxdEntity_JADVendNPOInvTable();
JADSCONPOInvoiceInServiceReference.AxdEntity_JADVendNPOInvLine _InfoLine = new JADSCONPOInvoiceInServiceReference.AxdEntity_JADVendNPOInvLine();
JADSCONPOInvoiceInServiceReference.AxdJADSCOVendNPOInvoice _Invoice = new JADSCONPOInvoiceInServiceReference.AxdJADSCOVendNPOInvoice();
_Invoice.JADVendNPOInvTable = new JADSCONPOInvoiceInServiceReference.AxdEntity_JADVendNPOInvTable[1] { _InfoTable };
_InfoTable.TransDate = DateTime.Today;
_InfoTable.DocumentDate = new DateTime(2017, 8, 10);
_InfoTable.Description = “Document Description”;
_InfoTable.JournalName = “INJ”;
_InfoTable.JournalNum = “5”;
_InfoTable.SCOCaseId = 1000000;
_InfoTable.TransDateSpecified = true;
_InfoTable.DocumentDateSpecified = true;
_InfoTable.SCOCaseIdSpecified = true;
_InfoTable.JADVendNPOInvLine = new JADSCONPOInvoiceInServiceReference.AxdEntity_JADVendNPOInvLine[1] { _InfoLine };
_InfoLine.AccountType = JADSCONPOInvoiceInServiceReference.AxdEnum_LedgerJournalACType.Vend;
_InfoLine.DimAccountNum = “V000001”;
_InfoLine.SupplierName = “Supplier name”;
_InfoLine.DocumentNum = “Document number”;
_InfoLine.AmountCurCredit = 5000;
_InfoLine.AmountCurDebit = 0;
_InfoLine.TaxAmount = 100;
_InfoLine.GrossAmount = 5100;
_InfoLine.Approved = JADSCONPOInvoiceInServiceReference.AxdExtType_Approved.Yes;
_InfoLine.TaxCode = “TaxGroup1_TAXItemGroup1”;
_InfoLine.DimCostCenter = “0001”;
_InfoLine.DimEmployee = “00000001”;
_InfoLine.DimPurpose = “Purpose1;
_InfoLine.DimSite = “Site1”;
_InfoLine.Due = DateTime.Today;
_InfoLine.Invoice = “LineLevelInvoiceId1”;
_InfoLine.Payment = “Payment1Id”;
_InfoLine.VATNumJournal = “VATNum1”;
_InfoLine.AccountTypeSpecified = true;
_InfoLine.AmountCurCreditSpecified = true;
_InfoLine.AmountCurDebitSpecified = true;
_InfoLine.TaxAmountSpecified = true;
_InfoLine.GrossAmountSpecified = true;
_InfoLine.ApprovedSpecified = true;
_InfoLine.DueSpecified = true;
JADSCONPOInvoiceInServiceReference.JADSCOVendNPOInvoiceServiceClient _Client = new JADSCONPOInvoiceInServiceReference.JADSCOVendNPOInvoiceServiceClient(/*_serviceEndpointName*/);
JADSCONPOInvoiceInServiceReference.CallContext _callContext = new JADSCONPOInvoiceInServiceReference.CallContext();
_callContext.Company = “CMP1”;
JADSCONPOInvoiceInServiceReference.EntityKey[] entityKeys = _Client.create(_callContext, _Invoice);
}
catch (System.ServiceModel.FaultException<JADSCONPOInvoiceInServiceReference.AifFault> aifFaults)
{
JADSCONPOInvoiceInServiceReference.InfologMessage[] infologMessageList = aifFaults.Detail.InfologMessageList;
foreach (JADSCONPOInvoiceInServiceReference.InfologMessage infologMessage in infologMessageList)
{
Console.WriteLine(“Exception: ” + infologMessage.Message + “\n”);
}
Console.WriteLine(“\nPress any key to quit.\n”);
Console.ReadKey();
}
}
}
}