Thursday, 7 November 2013

post the ledger journal through x++ code in AX 2012


For posting general journal we can use this table and classes
1.  LedgerJournalTable
2.  LedgerJournalTrans     
3.  ledgerJournalCheckPost(class) 



mandatory fields are offset account,ledger account,amount,journal name,journal number… etc


createFreeTextInvoicesAndPost(default class where one method is there-“ createGeneralJournalAndPost ”-through this method we can post the ledger journal through code)



[SysEntryPointAttribute(true)]
public str createGeneralJournalAndPost(Amount         _amount,
                                        CustAccount _cutomer,
                                        str         _businessUnit,
                                        ItemId      _itemId,
                                        str         _batchId,
                                        str         _qty,
                                        str         _costPrice,
                                        str         _billCode,
                                        str         _department,
                                        str         _costCenter,
                                        str         _employee,
                                        str         _NEW_OR_CANCEL_IND,
                                        str         _company,
                                        RecId       _InvTableRecId,
                                        LedgerDimensionDefaultAccount _defaultAccount)
{
    AxLedgerJournalTable    journalTable;
    AxLedgerJournalTrans    journalTransCredit,journalTransDebit;
    container               acctPattern;
    container               offSetAcctPattern;
    LedgerJournalTable      ledgerJournalTable,ledgerJournalTableUpdate;
    ledgerJournalCheckPost  ledgerJournalCheckPost;
    LedgerJournalTable      ledgerjournalTableDelete;
    LedgerJournalTrans      journalTransDelete;
    SalesParameters         SalesParameters;//JournalNameId;
    DimAttributeCustTable   DimAttributeCustTable;
    DimensionAttributeValueCombination valueCombination;
    CustInvoiceTable        custInvoiceTable;
    CustParameters  CustParameters;
    System.Exception        ex;
    str                     errorStr;
    LedgerJournalId         journalId;
    LedgerJournalPost       LedgerJournalPost;
    Args args = new Args();
    ;
    changeCompany(_company)
    {
        try
        {
            SalesParameters = SalesParameters::find();
            journalTable = new AxLedgerJournalTable();
            journalTransCredit = new AxLedgerJournalTrans();


            journalTable.parmJournalName(SalesParameters.JournalNameId);
            //journalTable.parmDefaultDimension(this.retDefaultDimension(_businessUnit,_department,_costCenter,_employee));
            journalTable.save();

            ledgerJournalTable = journalTable.ledgerJournalTable();

            select valueCombination where valueCombination.DisplayValue == _cutomer
                                       && valueCombination.LedgerDimensionType == LedgerDimensionType::Account
                                       && !valueCombination.MainAccount
                                       && !valueCombination.AccountStructure;

            journalTransCredit.parmJournalNum(ledgerJournalTable.JournalNum);
            journalTransCredit.parmTransDate(systemDateGet());
            journalTransCredit.parmAmountCurCredit(_amount);
            journalTransCredit.parmAccountType(LedgerJournalACType::Cust);
            journalTransCredit.parmLedgerDimension(valueCombination.RecId);
            journalTransCredit.parmDefaultDimension(_defaultAccount);
            CustParameters = CustParameters::find();

            if(_NEW_OR_CANCEL_IND == "C")
            {
                journalTransCredit.parmPostingProfile(CustParameters.PrepaymentPostingProfile);
            }
            else if(_NEW_OR_CANCEL_IND == "N")
            {
                journalTransCredit.parmPostingProfile(CustParameters.PostingProfile);
            }
            journalTransCredit.save();


            journalTransDebit = new AxLedgerJournalTrans();

            journalTransDebit.parmJournalNum(ledgerJournalTable.JournalNum);
            journalTransDebit.parmTransDate(systemDateGet());
            journalTransDebit.parmAmountCurDebit(_amount);
            journalTransDebit.parmAccountType(LedgerJournalACType::Cust);
            journalTransDebit.parmLedgerDimension(valueCombination.RecId);
            journalTransDebit.parmDefaultDimension(_defaultAccount);
            if(_NEW_OR_CANCEL_IND == "C")
            {
                journalTransDebit.parmPostingProfile(CustParameters.PostingProfile);
            }
            else if(_NEW_OR_CANCEL_IND == "N")
            {
                journalTransDebit.parmPostingProfile(CustParameters.PrepaymentPostingProfile);
            }
            journalTransDebit.save();

            ledgerJournalCheckPost              = LedgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable,Noyes::Yes);
            ledgerJournalCheckPost.run();

            return journalTransDebit.parmVoucher() + "," + journalTable.parmJournalNum();
        }
        catch(Exception::CLRError)
        {

            ex = ClrInterop::getLastException();
            if (ex != null)
            {
                ex = ex.get_InnerException();
                if (ex != null)
                {
                    error(ex.ToString());
                }
            }
            errorStr = "Error while Posting the General Journal Entry.";// + ex.ToString();
            delete_from journalTransDelete where journalTransDelete.JournalNum == journalId;
            delete_from ledgerjournalTableDelete where ledgerjournalTableDelete.JournalNum == journalId;
            //this.updateErrorInventMovement(sqlConGlobal,kRODBCParameters,itemIdStr,wareHouseStr,batchIdStr,businessUnitStr,costPriceStr,errorStr);
            this.insertErrorLogDetails(errorStr,_businessUnit,_itemId,_batchId,_qty,_costPrice,_billCode,_company);
            return "";
        }
    }


}





my code:


example:

in my form I wrote the journal posting method in form methods as


//[SysEntryPointAttribute(true)]
public void createGeneralJournalAndPost(Amount _shipnow)
{
    AxLedgerJournalTable    journalTable;
    AxLedgerJournalTrans    journalTransCredit,journalTransDebit;
    container               acctPattern;
    container               offSetAcctPattern;
    LedgerJournalTable      ledgerJournalTable,ledgerJournalTableUpdate;
    ledgerJournalCheckPost  ledgerJournalCheckPost;
    LedgerJournalTable      ledgerjournalTableDelete;
    LedgerJournalTrans      journalTransDelete;
    SalesParameters         SalesParameters;//JournalNameId;
    DimAttributeCustTable   DimAttributeCustTable;
    DimensionAttributeValueCombination valueCombination;
    CustInvoiceTable        custInvoiceTable;
    LedgerParameters        LedgerParameters;
    System.Exception        ex;
    str                     errorStr;
    LedgerJournalId         journalId;
    LedgerJournalPost       LedgerJournalPost;
    InventParameters        InventParameters;
    Amount                  markvalue;
    InventTransferTable InventTransferTable1;
    str s;

           markvalue = InventTransferTable.markupvalue(_shipnow);

            journalTable = new AxLedgerJournalTable();
            journalTransCredit = new AxLedgerJournalTrans();

              InventParameters = InventParameters::find();
            journalTable.parmJournalName(InventParameters.JournalNameId);
           // journalTable.parmTransferRecId(InventTransferTable.RecId);
            journalTable.parmDefaultDimension(InventTransferTable.DefaultDimension);
            journalTable.save();

            ledgerJournalTable = journalTable.ledgerJournalTable();

            ttsBegin;
            select forUpdate ledgerJournalTableUpdate  where ledgerJournalTableUpdate.JournalNum == ledgerJournalTable.JournalNum;
            ledgerJournalTableUpdate.TransferRefRecId = InventTransferTable.RecId;
            ledgerJournalTableUpdate.update();
            ttsCommit;
            journalTransCredit.parmJournalNum(ledgerJournalTable.JournalNum);

            journalTransCredit.parmTransDate(systemDateGet());
            journalTransCredit.parmAmountCurCredit(round(markvalue,2));
            journalTransCredit.parmAccountType(LedgerJournalACType::Ledger);
            journalTransCredit.parmLedgerDimension(InventParameters.RevenueDimension);
            journalTransCredit.parmDefaultDimension(InventTransferTable.DefaultDimension);
            LedgerParameters = LedgerParameters::find();
            journalTransCredit.parmOffsetLedgerDimension(InventParameters.LedgerDimension);

            journalTransCredit.save();

            ledgerJournalCheckPost              = LedgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable,Noyes::Yes);
            ledgerJournalCheckPost.run();
            //if (this.showInfo())
            //info(strFmt("%1,%2-%3",journalTable.parmJournalNum(),journalTransCredit.parmVoucher(),SysInfoAction_TableField::newBuffer(LedgerJournalTable)));
info(strfmt("%1", journalTable.parmJournalNum()),'',SysInfoAction_TableField::newBuffer(LedgerJournalTable));
//////////////////this above line I wrote for showing show button
    }
//}




When the general journal posted info box opened
Here we click on show button its go to current journal record.
For this functionality we can follow this procedure
Create one int64 field in ledgerjournal table(ex: TransferRefRecId)
And give the relation in ledgerjournal table to what ever table ur using that table(ex:I used the InventTransferTable)
(Then I am giving the relation as ledgerjournaltable. TransferRefRecId == InventTransferTable.recid)
After that where the journal posting method u write this following code
info(strfmt("%1", journalTable.parmJournalNum()),'',SysInfoAction_TableField::newBuffer( LedgerJournalTable));



Tuesday, 5 November 2013

How to add the ledger account (ledger dimension ) in form in AX 2012(show the ledger dimension lookup in field)

How to add the ledger account (ledger dimension ) in form in AX 2012(show the ledger dimension lookup in field)

A ledger account contains the main account, account structure, and each financial dimension value needed to fulfill the related account structure and account rule structures.A foreign key representing a ledger account is a 64 bit integer field that contains the data from the corresponding RecId field the DimensionAttributeValueCombination (LedgerDimension) table.
The ledger account control uses the LedgerDimensionAccountController class as its controller class.  This control handles the entry and display of ledger accounts in AX forms.
In simple scenarios the changes needed on an AX form are as follows:
1.    Verify that the table that holds the Foreign Key to the DimensionAttributeValueCombination table is a data source on the form.
2.    Drag the LedgerDimension field from the data source to the desired location on form design. This creates a SegmentedEntry control with the appropriate DataSource and ReferenceField property values. Alternatively this can be done by adding a SegmentedEntry control to the design and manually setting the DataSource and ReferenceField properties.
3.    Override the following methods on the form.  If the methods already exist just add the code to the methods.  Be sure to denote where the super () call is for the init () method.

classDeclaration():
public class FormRun extends ObjectRun
{
LedgerDimensionAccountController ledgerDimensionAccountController;
}
Init():
public void init()
{
super();
ledgerDimensionAccountController    =LedgerDimensionAccountController::construct(InventParameters_ds, fieldstr(InventParameters,LedgerDimension));
}

Overload the following methods on the SegmentedEntry control instance in the form design
jumpRef():
public void jumpRef()
{
ledgerDimensionAccountController.jumpRef();
}

validate():
public boolean validate()
{
boolean isValid;
isValid = super();
isValid = ledgerDimensionAccountController.validate() && isValid;
return isValid;
}
segmentValueChanged():
public void segmentValueChanged(SegmentValueChangedEventArgs _e)
{
super(_e);
ledgerDimensionAccountController.segmentValueChanged(_e);
}
loadSegments():
public void loadSegments()
{
super();
ledgerDimensionAccountController.parmControl(this);
// The valueof this parameter varies depending on the type of form.
// See the Additional options section for more detail.
ledgerDimensionAccountController.loadSegments();
}
loadAutoCompleteData():
public void loadAutoCompleteData(LoadAutoCompleteDataEventArgs _e)
{
super(_e);
ledgerDimensionAccountController.loadAutoCompleteData(_e)
}

After that go to datasource of form –datasource-field(ledgerdimension)
Overload the below on the data source field that backs the SegmentedEntry control
resolveReference():
public Common resolveReference(FormReferenceControl _formReferenceControl)
{
return ledgerDimensionAccountController.resolveReference();

}