Friday, March 24, 2017

Copy data from a table to another Ax2012

Hi guys,

Another short code for copying data from one table to another (backupTable). This is useful when you have to take backup of the data while you want to perform some operations of the production data (original table) or maybe to resolve some DB sync issues.

static void AKS_CopyTableData(Args _args)
{
    EcomInboundInventoryUpdate          origTable;
    Backup_EcomInboundInventoryUpdate   copyTable;

    ttsBegin;
    delete_from copyTable;

    while select origTable
    {
        buf2Buf(origTable, copyTable);
        copyTable.insert();
    }

    ttsCommit;
}

Delete duplicate records of a table AX2012

Hi Guys,

A code snippet to delete duplicate records of a table and keeping just one of the duplicate set.

static void AKS_DeleteDuplicates(Args _args)
{
    EcomInboundInventoryUpdate      table, tableSelect, tableDelete;

    while select table group by wmsreferencenum
        where table.EcomProcessStatus == EcomProcessStatus::Waiting
    {

        select firstOnly RecId from tableSelect
            where tableselect.WMSReferenceNum == table.WMSReferenceNum;


        delete_from tableDelete
            where tableDelete.WMSReferenceNum == table.WMSReferenceNum &&
                  tableDelete.RecId != tableSelect.RecId;
    }
}

Monday, March 13, 2017

Get dimension name and value in Ax2012

Hi Guys,

Here is another code snippet to get Dimension value and name by dimension:

static DimensionValue Ale_getDefaultDimensionValue(DimensionDefault   _dimensionDefault , Name  _defaultDimensionName)
{
    DimensionAttributeValueSet      dimAttrValueSet;
    DimensionAttributeValueSetItem  dimAttrValueSetItem;
    DimensionAttributeValue         dimAttrValue;
    DimensionAttribute              dimAttr;
    Common                          dimensionValueEntity;
    DimensionValue                  dimValue;
    Name                                   dimName;
    container                           dimNameValue;
    ;
    dimAttrValueSet = DimensionAttributeValueSet::find(_dimensionDefault);

    while select DimensionAttributeValue from dimAttrValueSetItem
        where   dimAttrValueSetItem.DimensionAttributeValueSet   == dimAttrValueSet.RecId
    {
        dimAttrValue        = DimensionAttributeValue::find(dimAttrValueSetItem.DimensionAttributeValue);

        dimAttr             = DimensionAttribute::find(dimAttrValue.DimensionAttribute);

        dimensionValueEntity = DimensionDefaultingControllerBase::findBackingEntityInstance(curext(),dimAttr,dimAttrValue.EntityInstance);

        if (dimAttr.Name == _defaultDimensionName)
        {
            dimNameValue = [dimAttrValue.getValue(), dimAttrValue.getName()];
        }
    }
    return dimNameValue;
}

Tuesday, March 7, 2017

Item CostPrice per dimension Ax2012

Hi guys,

Just a quick code snippet to fetch the CostPrice of an Item based on the dimensions:

public void ale_GetCostPrice()
{
    InventDim       inventDim;
    InventDimParm   inventDimParm;
    InventOnHand    inventOnHand;
    InventSum       inventSum;

    select firstOnly1 ItemId from inventSum
        where inventSum.ItemId == this.ItemId;

    inventDim.InventSiteId = this.inventDim().InventSiteId;
    inventDim.InventLocationId = this.inventDim().InventLocationId;
    inventDim.wMSLocationId = this.inventDim().wMSLocationId;
    inventDim.InventProfileId_RU = this.inventDim().InventProfileId_RU;
    inventDimParm.initFromInventDim(inventDim);

    inventOnHand = InventOnHand::newItemDim(inventSum.ItemId, inventDim, inventDimParm);

    ttsBegin;
    this.CostPrice = inventOnHand.costPricePcs();
    this.inventMovement().journalSetCostPrice();
    this.update();
    ttsCommit;
}