wtorek, 20 stycznia 2015

How to export sales invoice to XML using AIF classes

Example how to export sales invoice to XML and read from XML to AX.
  
static void AIF_XMLExample(Args _args)
{
    XML                                     xml;
    CustInvoiceJour                         custInvoiceJour;
    Map                                     keyData;
    AifEntityKey                            aifEntityKey        = AifEntityKey::construct();
    AifEntityKeyList                        aifEntityKeyList;    
    SalesSalesInvoiceService                service             = SalesSalesInvoiceService::construct();
    SalesSalesInvoice                       salesInvoice;
    SalesSalesInvoice                       salesInvoice2;
    SalesSalesInvoice_CustInvoiceJour       custInvoiceJourClass;    
    AfStronglyTypedDataContainerList        strongContainerList;
    AfStronglyTypedDataContainer            strongContainer;    
    ;
    
    // example invoice
    select custInvoiceJour where custInvoiceJour.InvoiceId == 'CIV-000020';
        
    keyData = SysDictTable::getKeyData(custInvoiceJour);

    aifEntityKey.parmTableId( custInvoiceJour.TableId );
    aifEntityKey.parmRecId( custInvoiceJour.RecId );
    aifEntityKey.parmKeyDataMap( keyData );

    aifEntityKeyList = aifEntityKeyList::construct();
    aifEntityKeyList.addEntityKey(aifEntityKey);
            
    // save to XML
    salesInvoice = service.read(aifEntityKeyList);
    xml = salesInvoice.serialize();
    info('Sales invoice in XML :');
    info( xml );
    
    // read from XML
    salesInvoice2 = new SalesSalesInvoice();
    salesInvoice2.deserialize(xml);
    strongContainerList = salesInvoice2.parmCustInvoiceJour();    
    
    if (strongContainerList.get_Count() > 0)
    {
        custInvoiceJourClass = strongContainerList.get_Item(1);
         
        info(strFmt("From XML - Invoice Id = %1", custInvoiceJourClass.parmInvoiceId()));
        info(strFmt("From XML - Sales Id = %1", custInvoiceJourClass.parmSalesId()));                    
    }
}