Wednesday, 30 November 2016

Unit Conversion by code in ax 2012 R3 , X++

static void unitConversion()
{
    SysExcelApplication xlsApplication;
    SysExcelWorkBooks xlsWorkBookCollection;
    SysExcelWorkBook xlsWorkBook;
    SysExcelWorksheets xlsWorkSheetCollection;
    SysExcelWorksheet xlsWorkSheet;
    SysExcelRange xlsRange;
    SysExcelCells Cells;
    SysExcelCell RCell;
    CommaIO inFile;
    int nRow,i;
    DialogField dialogPath;
    Dialog dialog;
    Filename filename;

   

    UnitOfMeasureConversion             unitOfMeasureConversion;
    UnitOfMeasure                       unitOfMeasure;
    EcoResProduct                       ecoResProduct;
    RecId                               fromUnitofMeasure, toUnitofMeasure, _product;
    int                                 x;
    UnitIDBase                          fromunit, tounit;
    UnitOfMeasureConversionDenominator  factor;

  ;
    dialog = new Dialog("Import");
    dialogPath = dialog.addField(extendedTypeStr(Filenameopen), "File Name");
    dialog.run();
    if (dialog.run())
    {
    filename = (dialogPath.value());
    }
    inFile = new CommaIO (filename, 'R');
    if (!inFile || infile.status() != IO_Status::Ok )
    {
    throw error (strfmt("@SYS19312",filename));
    }
    try
    {
    xlsApplication = SysExcelApplication::construct();
    xlsWorkBookCollection = xlsApplication.workbooks();
    xlsWorkBookCollection.open(filename);
    xlsWorkSheetCollection = xlsApplication.worksheets();
    xlsWorkSheet = xlsWorkSheetCollection.itemFromNum(1);
    Cells = xlsWorkSheet.Cells();
    nRow = 2;
    RCell = Cells.Item(nRow, 2);
    while(RCell.value().bstr() != "")
    {
            RCell = Cells.item(nRow,1);
            itemID = RCell.value().bStr();
            RCell = Cells.item(nRow,2);
            fromUnit = RCell.value().bStr();
            RCell = Cells.item(nRow,3);
            toUnit = RCell.value().bStr();
             RCell = Cells.item(nRow,4);
            factor = RCell.value().double();

                select unitOfMeasure where unitOfMeasure.Symbol == fromunit;
                    fromunitofMeasure = unitOfMeasure.RecId;
                select unitOfMeasure where unitOfMeasure.Symbol == tounit;
                    toUnitofMeasure = unitOfMeasure.RecId;
                _product = EcoResProduct::findByProductNumber(itemID).RecId;

                    unitOfMeasureConversion.FromUnitOfMeasure   = fromunitofMeasure;
                    unitOfMeasureConversion.ToUnitOfMeasure     = toUnitofMeasure;
                    unitOfMeasureConversion.Product             = _product;
                    unitOfMeasureConversion.Factor              = factor; // pcs to box purchase factor or sales factor
                    unitOfMeasureConversion.Numerator           = 1;
                    unitOfMeasureConversion.Denominator         = 1;  // pcs to box purchase factor or sales factor opposite
                    unitOfMeasureConversion.InnerOffset         = 0.0;
                    unitOfMeasureConversion.OuterOffset         = 0.0;
                    unitOfMeasureConversion.Rounding            = UnitOfMeasureConversionRounding::Nearest;
                    unitOfMeasureConversion.insert();

         nRow++;
        RCell = Cells.Item(nRow, 2);
        }

    xlsApplication.quit ();
    xlsApplication.finalize ();
    info("Import completed");
    }
    catch( Exception::Error)
    {
    xlsApplication.quit ();
    xlsApplication.finalize ();
    ttsabort;
    info("Unable to process the excel import ");
    }

}

Monday, 28 November 2016

AIF Document Service Invalid Data Container Type

Error:

Invalid data container type.

Resolution:

Compile forward AfStronglyTypedDataContainer class.

Wednesday, 16 November 2016

Update Ledger dimension through code in AX 2012 R3 (Account Structure) X++

Public void updateLedgerDimension(str 30 finMainaccount,str finArea,str fnCostCenter,str finFamily,str finBussiness,str finProject,str finSeason,str finCompany)
{
    List ListValueContract = new List(Types::Class);
    DimensionServiceProvider DimensionServiceProvider = new DimensionServiceProvider();
    LedgerAccountContract LedgerAccountContract = new LedgerAccountContract();
    updateValue = false;
    if(finArea)
    {
        ValueContract = new DimensionAttributeValueContract();
        ValueContract.parmName('Area') ;
        ValueContract.parmValue(finArea);
        ListValueContract.addEnd(ValueContract);
    }

    if(fnCostCenter)
    {
        ValueContract = new DimensionAttributeValueContract();
        ValueContract.parmName('CostCenter') ;
        ValueContract.parmValue(fnCostCenter);
        ListValueContract.addEnd(ValueContract);
    }

    if(finFamily)
    {
        ValueContract = new DimensionAttributeValueContract();
        ValueContract.parmName('Family') ;
        ValueContract.parmValue(finFamily);
        ListValueContract.addEnd(ValueContract);
    }

    if(finBussiness)
    {
        ValueContract = new DimensionAttributeValueContract();
        ValueContract.parmName('Business') ;
        ValueContract.parmValue(finBussiness);
        ListValueContract.addEnd(ValueContract);
    }

    if(finProject)
    {
        ValueContract = new DimensionAttributeValueContract();
        ValueContract.parmName('Project') ;
        ValueContract.parmValue(finProject);
        ListValueContract.addEnd(ValueContract);
    }


    if(finSeason)
    {
        ValueContract = new DimensionAttributeValueContract();
        ValueContract.parmName('Season') ;
        ValueContract.parmValue(finSeason);
        ListValueContract.addEnd(ValueContract);
    }

    /*
    if(finCompany == "RND")
    {
        ValueContract = new DimensionAttributeValueContract();
        ValueContract.parmName('Company') ;
        ValueContract.parmValue('');
        ListValueContract.addEnd(ValueContract);
    }
    */
        LedgerAccountContract.parmMainAccount(finMainaccount);
        LedgerAccountContract.parmValues(ListValueContract);


        dimStorage = DimensionServiceProvider::buildDimensionStorageForLedgerAccount(LedgerAccountContract);
        dimensionAttributeValueCombination = DimensionAttributeValueCombination::find(dimStorage.save());
        updateValue = true;
 }


For  finMainaccount,str finArea,str fnCostCenter,str finFamily,str finBussiness,str finProject,str finSeason,str finCompany this value please follow below link.

http://daxinfo.blogspot.in/2016/11/get-dimension-value-from-ledger.html

Get Dimension Value from ledger dimesion through X++ AX 2012 R3

static void getDimValue(Args _args)
{
str 30 finMainaccount, finArea, finBussiness, fnCostCenter, finFamily, finSeason, finProject, finCompany;
DimensionAttributeLevelValueAllView dimAttrView; //View that will display all values for ledger dimensions
    DimensionAttribute                  dimAttr; //Main dimension attribute table
    int i;

        i++;


        while select DisplayValue from dimAttrView
            where dimAttrView.ValueCombinationRecId == generalJournalAccountEntry.LedgerDimension
            join Name from dimAttr
                where dimAttr.RecId == dimAttrView.DimensionAttribute
        {
            switch (dimAttr.Name)
            {
                case "MainAccount":
                    finMainaccount = strFmt(dimAttrView.DisplayValue);
                    break;

                case "Area":
                    finArea  = dimAttrView.DisplayValue;
                    break;

                case "CostCenter":
                    fnCostCenter =  dimAttrView.DisplayValue;
                    break;

                case "Family":
                    finFamily = dimAttrView.DisplayValue;
                    break;

                case "Business":
                    finBussiness =  dimAttrView.DisplayValue;
                    break;

                case "Project":
                    finProject = dimAttrView.DisplayValue;
                    break;

                case "Season":
                    finSeason =  dimAttrView.DisplayValue;
                    break;

                case "Company":
                    finCompany = dimAttrView.DisplayValue;
                    break;
            }
     }
}

Thursday, 3 November 2016

Deleting unused Main Account Values in Ax 2012

A financial dimension value is based on the Main Account  record and has been used on a transaction, You cannot delete the Main Account record. 
In order to get rid off un-used dimension values, delete the related records in the following tables1.DimensionAttributeValueCombination
By default the ledger dimension values are stored in DimensionAttributeValueCombination.
The DimensionAttributeValueCombination table contains information about accounts and various dimensions combinations that are used. Anything that uses dimensions will hold reference to a record on this table.
 
2.DimensionAttributeLevelValue
This table contains the usage of  a dimension attribute value in a given dimension hierarchy.
3.DimensionAttributeValueGroup
This table represents a group of values for the specific dimension set.
4.DimensionAttributeValueGroupCombination
It contains the usage of dimension code groups in a  dimension code combination.
It allows a group to be re-used in multiple combination.
5.DimensionAttributeValue
This table contains the values, such as dimension codes, for a specific dimension.

After removing the reference records in the above tables, you can remove main account from its master itself.


Or you can use below code to delete the transaction record.

static void clear_LedgerAttributeCombinations(Args _args)



{

MainAccount mainAccount;

DimensionAttributeValueCombination dimAttributeValueCombo;

DimensionAttributeValueGroupCombination dimAttributeValueGroupCombo;

DimensionAttributeLevelValue dimAttributeLevelValue;

DimensionAttributeValue dimAttributeValue;

;

ttsBegin;

while select mainAccount

where mainAccount.LedgerChartOfAccounts == 5637144576 // Rec id for your COA.you can find it in Main account Table.



{

while select forUpdate dimAttributeLevelValue

where dimAttributeLevelValue.DisplayValue == mainAccount.MainAccountId



{

delete_from dimAttributeValue

where dimAttributeValue.RecId == dimAttributeLevelValue.DimensionAttributeValue &&



dimAttributeValue.EntityInstance == mainAccount.RecId &&

dimAttributeValue.DimensionAttribute == DimensionAttribute::getMainAccountDimensionAttribute();

dimAttributeLevelValue.delete();

while select forUpdate dimAttributeValueGroupCombo

where dimAttributeValueGroupCombo.DimensionAttributeValueGroup == dimAttributeLevelValue.DimensionAttributeValueGroup



{

delete_from dimAttributeValueCombo

where dimAttributeValueCombo.RecId == dimAttributeValueGroupCombo.DimensionAttributeValueCombination;



dimAttributeValueGroupCombo.delete();

}

}

info(strFmt("%1", mainAccount.MainAccountId));



}

ttsCommit;




}

Now you will be able to delete the main accounts.