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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Create an invoice (CustInvoiceJour) with AIF Services

(0) ShareShare
ReportReport
Posted on by

Hello,

I'm trying to create an invoice in the Dynamics AX database, using AIF services from a c# application.

I'm following this tutorial :

dev.goshoom.net/.../sales-orders-via-aif

with one obvious change :

- link shows how to create orders, for invoices I use a custom service based on a query using tables "CustInvoiceJour" and "CustInvoiceTrans"

It seems to work (no exception, after few attemps struggling with mandatory fields), but nothing happens : there is no row in tables "CustInvoiceJour" or "CustInvoiceTrans"

Can someone show me what I'm doing wrong ?

Or maybe it's simply not possible to create invoices from AIF services ? (if not, which is the best way to do that from a c# application ?)

Regards,
Charles


NB :

you will find below :

- the c# code

- the XML data (request and respond)


// header
var header = new AxdEntity_CustInvoiceJour();
header.OrderAccount = "[...]";
header.InvoiceAccount = "[...]";
header.SalesId = "[...]";
header.InvoiceDate = DateTime.Now.Date;
header.InvoiceId = "[...]";
header.CustomerRef = "[...]";
header.CurrencyCode = "[...]";
header.CustGroup = "[...]";
header.LanguageId = "[...]";

// line
var line = new AxdEntity_CustInvoiceTrans();
line.InventTransId = "[...]";
line.Qty = 3;
line.ItemId = "[...]";
line.ItemCodeId = null;
line.InvoiceId = "[...]";
line.InvoiceDate = DateTime.Now.Date;

// set lines on header
header.CustInvoiceTrans = new AxdEntity_CustInvoiceTrans[]
{
    line
};

// set invoices list
var invList = new AxdCustomInvoice()
{
    CustInvoiceJour = new AxdEntity_CustInvoiceJour[]
    {
        header
    }
};

// context
var callContext = new CallContext() { Company = "our_company" };

// create
var key = client.create(callContext, invList);

// no exceptions here but nothing in the database
// value of "key" is :
// KeyData(){ Field = "RecId", Value = "0" }

Xml Data request :

<?xml version="1.0" encoding="UTF-8" ?>
- <MyCustomInvoiceServiceCreateRequest xmlns="http://tempuri.org">
    - <MyCustomInvoice xmlns="schemas.microsoft.com/.../MyCustomInvoice">
      <ClearNilFieldsOnUpdate>true</ClearNilFieldsOnUpdate>
      <DocPurpose>Original</DocPurpose>
      <SenderId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
        - <CustInvoiceJour class="entity">
              <_DocumentHash xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <BankLCExportLine xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <BillOfLadingId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <CashDiscCode xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <ContactPersonId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <CurrencyCode>[...]</CurrencyCode>
              <CustGroup>[...]</CustGroup>
              <CustomerRef>[...]</CustomerRef>
              <DeliveryName xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <DirectDebitMandate xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <DlvMode xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <DlvTerm xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <DocumentNum xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <DueDateTimeEinvoice xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <EInvoiceAccountCode xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <EnterpriseNumber xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <EUSalesList xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <InterCompanyCompanyId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <InterCompanyPurchId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <IntrastatDispatch xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <InventLocationId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <InvoiceAccount>[...]</InvoiceAccount>
              <InvoiceDate>[...]</InvoiceDate>
              <InvoiceId>[...]</InvoiceId>
              <InvoicingName xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <LanguageId>[...]</LanguageId>
              <LedgerVoucher xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <LineSpecEinvoice xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <Log xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <MCREmail xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <NumberSequenceGroup xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <OrderAccount>[...]</OrderAccount>
              <ParmId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <PaymDayId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <Payment xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <PaymentId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <PaymentSched xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <PaymId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <PostingProfile xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <PrintMgmtSiteId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <PurchaseOrder xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <RCSalesList_UK xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <ReturnItemNum xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <ReturnReasonCodeId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <SalesId>[...]</SalesId>
              <SalesOriginId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <TaxGroup xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <TaxInvoiceSalesId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <TaxItemGroup xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <TaxTypeCodeEinvoice xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <TypeCodeEinvoice xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <VATNum xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              <WorkerSalesTaker xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
            - <CustInvoiceTrans class="entity">
                  <AssetBookId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <AssetId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <BillingCode xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <CountryRegionOfShipment xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <CurrencyCode xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <EInvoiceAccountCode xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <ExternalItemId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <InterCompanyInventTransId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <IntrastatDispatchId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <InventDimId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <InventRefId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <InventRefTransId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <InventTransId>[...]</InventTransId>
                  <InvoiceDate>[...]</InvoiceDate>
                  <InvoiceId>[...]</InvoiceId>
                  <ItemCodeId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <ItemId>[...]</ItemId>
                  <ItemName xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <LedgerDimension xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <LineHeader xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <MCRDeliveryName xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <MCRDlvMode xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <Name xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <NumberSequenceGroup xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <OrigCountryRegionId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <OrigSalesId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <OrigState xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <Port xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <RetailCategory xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <ReturnDispositionCodeId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <SalesCategory xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <SalesGroup xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <SalesId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <SalesUnit xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <StatProcId xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <TaxGroup xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <TaxItemGroup xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <TaxWithholdGroup_TH xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <TaxWithholdItemGroupHeading_TH xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <TaxWriteCode xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <TransactionCode xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
                  <Transport xsi:nil="true" xmlns:xsi="www.w3.org/.../XMLSchema-instance" />
              </CustInvoiceTrans>
          </CustInvoiceJour>
      </MyCustomInvoice>
  </MyCustomInvoiceServiceCreateRequest>

 

Xml Data response :

 

 <?xml version="1.0" encoding="UTF-8" ?>
- <MyCustomInvoiceServiceCreateResponse xmlns="http://tempuri.org">
    - <EntityKeyList xmlns="schemas.microsoft.com/.../EntityKeyList">
        - <EntityKey xmlns="schemas.microsoft.com/.../EntityKey">
            - <KeyData>
                - <KeyField>
                      <Field>RecId</Field>
                      <Value>0</Value>
                  </KeyField>
              </KeyData>
          </EntityKey>
      </EntityKeyList>
  </MyCustomInvoiceServiceCreateResponse>

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    Trying to write directly to these tables is a terrible idea - you would completely skip the crucial thing: the invoicing itself! It does much more than just creating CustInvoice* records: it updates accounting, inventory transactions, order status and so on.

    You have to rethink what you're doing. Don't try inserting CustInvoice* records at all. Instead, call SalesFormLetter framework to invoice given order(s).

  • Suggested answer
    5400 Profile Picture
    7,162 on at

    See CustInvoiceJour and custInvoicetrans will get populate during posting the SO invoice. I don't think so populating those table and posting invoice is good idea. Can you tell me what is your data scope. If you want to post SO invoice you can recall the Sales order service and populate the sales order and call the SalesFormLetter to post the invoice.  

  • Sohaib Cheema Profile Picture
    49,438 User Group Leader on at

    in addition to above what has been specified as suggestion, you may also look to use Free Text Invoice Service, if you don't want to use items and full sales order process. that will ultimately hit you CutsInvoice* Tables

    Note: it depends upon your accurate requirement that what you is your requirement e.g. if you want to use inventory or not

  • Community Member Profile Picture
    on at

    Thanks everyone for your replies.

    I guess I knew what I was doing here was a long shot, my knowledge of Dynamics is close to zero !

    I have habits from simpler ERP, where basically there is a method "create_invoice( order_to_invoice_args )", and it's over : things looks a bit complicated here.

    I guess I cannot use free texte invoice because there is in fact an inventory management, and our client expects to see invoices we'll make at the same place (in dynamics interface) than invoices he already have (a piece of context here : our client have both online store and physical stores, we have to create invoices from the online store).

    I'm gonna look in details the classe SalesFormLetter now, thanks for your help.

  • Suggested answer
    Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    What you say you expected is exactly what you should have done - and you didn't. You didn't call any method to invoice an order; instead, you tried to insert some data directly into AX database, ignoring all related business logic.

    The equivalent of create_invoice( order_to_invoice_args ) is SalesFormLetter::construct(DocumentStatus::Invoice).update(salesTable). It isn't much different, is it?

    (Please note that there are many parameters that you may want to change.)

  • Community Member Profile Picture
    on at

    Agreed. But there's something who might be obvious for you, but who wasn't for me :

    I had hopes that the method "create()" of salesinvoice service implied some business logic ! And I didn't have knowledge of the classe "SalesFromLetter" at the time...

    Anyway, I have found severals sample of code who looks like this : blogs.bojensen.eu/.../posting-salesorder-confirmation-with-salesformletter-class-in-ax

    I think it's a good way to start.

  • Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    You created an AIF document service based on a query. Then you filled data CustInvoiceJour and CustInvoiceTrans and asked AX to create these records in AX database.

    If you wanted to call some business logic when creating records, you would have to write some code for it. The wizard generating classes based on your query obviously can't know that you want to run some extra business logic there. (Nevertheless because CustInvoice* records are the result of invoicing, not the starting point, you shouldn't try to do anything like that anyway.)

  • Community Member Profile Picture
    on at

    hi martin

    how can i use form letter service in c# project to create invoice for aif created sales order

  • Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    If you're fine with using a custom service, you may be able to utilize postSalesOrderInvoice operation of FormletterService.

  • Community Member Profile Picture
    on at

    Thanks for your reply

    1- How should i callPostSalesOrderInvoice in Visual c# Application.
    2- the operation must be utilized or i can just call it and send parameters like salesId?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans