Monday, 30 July 2018

Creating custom financial dimension Lookup using X++ code in AX / D365FO / AX 7


Here is the sample X++ code to create custom financial dimension lookup

public void Lookup(FormStringControl _control)
{
        DimensionAttribute                  dimensionAttribute;
        DimensionAttributeDirCategory       dimAttributeDirCategory;
        Query                               query = new Query();
        SysTableLookup                      sysTableLookup;

        dimensionAttribute = DimensionAttribute::findByName('ServiceLine');

        if (dimensionAttribute.Type == DimensionAttributeType::CustomList)

        {

        select firstonly DirCategory from dimAttributeDirCategory
            where dimAttributeDirCategory.DimensionAttribute == dimensionAttribute.RecId;

        sysTableLookup = SysTableLookup::newParameters(tableNum(DimensionFinancialTag), _control);

        sysTableLookup.addLookupfield(fieldNum(DimensionFinancialTag, Value));
        sysTableLookup.addLookupfield(fieldNum(DimensionFinancialTag, Description));

        query = new Query();
        query.addDataSource(tableNum(DimensionFinancialTag)).

        addRange(fieldNum(DimensionFinancialTag, FinancialTagCategory)).

        value(queryValue(dimAttributeDirCategory.DirCategory));

        sysTableLookup.parmQuery(query);

        // Perform the lookup.
        sysTableLookup.performFormLookup();

       }
}



How to get Financial Dimension value from Worker Position in AX / D365FO / AX 7

Here is the sample X++ code to find financial dimension value from the worker's primary position.

public Name getDimensionValue(HcmWorkerRecId _workerRecId, Name _DimensionName)
{
        HcmPositionDefaultDimension             hcmPositionDefaultDimension;
        DefaultDimensionView                    dimensionView;
        DimensionAttributeValueSet              DimensionAttributeValueSet;
        DimensionAttributeValueSetItem          DimensionAttributeValueSetItem;
        DimensionAttributeValue                 DimensionAttributeValue;
        DimensionAttribute                      DimensionAttribute;
       
        select HcmPositionDefaultDimension where
            HcmPositionDefaultDimension.Position == HcmWorker::getPrimaryPosition(_workerRecId);

        select RecId from DimensionAttributeValueSet
           where  DimensionAttributeValueSet.RecId == HcmPositionDefaultDimension.DefaultDimension
           join RecId, DisplayValue, DimensionAttributeValueSet from DimensionAttributeValueSetItem
           where DimensionAttributeValueSetItem.DimensionAttributeValueSet == DimensionAttributeValueSet.RecId
           join RecId from DimensionAttributeValue
           where DimensionAttributeValue.RecId == DimensionAttributeValueSetItem.DimensionAttributeValue
           join RecId, Name, ValueAttribute from DimensionAttribute
           where DimensionAttribute.RecId == DimensionAttributeValue.DimensionAttribute
                && DimensionAttribute.Name == _DimensionName;

        select dimensionView
            where dimensionView.DefaultDimension == HcmPositionDefaultDimension.DefaultDimension
                && dimensionView.Name == _DimensionName
                && dimensionView.DisplayValue == DimensionAttributeValueSetItem.DisplayValue
                && dimensionView.DimensionAttributeId == DimensionAttribute.RecId;

        return dimensionView.dimensionDiscription();
       
}

Friday, 27 July 2018

How to get multiple selected records in control level event handlers in D365FO / AX 7

Purpose:
The purpose of this document is to show how to get multiple selected records in form control event handlers. 

Development:
First of all create new event handler class HRPayrollPayStatementEventHandler and subscribe to form button OnClicked event handler.

/// <summary>
/// The <c>HRPayrollPayStatementEventHandler</c> class is the event handler class for managing PayrollPayStatement form events
/// </summary>
class HRPayrollPayStatementEventHandler
{
   

    /// <summary>
    /// Click event handler
    /// </summary>
    /// <param name="_sender">Form control buffer</param>
    /// <param name="_e"> Event args</param>
    [FormControlEventHandler(formControlStr(PayrollPayStatement, CustomButton), FormControlEventType::Clicked)]
    public static void SLD_CustomButton_OnClicked(FormControl _sender, FormControlEventArgs _e)
    {
        FormDataSource    PayrollPayStatement_DS = _sender.formRun().dataSource(formDataSourceStr(PayrollPayStatement, PayrollPayStatement));

        MultiSelectionHelper    selectionHelper = MultiSelectionHelper::construct();
        PayrollPayStatement     payStatement;

        selectionHelper.parmDataSource(PayrollPayStatement_DS);
        payStatement  = selectionHelper.getFirst();

        if (payStatement.RecId)
        {
            while (payStatement.RecId != 0)
            { 
                info (payStatement.DocumentNumber);

                payStatement = selectionHelper.getNext();
            }          
        }
    }
}

Tuesday, 17 July 2018

D365 / AX 7 - How to override form control methods

Purpose:
The purpose of this document is to show how to override form control methods without overlaying in Dynamics 365 for Operations

Development:
To demonstrate we are using the HRMCompFixedEmplActionDialog form and going to override the planId_Control lookup method.

First of all, create new event handler class HRMCompFixedEmplActionDialogEventHandler and subscribe to post-event handler of form init.

/// <summary>
/// The <c>HRMCompFixedEmplActionDialogEventHandler</c> class is the event handler class
/// for managing HRMCompFixedEmplActionDialog form events
/// </summary>
class HRMCompFixedEmplActionDialogEventHandler
{     

    /// <summary>
    ///  Post event handler for HRMCompFixedEmplActionDialog
    /// </summary>
    /// <param name="_args">Event args</param>
    [PostHandlerFor(formStr(HRMCompFixedEmplActionDialog), formMethodStr(HRMCompFixedEmplActionDialog, init))]
    public static void HRMCompFixedEmplActionDialog_Post_init(XppPrePostArgs _args)
    {
        FormRun formRun = _args.getThis();

        var methodOverrides = HRMCompFixedEmplActionDialogEventHandler::construct();

        FormStringControl PlanIdctrl = formRun.design().controlName(formControlStr(HRMCompFixedEmplActionDialog, planId_Control));
        PlanIdctrl.registerOverrideMethod(methodStr(FormStringControl, lookup), methodStr(HRMCompFixedEmplActionDialogEventHandler, LookupPlanId ), methodOverrides );
    }

    /// <summary>
    /// Constructs a new instance of <c>HRMCompFixedEmplActionDialogEventHandler</c> class.
    /// </summary>
    /// <returns>
    /// A <c>HRMCompFixedEmplActionDialogEventHandler</c> class.
    /// </returns>
    public static HRMCompFixedEmplActionDialogEventHandler construct()
    {
        return new HRMCompFixedEmplActionDialogEventHandler();
    }

    /// <summary>
    ///    Overrided method for Plan id lookup
    /// </summary>
    /// <param name="_ctrl">
    /// FormStringControl buffer
    /// </param>
    public void LookupPlanId(FormStringControl _ctrl)
    {
        SysTableLookup      sysTableLookup = SysTableLookup::newParameters(tableNum(HRMCompFixedPlanTable), _ctrl);
        Query               query;

        QueryBuildDataSource qbds = query.addDataSource(tablenum(HRMCompFixedPlanTable));

        sysTableLookup.addLookupfield(fieldNum(HRMCompFixedPlanTable, PlanId));
        sysTableLookup.addLookupfield(fieldNum(HRMCompFixedPlanTable, Description));
        sysTableLookup.addLookupfield(fieldNum(HRMCompFixedPlanTable, Type));
        sysTableLookup.addLookupfield(fieldNum(HRMCompFixedPlanTable, CurrencyCode));
        sysTableLookup.addLookupfield(fieldNum(HRMCompFixedPlanTable, PayFrequencyId));

        sysTableLookup.parmQuery(query);
        sysTableLookup.performFormLookup();
    }

}



Using this the approach we can override any form of control methods

Wednesday, 30 May 2018

Create custom data entity in AX / D365FO / AX 7

Purpose:
The purpose of this document is to show how to create custom data entity for custom table in Dynamics 365 for Operations.

Business requirement:
To perform data migration operations on custom table

Development:
Creating data entity by using data entity wizard.

Step #1 To demonstrate we are creating data entity for custom table HREvaluationProcess.
  1. Right click on your project and then click on Add > New item 
  2. Select Data Entity, and then set the Name property to HREvaluationProcessEntity
  3. Now set data entity properties. Select HREvaluationProcess as Primary datasource and Click Next.
  4. Now add fields to the entity from data source and click Finish. Data entity and staging table are added to the project.
  5. Build and sync the project.

Creating data entity by using addins























Consume data entity.











Adding custom reference field to Data Entity Extension in D365 / AX 7


 Purpose:
The purpose of this document is to show how to add a custom reference field in the existing data entity

Business requirement:
Adding custom reference field to the existing data entity. 

Development:
To demonstrate we have added compensation level reference in HcmPositionDetail table. We need to add this field in the existing data entity HcmPositionDetailEntity


Step #1 Creating HcmPositionDetailEntity entity extension

















Step #2 In order to add the reference field we need to add the reference data source first and creates the relation

Here in our case the data source we need to add is ‘HcmCompensationLevel’.





Step #3 Creating extension of data entity staging table. Here the staging table  is HcmPositionDetailStaging























Step #4 Adding custom field to the staging table. 

Step #5 Now build and sync the project

Step #6 You have now successfully added custom field. You can verify if the custom field added or not go to

Workspaces > Data Management > Data Entities > Search data entity > Click on Modify Target Mapping












Run AX report using X++

       Args                     args;      ReportRun          report;     salesLineProductPartProd salesLineProductPartProdLocal;     ;     ...