02 August 2017

Customer postal address Primary address X++ code AX 2012 / Logistics postal address city, state and zip code

public LogisticsPostalAddress getPostalAddressByType(DirPartyRecId _party, LogisticsLocationRoleType _type)
{

DirPartyLocation partyLocation;
DirPartyLocationRole partyLocationRole;
LogisticsLocation location;
LogisticsLocationRole locationRole;
LogisticsPostalAddress postalAddress;
LogisticsElectronicAddress electronicAddress;

select firstonly postalAddress
exists join location
where location.RecId == postalAddress.Location
exists join locationRole
where locationRole.Type == _type
exists join partyLocation
where partyLocation.Location == location.RecId &&
partyLocation.Party == _party &&
partyLocation.IsPrimary == NoYes::Yes
exists join partyLocationRole
where partyLocationRole.PartyLocation == partyLocation.RecId &&
partyLocationRole.LocationRole == locationRole.RecId;

return postalAddress;
}

21 June 2017

Event Handlers in Dynamics AX 7 and Dynamics AX 365 - D365

If you want to create a new event handler then have to create a class and define.

Example:
How to get formRun or element in the event handler in D365

 [PostHandlerFor(formStr(AssetTrans), formMethodStr(AssetTrans, init))]
    public static void AssetTrans_Post_init(XppPrePostArgs args)
    {
        FormRun                     formRunElement;
        FormDataSource              formDataSource;
        CustomTableFixedAssetProfileTmp    assetProfileTmp;
        QueryBuildDataSource        dataSource;

        formRunElement = args.getThis();

        if( formRunElement.args().callerName() == formStr(HHD_FixedAssetProfile))
        {           
            assetProfileTmp = formRunElement.args().record();// formRun calles the args which is contains the record of caller form
            formDataSource  = formRunElement.dataSource();
            dataSource      = formDataSource.query().dataSourceTable(tableNum(AssetTrans));
            dataSource.addRange(fieldNum(AssetTrans,BookId)).value(assetProfileTmp.BookId);
            dataSource.addRange(fieldNum(AssetTrans,AssetId)).value(assetProfileTmp.AssetId);
        }
    }

class VendTableHandler
{
[PostHandlerFor(tableStr(VendTable), tableMethodStr(VendTable, insert))] // Post even Handler

public static void VendTable_Post_insert(XppPrePostArgs _args)
{
VendTable vendTable = _args.getThis();

_args.setReturnValue(VendTable::exists(vendTable.AccountNum));
}

}

// For Pre event handler
// [PreHandlerFor(tableStr(VendTable), tableMethodStr(VendTable, insert))]

Note: once you created an event handler for a table method. You can see + symbol for the method left side.

25 May 2017

X++ code to get tax calculation in AX 2012 - Sales tax Line wise to get sales tax.

Tax::calcTaxAmount(salesLine.TaxGroup, salesLine.TaxItemGroup, Systemdateget(), salesLine.CurrencyCode, salesLine.LineAmount, TaxModuleType::Sales);


public TaxAmount taxValue1(SalesLine  _salesLine)
{
    TaxOnItem       TaxOnItem;
    TaxGroupData    TaxGroupData;
    real            TaxAmount;
    TaxValue        TaxValue;
    //SalesLine       SalesLine
    ;

    //select firstOnly from salesLine where salesLine.SalesId == 'YourSalesId';
    if(_salesLine.TaxItemGroup && _salesLine.TaxGroup && _salesLine.LineAmount != 0)
    {
        select TaxOnItem
            where TaxOnItem.TaxItemGroup == _salesLine.TaxItemGroup;      
            if(TaxOnItem)
            {
                select TaxGroupData
                    where TaxGroupData.TaxGroup == _salesLine.TaxGroup
                        && TaxGroupData.TaxCode  == TaxOnItem.TaxCode;
               
                if(TaxGroupData)
                {
                    TaxValue  =  TaxData::find(TaxOnItem.TaxCode, Systemdateget(), 0).TaxValue;
                    TaxAmount = (_salesLine.LineAmount * TaxValue) / 100;
                }
               
            }
       
    }
    return TaxValue;
}

X++ to to get financial dimension value using default dimension in AX 2012

#define.dimProductGroups("ProductGroups")
    #define.dimCostCenters("CostCenters")
    #define.dimDepartments("Departments")


private str getDimensionValue(RecId _dimensionDefault, Str dimName)
{
    DimensionAttributeValueSetStorage   dimStorage;
    Str                                 dimValue;
    Counter                             i;


    // DimensionDefault is a RecId that combines all Dimension Values
    dimStorage = DimensionAttributeValueSetStorage::find(_dimensionDefault);

    for (i= 1 ; i<= dimStorage.elements() ; i++)
    {
        if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == dimName)
        {
            dimValue = dimStorage.getDisplayValueByIndex(i);
        }
        if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == dimName)
        {
            dimValue = dimStorage.getDisplayValueByIndex(i);
        }
        if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == dimName)
        {
            dimValue = dimStorage.getDisplayValueByIndex(i);
        }
    }

    return dimValue;
}

Calling:
salesDeliveryLinesTmp.ProductGroup      =   this.getDimensionValue(custPackingSlipTrans.DefaultDimension,#dimProductGroups);
    salesDeliveryLinesTmp.CostCenter        =   this.getDimensionValue(custPackingSlipTrans.DefaultDimension,#dimCostCenters);
    salesDeliveryLinesTmp.Department        =   this.getDimensionValue(custPackingSlipTrans.DefaultDimension,#dimDepartments);

====
Using Dimension name getting default dimension getting dimension value;

private DimensionValue ceDimensionProductGroup(DimensionDefault _dimensionDefault)
{
    DimensionValue                      value;
    DimensionAttributeValueSetStorage   dimStorage;
    int i;

    dimStorage = DimensionAttributeValueSetStorage::find(_dimensionDefault);

    for (i=1; i<=dimStorage.elements(); i++)
    {
        if (DimensionAttribute::findByName(CustParameters::find().CeDimensionReportValue3).Name == DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name)
        {
            value = dimStorage.getDisplayValueByIndex(i);
        }
    }

    return value;
}



23 May 2017

SQL server 2012 BackUp database using compression query command


USE [MicrosoftDynamicsAX]
BACKUP DATABASE MicrosoftDynamicsAX
TO DISK = 'D:\Backup\Data\MicrosoftDynamicsAX_BackUp\MicrosoftDynamicsAX.bak'
WITH COPY_ONLY, COMPRESSION
GO


USE [MicrosoftDynamicsAX_model]
BACKUP DATABASE MicrosoftDynamicsAX_model
TO DISK = 'D:\Backup\Data\MicrosoftDynamicsAX_BackUp\MicrosoftDynamicsAX_model.bak'
WITH COPY_ONLY, COMPRESSION
GO

28 April 2017

How to get on hand inventory quantity or Available physical quantity in AX 2012

public InventQty onHandInventory(ItemId itemId, InventSiteId inventSiteId,InventLocationId inventLocation)
{
    InventOnhand    inventOnhand;
    InventDim       _inventDim;
    InventDimParm   inventDimParm;
    InventQty       availQty;
    //inventDim
    _inventDim.InventSiteId = inventSiteId;
    _inventDim.InventLocationId = inventLocation;
    inventDimParm.initFromInventDim(_inventDim);

    inventOnhand = inventOnhand::newParameters(itemId,_inventDim,inventDimParm);

    availQty = inventOnhand.availPhysical();
   // info(strFmt("%1",availQty));
    return availQty;

}

Service class to get the selected record and deleted matching records and refresh the form data source in D365 F&O

 [DataContractAttribute] class ABCUserProfilesBulkDeleteContract {         UserId userId;     [DataMemberAttribute('UserId')]     pu...