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.


Wednesday, 5 October 2016

How to select multiple value from lookup in ax 2012

Hi,

Today we will see how to select multiple value from dropdown lookup.

In this case we will select multiple customer account num.

Step 1: Create a query

Step 2 : In this example I have created a table  but you can use existing one. I have added one field account number.
Step 3 : As I have created a new table , so I have to create new form too. Again you can continue with existing form.


Step 4 : Now Add below method in form methods

1.

public class FormRun extends ObjectRun
{
SysLookupMultiSelectCtrl msCtrl;
}
-------------------------------------------------------------------------------------------------------------
2.

public void init()
{
Query query = new Query();
super();
msCtrl = SysLookupMultiSelectCtrl::construct(element, S3_AccountTable_AccountNum, querystr(CustTablelookup));
}
---------------------------------------------------------------------------------------------------------------
3.

public int task(int _taskId)

{

#task

int ret;

ret = super(_taskId);


if (ret && _taskId == #TaskSave)
{
msCtrl.get();




}

return ret;


}

 
--------------------------------------------------------------------------------------------------------------


Step 5 : You have to do some customization in "SysLookupMultiSelectCtrl" class.

Copy and Paste below code in SysLookupMultiSelectCtrl in below listed methods.

1. init() and 2.  ctrlNames_textChange()


public void init()
{
// Added by Pramod
boolean isOnGrid(FormStringControl _ctrl)
{
FormControl parentControl;
FormControl currentControl;
boolean retValue;
currentControl = _ctrl;
retValue = false;
while(currentControl.parentControl() && !retValue)
{
parentControl = currentControl.parentControl();
if(parentControl is FormGridControl)
retValue = true;
else
currentControl = currentControl.parentControl();
}
return retValue;
}
// Addded by Pramod end
frmRun.lock();
fsCtrlIds = frmRun.design().addControl(FormControlType::String, fsCtrlNames.name() + '_Ids');
fsCtrlIds.extendedDataType(fsCtrlNames.extendedDataType());
fsCtrlNamesTmp = frmRun.design().addControl(FormControlType::String, fsCtrlNames.name() + '_Tmp');
fsCtrlNamesTmp.extendedDataType(fsCtrlNames.extendedDataType());
// set the value in ctrlNames to the same as the one in the original ctrl. Required if data is loaded from SysLastValue.
fsCtrlNamesTmp.text(fsCtrlNames.valueStr());
// Added by Pramod on 15 Sept 2016 started
if(isOnGrid(fsCtrlNames))
{
fsCtrlNames.registerOverrideMethod('modified', 'ctrlNames_textChange', this);
}
else
{
fsCtrlNames.registerOverrideMethod('textChange', 'ctrlNames_textChange', this);
}
// end
// fsCtrlNames.registerOverrideMethod('textChange', 'ctrlNames_textChange', this);
fsCtrlNames.registerOverrideMethod('lookup', 'ctrlNames_lookup', this);
fsCtrlIds.visible(false);
fsCtrlNamesTmp.visible(false);
fsCtrlIds.enabled(false);
fsCtrlNamesTmp.enabled(false);
fsCtrlNames.mandatory(isMandatory);
fsCtrlNames.lookupButton(FormLookupButton::Always);
frmRun.resetSize();
frmRun.unLock();
}
----------------------------------------------------------------------------------------------------------------------
public void ctrlNames_textChange(FormStringControl _fsCtrlNames)



{

// _fsCtrlNames.text(fsCtrlNamesTmp.text());


// fsCtrlNames.modified();



FormDataSource fds;

Common anyBuffer;

_fsCtrlNames.text(fsCtrlNamesTmp.text());

fds = _fsCtrlNames.dataSourceObject();

if(fds)



{

anyBuffer = fds.cursor();

anyBuffer.(_fsCtrlNames.dataField()) = fsCtrlNamesTmp.text();

fds.refresh();

}

fsCtrlNames.modified();

}

--------------------------------------------------------------------------------------------------------------------
Step 7: Now check the result.


Thanks

Monday, 4 July 2016

Confirm PO through Code in X++ 2012

static void POConfirmCode(Args _args)


{

SysExcelApplication xlsApplication;
SysExcelWorkBooks xlsWorkBookCollection;
SysExcelWorkBook xlsWorkBook;
SysExcelWorksheets xlsWorkSheetCollection;
SysExcelWorksheet xlsWorkSheet;
SysExcelRange xlsRange;
SysExcelCells Cells;
SysExcelCell RCell;
CommaIO inFile;
#define.ExampleOpenMode("r")
int nRow,i;


DialogField dialogPath;
Dialog dialog;
Filename filename;
Name Name;

PurchFormLetter purchFormLetter;
PurchFormletterParmData purchFormLetterParmData;
PurchParmUpdate purchParmUpdate;
PurchParmTable purchParmTable;
PurchParmLine purchParmLine;
PurchTable purchTable;
PurchLine purchLine;
PurchId purchId;
Num packingSlipId;

;
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, 1);
while (RCell.value().bstr() != "")


{

purchId = RCell.value().bStr();
purchTable = PurchTable::find(purchId);
ttsBegin;

// Create PurchParamUpdate table


purchFormLetterParmData = PurchFormletterParmData::newData(
DocumentStatus::PurchaseOrder,
VersioningUpdateType::Initial);
purchFormLetterParmData.parmOnlyCreateParmUpdate(true);
purchFormLetterParmData.createData(false);


purchParmUpdate = purchFormLetterParmData.parmParmUpdate();
// Set PurchParmTable table


purchParmTable.clear();
purchParmTable.TransDate = SystemDateGet();
purchParmTable.DocumentDate = SystemDateGet();


purchParmTable.Ordering = DocumentStatus::PurchaseOrder;
purchParmTable.ParmJobStatus = ParmJobStatus::Waiting;
purchParmTable.PurchId = purchTable.PurchId;
purchParmTable.PurchName = purchTable.PurchName;
purchParmTable.DeliveryName = purchTable.DeliveryName;
purchParmTable.DeliveryPostalAddress = purchTable.DeliveryPostalAddress;
purchParmTable.OrderAccount = purchTable.OrderAccount;
purchParmTable.CurrencyCode = purchTable.CurrencyCode;
purchParmTable.InvoiceAccount = purchTable.InvoiceAccount;
purchParmTable.ParmId = purchParmUpdate.ParmId;
purchParmTable.insert();

purchFormLetter = PurchFormLetter::construct(DocumentStatus::PurchaseOrder);
purchFormLetter.transDate(systemDateGet());
purchFormLetter.proforma(false);


purchFormLetter.specQty(PurchUpdate::All);
purchFormLetter.purchTable(purchTable);
// This is the ID we hard code as the product receipt ID, if we do the posting via UI
// user would have the option to manually enter this value


purchFormLetter.parmParmTableNum(purchParmTable.ParmId);
purchFormLetter.parmId(purchParmTable.ParmId);
purchFormLetter.purchParmUpdate(purchFormLetterParmData.parmParmUpdate());
purchFormLetter.run();
ttsCommit;


nRow++;
// info(strFmt("%1", counte));
// counte = 0;
RCell = Cells.Item(nRow, 1);



}
xlsApplication.quit ();
xlsApplication.finalize ();
info("Import completed");


}
catch( Exception::Error)


{
xlsApplication.quit ();
xlsApplication.finalize ();
ttsabort;
info("Unable to process the excel import");


}
}