Create General Journal in AX 2012 from AX 2009 using X++ and webservices

Recently i came across a requirement where i need to create a daily general journal entry in AX 2012 for the data entered on custom screen in AX 2009. To achieve this, we have different options. Initially we thought of making use of AIF and XML files but we want to see the journal created with immediate effect.

          So decided to consume the AX2012 AIF webservice WSDL URI directly in AX 2009 using X++ code. So below are the steps we followed.

  1. Create the Inbound service port for Ledger general journal service. Use the service operation create from LedgerGeneralJournalService. It is advisable to enable the include exceptions in fault flag to get the error message under troubleshoot fast tab. Activate the port.
  2. Copy the dll file created for inbound port from the AOS server (Typically the folder path would be - C:\Program Files\Microsoft Dynamics AX\60\Server\<your server>\bin\XppIL\AppShare\ServiceGeneration\<YourAIFPortName.dll>) to the AX 2009 AOS server Bin folder.
  3. Copy the WSDL URI from the inbound port created above in AX 2012.
  4. Create a new service reference in AX 2009 from File -> Tools -> Development -> AIF -> Add service reference. Paste the WSDL URI and fill other details to create the service refence.
  5. If you open the AOT under reference section you can see the new reference we created above.
  6. Now we have start using the service reference in our AX 2009 X++ code. For example i gave my service reference name LedgerJournalDAX12. Below is the code
  7. Make sure you create the code under a class and set the run on property - on server , otherwise the client of AX2012 will not be invoked and runtime error will be thrown.

LedgerJournalDAX12.CallContext                      context;
    str                                                 journalNum;
    LedgerJournalDAX12.Client                           clientAX2012;
    LedgerJournalDAX12.AxdLedgerGeneralJournal          journal;

    LedgerJournalDAX12.AxdEntity_LedgerJournalTable[]   header;
    LedgerJournalDAX12.AxdEntity_LedgerJournalTrans[]   lines;

    LedgerJournalDAX12.AxdEntity_LedgerJournalTable     ledgerJournalTable;
LedgerJournalDAX12.AxdEntity_LedgerJournalTrans     ledgerJournalTrans;

InteropPermission                                   permission = new InteropPermission(InteropKind::ClrInterop);
;

permission.assert();

    clientAX2012 = new LedgerJournalDAX12.Client();
    context = new LedgerJournalDAX12.CallContext();

    context.set_Company("your companyid");

    journal = new LedgerJournalDAX12.AxdLedgerGeneralJournal();

    header = new LedgerJournalDAX12.AxdEntity_LedgerJournalTable[1] ();
    ledgerJournalTable = new LedgerJournalDAX12.AxdEntity_LedgerJournalTable();

ledgerJournalTable.set_JournalName("yourJournalName");
    ledgerJournalTable.set_Name("LG from AX 2009");
try
{
ttsbegin;

lines =  new LedgerJournalDAX12.AxdEntity_LedgerJournalTrans[1]();
            ledgerJournalTrans = new LedgerJournalDAX12.AxdEntity_LedgerJournalTrans();


ledgerJournalTrans = this.InsertTrans("LedgerAccount", "Dimension_Array of AX2009", DrAmt, CrAmt,
                                    LedgerJournalDAX12.AxdEnum_LedgerJournalACType::Ledger);

                    lines.SetValue(ledgerJournalTrans, linesCreated);

 ledgerJournalTable.set_LedgerJournalTrans(lines);

            header.SetValue(ledgerJournalTable, 0);
            journal.set_LedgerJournalTable(header);

entityKeyList = new LedgerJournalDAX12.EntityKey[1]();

entityKeyList = clientAX2012.create(context, journal);

entityKey = entityKeyList.GetValue(0);
            keyFieldList = entityKey.get_KeyData();
            keyField = keyFieldList.GetValue(0);

            journalNum = keyField.get_Value();

            info(strfmt("Journal# %1 is created in AX2012.", journalNum));

ttscommit;
}
        catch(Exception::CLRError)
        {
            ex = CLRInterop::getLastException();
            info(ex.ToString());
        }
    CodeAccessPermission::revertAssert();


The InsertTrans method is explanied below.



LedgerJournalDAX12.AxdEntity_LedgerJournalTrans InsertTrans( AccountNum _accountNum, Dimension _dim, AmountCurDebit _amountCurDebit, AmountCurCredit _amountCurCredit,
                    LedgerJournalDAX12.AxdEnum_LedgerJournalACType _accountType)
{
LedgerJournalDAX12.AxdEntity_LedgerJournalTrans     ledgerJournalTrans = new LedgerJournalDAX12.AxdEntity_LedgerJournalTrans();


System.Decimal      dr = new System.Decimal(_amountCurDebit);
    System.Decimal      cr = new System.Decimal(_amountCurCredit);
    LedgerJournalDAX12.AxdType_MultiTypeAccount ledgerDimension = new LedgerJournalDAX12.AxdType_MultiTypeAccount();
    LedgerJournalDAX12.AxdType_MultiTypeDefaultAccount defaultDimension = new LedgerJournalDAX12.AxdType_MultiTypeDefaultAccount();

    LedgerJournalDAX12.AxdType_DimensionAttributeValue[] dimValuesArray;
    LedgerJournalDAX12.AxdType_DimensionAttributeValue dimValue_BS = new LedgerJournalDAX12.AxdType_DimensionAttributeValue();
    LedgerJournalDAX12.AxdType_DimensionAttributeValue dimValue_DP = new LedgerJournalDAX12.AxdType_DimensionAttributeValue();
    LedgerJournalDAX12.AxdType_DimensionAttributeValue dimValue_DIV = new LedgerJournalDAX12.AxdType_DimensionAttributeValue();
    LedgerJournalDAX12.AxdType_DimensionAttributeValue dimValue_PR = new LedgerJournalDAX12.AxdType_DimensionAttributeValue();
    

LedgerJournalDAX12.AxdType_DimensionAttributeValueSet[] dimValueSetArray;
    LedgerJournalDAX12.AxdType_DimensionAttributeValueSet defaultDimValues = new LedgerJournalDAX12.AxdType_DimensionAttributeValueSet();


String255           dimStrTotal;
    Int                 dimensionCount;
    #define.Dim1('D010_BS')
    #define.Dim2('D020_DP')
    #define.Dim3('D030_CC')
    #define.Dim4('D040_PR')
    ;

ledgerJournalTrans.set_AccountType(_accountType);
    ledgerJournalTrans.set_AccountTypeSpecified(true);

    dimensionCount = 0;
    ledgerJournalTrans.set_AmountCurDebit(dr);
    ledgerJournalTrans.set_AmountCurDebitSpecified(true);

    dept = _dim[1];
    CC = _dim[2];
    Purpose = _dim[3];


dimValue_BS.set_Name(#Dim1);
        dimValue_BS.set_Value(Dim1_2012);
        dimValue_DP.set_Name(#Dim2);
        dimValue_DP.set_Value(Dim2_2012);
        dimValue_DIV.set_Name(#Dim3);
        dimValue_DIV.set_Value(Dim3_2012);
        dimValue_PR.set_Name(#Dim4);
        dimValue_PR.set_Value(Dim4_2012);
        dimensionCount = 4;


if(_accountType == LedgerJournalDAX12.AxdEnum_LedgerJournalACType::Ledger)
    {
        ledgerDimension.set_Account(_accountNum);
        ledgerDimension.set_DisplayValue(_accountNum + '-' + dimStrTotal);
    }
    else if(_accountType == LedgerJournalDAX12.AxdEnum_LedgerJournalACType::Bank)
    {
        bankAccountTable = BankAccountTable::find(_accountNum);
        if(bankAccountTable.BankAcct_AX2012)
        {
            _accountNum = "AX2012BankAcct"; 
        }
        ledgerDimension.set_Account(_accountNum);
        ledgerDimension.set_DisplayValue(_accountNum+ '-' + dimStrTotal);

        defaultDimension.set_Account(dimStrTotal);
    }
    else
    {
        throw error("Invalid Account Type");
    }

dimValuesArray = new LedgerJournalDAX12.AxdType_DimensionAttributeValue[dimensionCount]();

    dimValuesArray.SetValue(dimValue_BS, 0);
    dimValuesArray.SetValue(dimValue_DP, 1);
    dimValuesArray.SetValue(dimValue_DIV, 2);
    dimValuesArray.SetValue(dimValue_PR, 3);

    defaultDimValues.set_Values(dimValuesArray);

    dimValueSetArray = new LedgerJournalDAX12.AxdType_DimensionAttributeValueSet[1]();
    dimValueSetArray.SetValue(defaultDimValues, 0);

ledgerDimension.set_Values(dimValuesArray);

    ledgerJournalTrans.set_LedgerDimension(ledgerDimension);

    if(_accountType == LedgerJournalDAX12.AxdEnum_LedgerJournalACType::Bank)
    {
        ledgerJournalTrans.set_DefaultDimension(defaultDimValues);
    }

    ledgerJournalTrans.set_AmountCurCredit(cr);
    ledgerJournalTrans.set_AmountCurCreditSpecified(true);

ledgerJournalTrans.set_TransDate("Date");
ledgerJournalTrans.set_TransDateSpecified(true);


LedgerJournalTrans.set_Txt("Your txt");

    ledgerJournalTrans.set_Company(curExt());

    return ledgerJournalTrans;


}

Comments

Popular posts from this blog

Sales order invoice posting using X++ with custom SalesParmLine fields

Create a custom lookup with multiple data sources on form using X++

SalesTable2Line custom field update prompt on Sales order in AX 2012