Tuesday 19 July 2016

Merging of Dimensions while creating a new General Ledger Journal

Merging of ledger and default dimensions while creating a new GL journal using DIXF:


While creating the GL journal in Ax using DIXF, below code can be used.

Scenerio:
The financial dimensions needs to be default if they are not provided in the file.
·         If no Financial dimension is provided in the file, then the values from respective masters to default.
·         Even if one financial dimension provided in the file only that value will be imported. No values should be picked from Master data.

If the Account Type is Customer, Vendor, Bank and Fixed Asset then,
LedgerJournalTrans.DefaultDimension = default dimensions value of the master record &
LedgerJournalTrans.LedgerDimension = CustId or VendId or BankAccount or FixedAsset value


private LedgerJournalTrans mergeLedgerDimensions(LedgerJournalTrans _ledgerJournalTrans)
{
    DimensionMerge  dimensionMerge;

    dimensionMerge = DimensionMerge::newFromTable(_ledgerJournalTrans,                _ledgerJournalTrans.companyInfo().RecId);

        switch (_ledgerJournalTrans.AccountType)
        {
            //Customer
            case LedgerJournalACType::Cust:
                if (!_ledgerJournalTrans.DefaultDimension)
                {
                    _ledgerJournalTrans.DefaultDimension  =
                    dimensionMerge.merge(CustTable::find(DimensionAttributeValueCombination::getDisplayValue(
                                        _ledgerJournalTrans.LedgerDimension)).DefaultDimension, _ledgerJournalTrans.DefaultDimension);
                }
                break;

            //Vendor
            case LedgerJournalACType::Vend:
               if (!_ledgerJournalTrans.DefaultDimension)
                {
                    _ledgerJournalTrans.DefaultDimension  =
                    dimensionMerge.merge(VendTable::find(DimensionAttributeValueCombination::getDisplayValue(
                                        _ledgerJournalTrans.LedgerDimension)).DefaultDimension,               _ledgerJournalTrans.DefaultDimension);
                }

                break;

            //Bank
            case LedgerJournalACType::Bank:
               if (!_ledgerJournalTrans.DefaultDimension)
                {
                    _ledgerJournalTrans.DefaultDimension  =
                    dimensionMerge.merge(BankAccountTable::find(DimensionAttributeValueCombination::getDisplayValue(
                                        _ledgerJournalTrans.LedgerDimension)).DefaultDimension, _ledgerJournalTrans.DefaultDimension);
                }
                break;

            //FixedAssets
            case LedgerJournalACType::FixedAssets:
               if (!_ledgerJournalTrans.DefaultDimension)
                {
                    _ledgerJournalTrans.DefaultDimension  =
                        dimensionMerge.merge(AssetTable::find(DimensionAttributeValueCombination::getDisplayValue(
                                        _ledgerJournalTrans.LedgerDimension)).DefaultDimension, _ledgerJournalTrans.DefaultDimension);
                }
                break;

            //Ledger
            case LedgerJournalACType::Ledger:
                //If Ledger Dimension has no dimensions other than Main account then the below if condition will return 0, otherwise it will return a RecId value.
                if (!DimensionStorage::getDefaultDimensionFromLedgerDimension(_ledgerJournalTrans.LedgerDimension))
                {
                    _ledgerJournalTrans.LedgerDimension =
                        DimensionDefaultingService::ServiceCreateLedgerDimension(_ledgerJournalTrans.LedgerDimension,
                            MainAccountLegalEntity::findByMainAccountLegalEntity(
                                MainAccount::findByLedgerDimension(_ledgerJournalTrans.LedgerDimension).RecId,
                                CompanyInfo::findDataArea(curext()).RecId).DefaultDimension);
                }
                break;

            default:
                break;
        }

       // For Offset dimensions

        switch (_ledgerJournalTrans.OffsetAccountType)
        {
            //Customer
            case LedgerJournalACType::Cust:
                if (!_ledgerJournalTrans.OffsetDefaultDimension)
                {
                    _ledgerJournalTrans.OffsetDefaultDimension  =
                    dimensionMerge.merge(CustTable::find(DimensionAttributeValueCombination::getDisplayValue(
                                        _ledgerJournalTrans.OffsetLedgerDimension)).DefaultDimension, _ledgerJournalTrans.OffsetDefaultDimension);
                }
                break;

             //Vendor
            case LedgerJournalACType::Vend:
               if (!_ledgerJournalTrans.OffsetDefaultDimension)
                {
                    _ledgerJournalTrans.OffsetDefaultDimension  =
                        dimensionMerge.merge(VendTable::find(DimensionAttributeValueCombination::getDisplayValue(
                                        _ledgerJournalTrans.OffsetLedgerDimension)).DefaultDimension, _ledgerJournalTrans.OffsetDefaultDimension);
                }

                break;

            //Bank
            case LedgerJournalACType::Bank:
               if (!_ledgerJournalTrans.OffsetDefaultDimension)
                {
                    _ledgerJournalTrans.OffsetDefaultDimension  =
                    dimensionMerge.merge(BankAccountTable::find(DimensionAttributeValueCombination::getDisplayValue(
                                        _ledgerJournalTrans.OffsetLedgerDimension)).DefaultDimension, _ledgerJournalTrans.OffsetDefaultDimension);
                }
                break;

            //FixedAssets
            case LedgerJournalACType::FixedAssets:
               if (!_ledgerJournalTrans.OffsetDefaultDimension)
                {
                     _ledgerJournalTrans.OffsetDefaultDimension  =
                        dimensionMerge.merge(AssetTable::find(DimensionAttributeValueCombination::getDisplayValue(
                                        _ledgerJournalTrans.OffsetLedgerDimension)).DefaultDimension, _ledgerJournalTrans.OffsetDefaultDimension);
                }
                break;

            //ledger
            case LedgerJournalACType::Ledger:
                if (!DimensionStorage::getDefaultDimensionFromLedgerDimension(_ledgerJournalTrans.OffsetLedgerDimension))
                {
                    _ledgerJournalTrans.OffsetLedgerDimension =
                        DimensionDefaultingService::ServiceCreateLedgerDimension(_ledgerJournalTrans.OffsetLedgerDimension,
                            MainAccountLegalEntity::findByMainAccountLegalEntity(
                                MainAccount::findByLedgerDimension(_ledgerJournalTrans.OffsetLedgerDimension).RecId,
                                CompanyInfo::findDataArea(curext()).RecId).DefaultDimension);
                }
                break;

            default:
                break;
        }
    return _ledgerJournalTrans;
}

To create Consolidated Vendor Invoices while importing data from DIXF:

To create Consolidated Vendor Invoices while importing data from DIXF:
Below is the Code:


public void createConsolidatedInvoices(isConsolidatedInvoice    _consolidatedInvoice = NoYes::No)
{
    DMFVendInvoiceInfoTableEntity        dmfVendInvoiceInfoTableEntity;
    DMFVendInvoiceInfoLineEntity          dmfVendInvoiceInfoLineEntity;
    PurchFormLetter                                   purchFormLetterConsolidate;
    PurchFormletterParmData                    purchFormletterParmData;
    PurchParmUpdate                                 purchParmUpdate;
    PurchParmTable                                    purchParmTable;
    PurchTable                                            purchTable;

    VendInvoiceInfoTable                          vendInvoiceInfoTable;
    VendInvoiceInfoLine                           vendInvoiceInfoLineLocal;

    if (stagingTable is DMFVendInvoiceInfoLineEntity)
    {
        dmfVendInvoiceInfoLineEntity = stagingTable;
    }

    if  (_consolidatedInvoice)
    {
        select firstonly purchParmUpdate
                join  PurchId, Num, ParmId from purchParmTable
                join  PackingSlipId, TableRefId, ParmId from dmfVendInvoiceInfoTableEntity
                join  OrigPurchId, InventTransId, TableRefId from vendInvoiceInfoLineLocal
                    where  purchParmUpdate.ParmId == purchParmTable.ParmId                            &&
                               purchParmTable.Num        == dmfVendInvoiceInfoTableEntity.PackingSlipId                                                                                                                                                            &&
                              purchParmTable.PurchId    == vendInvoiceInfoLineLocal.OrigPurchId    &&
                              dmfVendInvoiceInfoTableEntity.TableRefId   ==               dmfVendInvoiceInfoLineEntity.TableRefId          &&
                           vendInvoiceInfoLineLocal.OrigPurchId     == dmfVendInvoiceInfoLineEntity.OrigPurchId         &&
                           vendInvoiceInfoLineLocal.InventTransId   == dmfVendInvoiceInfoLineEntity.InventTransId;

        purchTable  = PurchTable::find(purchParmTable.PurchId);

        vendInvoiceInfoTable = VendInvoiceInfoTable::find(dmfVendInvoiceInfoTableEntity.ParmId, vendInvoiceInfoLineLocal.TableRefId);
        try
        {
            //ttsbegin;
            purchFormletterParmData = PurchFormletterParmData::newData(
                DocumentStatus::PackingSlip,
                VersioningUpdateType::Initial);

            purchFormletterParmData.parmOnlyCreateParmUpdate(true);
            purchFormletterParmData.createData(false);

            purchFormLetterConsolidate = PurchFormLetter::construct(DocumentStatus::Invoice);
            purchFormLetterConsolidate.transDate(systemDateGet());
            purchFormLetterConsolidate.proforma(false);
            purchFormLetterConsolidate.purchTable(purchTable);
            purchFormLetterConsolidate.purchParmUpdate(purchParmUpdate);

            purchFormLetterConsolidate.reArrange();
            //ttscommit;

       

        }
        catch
        {
            throw error("Error occurred while consolidating the invoices");
        }
       
        try
        {
                    //For posting
             purchFormLetterInvoice =                                PurchFormLetter_Invoice::newFromSavedInvoice(vendInvoiceInfoTable);
        purchFormLetterInvoice.update(vendInvoiceInfoTable.purchTable(),                            vendInvoiceInfoTable.Num);
        }

        catch
        {
            throw error("Error occurred while posting the invoices");
        }

    }
   
}


Best Regards
Pranav :)

Consolidate multiple PO's to a single Invoice in Ax.

Consolidate multiple PO's to a single Invoice in Ax.
Below is the code:

static void InvoiceConsolidation(Args _args)
{
PurchFormLetter purchFormLetter;
PurchFormletterParmData purchFormLetterParmData;
PurchParmUpdate purchParmUpdate;
PurchParmTable purchParmTable;
PurchParmLine purchParmLine;
PurchTable purchTable;
PurchLine purchLine;
PurchId purchId;
Num packingSlipId;
Num invoiceId;

VendInvoiceInfoTable vendInvoiceInfoTable;
VendInvoiceInfoLine vendInvoiceInfoLine;
VendInvoiceInfoSubTable vendInvoiceInfoSubTable;



purchId = "PO0000230";
packingSlipId = "PS000012";


purchTable = PurchTable::find(purchId);

ttsBegin;
// Create PurchParamUpdate table
purchFormLetterParmData = PurchFormletterParmData::newData(
DocumentStatus::PackingSlip,
VersioningUpdateType::Initial);

purchFormLetterParmData.parmOnlyCreateParmUpdate(true);
purchFormLetterParmData.createData(false);

purchParmUpdate = PurchParmUpdate::find('PHO-004150', false);

purchParmTable = PurchParmTable::findPurchId("PO0000230", 'PHO-004150');

vendInvoiceInfoTable.initFromPurchTable(purchTable);

vendInvoiceInfoTable.Approved = NoYes::Yes;
vendInvoiceInfoTable.DeliveryName = purchTable.DeliveryName;
vendInvoiceInfoTable.PurchName = purchTable.PurchName;
vendInvoiceInfoTable.BatchAdministration = NoYes::Yes;
vendInvoiceInfoTable.ParmJobStatus = ParmJobStatus::Waiting;
vendInvoiceInfoTable.FixedDueDate = purchTable.FixedDueDate;
vendInvoiceInfoTable.LastMatchVariance = LastMatchVarianceOptions::OK;
vendInvoiceInfoTable.TransDate = systemDateGet();
vendInvoiceInfoTable.VendInvoiceSaveStatus = VendInvoiceSaveStatus::Pending;

vendInvoiceInfoTable.Num = "Inv_" + purchTable.PurchId;

vendInvoiceInfoTable.insert();

// Set PurchParmLine table
while select purchLine
where purchLine.PurchId == 'PO0000230'
{
vendInvoiceInfoLine.clear();
vendInvoiceInfoLine.initFromPurchLine(purchLine);
vendInvoiceInfoLine.InventNow = 1;
vendInvoiceInfoLine.LineAmount = purchLine.LineAmount;
vendInvoiceInfoLine.ReceiveNow = 1;
vendInvoiceInfoLine.RemainBefore = 1;
vendInvoiceInfoLine.RemainBeforeInvent = 1;
vendInvoiceInfoLine.TableRefId = vendInvoiceInfoTable.TableRefId;
vendInvoiceInfoLine.insert();
}

// Set PurchParmLine table
while select purchLine
where purchLine.PurchId == 'PO0000231'
{
vendInvoiceInfoLine.clear();
vendInvoiceInfoLine.initFromPurchLine(purchLine);
vendInvoiceInfoLine.InventNow = 1;
vendInvoiceInfoLine.LineAmount = purchLine.LineAmount;
vendInvoiceInfoLine.ReceiveNow = 1;
vendInvoiceInfoLine.RemainBefore = 1;
vendInvoiceInfoLine.RemainBeforeInvent = 1;
vendInvoiceInfoLine.TableRefId = vendInvoiceInfoTable.TableRefId;
vendInvoiceInfoLine.insert();
}

vendInvoiceInfoSubTable.clear();
vendInvoiceInfoSubTable.OrigPurchId = 'PO0000230';
vendInvoiceInfoSubTable.TableRefId = vendInvoiceInfoTable.TableRefId;
vendInvoiceInfoSubTable.PurchName = purchTable.PurchName;
vendInvoiceInfoSubTable.insert();

vendInvoiceInfoSubTable.clear();
vendInvoiceInfoSubTable.OrigPurchId = 'PO0000231';
vendInvoiceInfoSubTable.PurchName = purchTable.PurchName;
vendInvoiceInfoSubTable.TableRefId = vendInvoiceInfoTable.TableRefId;
vendInvoiceInfoSubTable.insert();

purchFormLetter = PurchFormLetter::construct(DocumentStatus::Invoice);
purchFormLetter.transDate(systemDateGet());
purchFormLetter.proforma(false);
purchFormLetter.specQty(PurchUpdate::ReceiveNow);
purchFormLetter.purchTable(purchTable);
purchFormLetter.purchParmUpdate(purchParmUpdate);

purchFormLetter.reArrange(true,true,true);
purchFormLetter.update(purchtable, vendInvoiceInfoTable.Num , systemdateget());

info('Done');

}

Best Regards
Pranav

To check whether a financial dimension has dimension values other than Main account.

Hi, 
Recently I have a requirement where I have to check whether a financial dimension has dimension values other than "Main account" like "Cost center, Department" etc, while creating a GL journal.
Below is the code which returns a Int64 recId if the given dimension value has atleast one dimension value other than main account, otherwise it will return '0'.
Based on that I have to check, if the financial dimension coming from the file doesn't have dimensions other than Main account, I have to merge the default dimensions of the main account while creating a GL journal line.

if (!DimensionStorage::getDefaultDimensionFromLedgerDimension(_ledgerJournalTrans.LedgerDimension))
{
     The below method takes 2 parameters:
         DimensionDefaultingService::ServiceCreateLedgerDimension(Parameter 1, parameter 2);
   Parameter 1) RecId of the Current dimension that needs to merge.
   Parameter 2) Main Account default dimension recId.
              To find the Main account default dimensions recId use this method:
                          MainAccountLegalEntity::findByMainAccountLegalEntity(Parameter 1, parameter 2)
                          Parameter 1) RecId of the Current MainAccount.
                          Parameter 2) Current company reci Id.
                                      
This is how the code will look like:
       _ledgerJournalTrans.LedgerDimension = DimensionDefaultingService::ServiceCreateLedgerDimension(_ledgerJournalTrans.LedgerDimension,
          MainAccountLegalEntity::findByMainAccountLegalEntity(
          MainAccount::findByLedgerDimension(_ledgerJournalTrans.LedgerDimension).RecId,
          CompanyInfo::findDataArea(curext()).RecId).DefaultDimension);
}


Best Regards
Pranav Gupta

Insert/Update or remove the default dimension value in D365 FSCM via x++

Use below method to insert/update the dimension values, just pass the parameter values to the method and it will return the updated value: p...