InventSum update event handler

Usual update/insert InventSum event handlers to handle simple cases
/// <summary>
/// Event handlers for InventSum table
/// </summary>
class VKInventSumEventHandler
{
    /// <summary>
    /// pre insert event handler
    /// </summary>
    /// <param name = "args">PrePost event handler arguments</param>
    [PreHandlerFor(tableStr(InventSum), tableMethodStr(InventSum, insert))]
    public static void InventSum_Pre_insert(XppPrePostArgs args)
    {
        InventSum            inventSum            = args.getThis();
        InventItemBarcode    inventItemBarcode    = InventItemBarcode::findItem(
            inventSum.ItemId,
            VKUpdateBarcodesService::getProductDimensionsOnly(inventSum.ItemId, inventSum.inventDim()),
            false,
            false);

        inventSum.VKItemBarcode = inventItemBarcode.itemBarCode;
    }

    /// <summary>
    /// pre update event handler
    /// </summary>
    /// <param name = "args">PrePost event handler arguments</param>
    [PreHandlerFor(tableStr(InventSum), tableMethodStr(InventSum, update))]
    public static void InventSum_Pre_update(XppPrePostArgs args)
    {
        InventSum            inventSum            = args.getThis();
        InventItemBarcode    inventItemBarcode    = InventItemBarcode::findItem(
            inventSum.ItemId,
            VKUpdateBarcodesService::getProductDimensionsOnly(inventSum.ItemId, inventSum.inventDim()),
            false,
            false);

        inventSum.VKItemBarcode = inventItemBarcode.itemBarCode;
    }

}
Update inventSum advanced case (InventUpdateOnhand.updateInventSum())
/// <summary>
/// An extension class for InventUpdateOnhand class
/// </summary>
[ExtensionOf(classstr(InventUpdateOnhand))]
public final class VKInventUpdateOnhand_Extension
{
    protected str sqlUpdateInventSumStr()
    {
        str    sqlSet        =  ' SET ',
            sqlFrom        = ' FROM INVENTSUMDELTA ',
            sqlGroupBy    = ' GROUP BY ',
            sqlStr,
            sqlStrFirst,
            sqlStrLast,
            sqlStrInject,
            newSqlStr;
        int injectCount = 0;

        str 256       inventSumName                = new SysDictTable(tableNum(InventSum)).name(DbBackend::Sql);
        str 256       inventSumDeltaName           = new SysDictTable(tableNum(InventSumDelta)).name(DbBackend::Sql);

        str 256       inventSumVKItemBarcodeName         = new SysDictField(tableNum(InventSum),fieldNum(InventSum,VKItemBarcode)).name(DbBackend::Sql,0,FieldNameGenerationMode::FieldList);
        str 256       inventSumDeltaVKItemBarcodeName    = new SysDictField(tableNum(InventSumDelta),fieldNum(InventSumDelta,VKItemBarcode)).name(DbBackend::Sql,0,FieldNameGenerationMode::FieldList);
  
        // see query structure at header comments of original sqlUpdateInventSumStr method 
        sqlStr = next sqlUpdateInventSumStr();

        newSqlStr = sqlStr;
        // modify SET section to insert our new field update
        int pos = strScan(newSqlStr, sqlSet, 0, strLen(newSqlStr));
        if (pos > 0)
        {
            sqlStrInject    = strFmt(' %1.%2 = %3.%4, ', inventSumName, inventSumVKItemBarcodeName, inventSumDeltaName, inventSumDeltaVKItemBarcodeName);
            newSqlStr        = this.injectStr(newSqlStr, pos + strLen(sqlSet) - 1, sqlStrInject);
            injectCount++;
        }

        // modify SELECT ... FROM section to select new field from InventSumDelta
        pos = strScan(newSqlStr, sqlFrom, 0, strLen(newSqlStr));
        if (pos > 0)
        {
            sqlStrInject    = strFmt(', %1.%2 ', inventSumDeltaName, inventSumDeltaVKItemBarcodeName);
            newSqlStr        = this.injectStr(newSqlStr, pos - 1, sqlStrInject);
            injectCount++;
        }

        // modify GROUP BY section to add out field and let it appear in selection
        pos = strScan(newSqlStr, sqlGroupBy, pos, strLen(newSqlStr));
        if (pos > 0)
        {
            sqlStrInject    = strFmt('%1.%2, ', inventSumDeltaName, inventSumDeltaVKItemBarcodeName);
            newSqlStr        = this.injectStr(newSqlStr, pos + strLen(sqlGroupBy) - 1, sqlStrInject);
            injectCount++;
        }
        if (3 == injectCount)
        {
            sqlStr = newSqlStr;
        }

        return sqlStr;
    }

    public void addInventSumDelta(
        InventSumDelta _inventSumDelta,
        InventTrans    _inventTrans,
        InventTable    _inventTable)
    {
        InventItemBarcode    inventItemBarcode    = InventItemBarcode::findItem(
            _inventSumDelta.ItemId,
            VKUpdateBarcodesService::getProductDimensionsOnly(_inventSumDelta.ItemId, InventDim::find(_inventSumDelta.inventDimId)),
            false,
            false);

        // Calculate/update required field here. InventSumDelta table should also have the same field added to InventSum
        _inventSumDelta.VKItemBarcode = inventItemBarcode.itemBarCode;

        next addInventSumDelta(_inventSumDelta, _inventTrans, _inventTable);

    }

    protected str injectStr(str _baseStr, int _pos, str _inStr)
    {
        str newStr, sqlStrFirst, sqlStrLast;

        sqlStrFirst        = subStr(_baseStr, 0, _pos);
        sqlStrLast        = subStr(_baseStr, _pos+1, strLen(_baseStr));
        newStr            = sqlStrFirst + _inStr + sqlStrLast;

        return newStr;
    }

}


 

Search

About

DaxOnline.org is free platform that allows you to quickly store and reuse snippets, notes, articles related to Dynamics AX.

Authors are allowed to set their own AdSense units and "buy me a coffee" link.
Join us.

Blog Tags