Friday, June 9, 2017

AX 7. Process(update/post opertion) the data after Data Entity import.

It’s a common requirement to perform post actions on records that are imported by Data Entity, for example to invoice created sales order or to post created journal.
To do this we need to add a new method postTargetProcess() to our Data Entity. This method is automatically called by the DMFEntityWriter class in the end of processRecords() method when all records are transferred from staging to target. It is not available from override method drop down on Data Entity because it is static and is called via reflection.

/// 
/// 
/// The definition group that should be processed.
/// 
public static void postTargetProcess(DMFDefinitionGroupExecution _dmfDefinitionGroupExecution)
{
    //check if import job is finished
    if (_dmfDefinitionGroupExecution.StagingStatus == DMFBatchJobStatus::Finished)
    {
        MyStaging myStaging;

        //select all staging records that were processed by current execution job without errors.
        while select myStaging
            where myStaging.DefinitionGroup == _dmfDefinitionGroupExecution.DefinitionGroup
               && myStaging.ExecutionId     == _dmfDefinitionGroupExecution.ExecutionId
               && myStaging.TransferStatus  == DMFTransferStatus::Completed
        {
            //here you can find newly created records and update\post them.
        }
    }
}
Please note that it can be done only in data management scenarios but not via OData because OData updates\inserts records row by row and there is no post event\method to use.

Friday, February 10, 2017

Dynamics 365 - Table Extension Predefined Method() and Userdefined Method():


public static class SalesLine_Extension
{
    [PostHandlerFor(tableStr(SalesLine), tableMethodStr(SalesLine, insert))]
    public static void SalesLine_Post_insert(XppPrePostArgs args)
    {
        SalesLine                  saleslineLoc;
        FormDataSource      salesFormDataSource;           
      
        saleslineLoc        =  args.getthis();

        if(saleslineLoc.SalesId)
        {           
            Info("SalesID existed");
         }
         salesFormDataSource =  FormDataUtil::getFormDataSource(saleslineLoc);
               
         if(salesFormDataSource)
         {
                 salesFormDataSource.research(true);
         }
     }
public static void updateDefaultWarehouse(SalesLine _salesLine)
    {
        InventTable         inventTable;
        ;       
        inventTable   = InventTable::find(_salesLine.ItemId);
    }

[DataEventHandler(tableStr(SalesLine), DataEventType::ModifiedFieldValue)]
    public static void SalesLine_onModifiedFieldValue(Common sender, DataEventArgs e)
    {
        SalesLine       salesLineLocal = sender;
        Table              table1;
       
       
        if(salesLineLocal.SalesId)
       {        
            ttsbegin;
            table1.OrderNumber       = salesLineLocal.SalesId;
            table1.SalesType         = salesLineLocal.SalesType;
            table1.ItemId            = salesLineLocal.ItemId;
            table1.insert();
            ttscommit;
        }
    }
[PostHandlerFor(tableStr(SalesLine), tableMethodStr(SalesLine, update))]
    public static void SalesLine_Post_update(XppPrePostArgs args)
    {
        SalesLine   salesLineLocal;
        SalesTable  salesTable;

        salesLineLocal        =  args.getthis();
        ttsbegin;
            salesTable = salesTable::find(salesLineLocal.SalesId, true);
            if (salesTable)
            {
                salesTable.FieldId= SalesTable_Extension::MethodName(salesTable);
                salesTable.update();
            }
            ttscommit;
        }
[PostHandlerFor(tableStr(SalesLine), tableMethodStr(SalesLine, modifiedField))]
    public static void SalesLine_Post_modifiedField(XppPrePostArgs args)
    {
        FieldId fieldId = args.getArg('_fieldId');
        SalesLine salesLine = args.getThis();

        switch (fieldId)
        {
             case fieldNum(SalesLine, NewItemNumber):
               
               if (salesLine.NewItemNumber)                    
                {
                    ItemId itemId = salesLine.ItemId;
                    Info(strfmt("%1, " ItemId"));
                }
                break;     
        }
    }
[PostHandlerFor(tableStr(SalesLine), tableMethodStr(SalesLine, delete))]
    public static void SalesLine_Post_delete(XppPrePostArgs args)
    {
        SalesLine           salesLine;
        TableName       tableName;       
        Args                _args = new Args();

        salesLine=  args.getthis();

        if(salesLine.RecId)
        {
            ttsbegin;
            delete_from tableName
                where  tableName.OrderNumber    == salesLine.SalesId
                    && tableName.ItemId         == salesLine.ItemId
                    && tableName.LineNum        == salesLine.LineNum;
            ttscommit;
        }
    }

Dynamics 365 - DataSource Method:

public class InventLocation_Extension
{
    ///
    /// Post event handler for InventLocation SalesLine Initialized event.
    ///

    ///
    ///

[FormDataSourceEventHandler(formDataSourceStr(InventLocation, InventLocation), FormDataSourceEventType::Initialized)]
    public static void InventLocation_OnInitialized(FormDataSource sender, FormDataSourceEventArgs e)
    {
        InventLocation  inventLocation = sender.cursor();
        if (inventLocation.InventLocationId)
    {
        info("There is inventLocation Id existed");
    }
    else
    {
        info("There is NO inventLocation Id");
    }
    }
}