Tag Archives: Finance and Operations

D365 FO/AX7: Cross-company & Change-company for accessing Data for Multiple Companies Using X++

Introduction

In Microsoft Dynamics AX/D365 Finance and operation, users can setup multiple legal entities. By default in Microsoft Dynamics AX/D365 FO, all tables store data per company unless SaveDataPerCompany  property is not changed to NO. By dafault value of property SaveDataPerCompany  = Yes.Using crosscompany & changecompany keyword, you can construct a query to retrieve all records, regardless of the company you are currently logged into.You can fetch the all the company records using keyword -crosscompany and function – changecompany.

Change-Company

static void main()
{
  CustTable custTable;
  changeCompany('INMF') 
  {
    custTable = null;
    while select custTable
    {
       //custTable of company 'INMF'.
    }
  }
}

Cross-Company

static void main()
{
  CustTable custTable;
  while select crosscompany custTable
  {
      custTable of all the companies
  }
}

AX7/D365: JSON DeSerialization/Parsing Using FormJsonSerializer

Requirement:

Developer is consuming an external REST web-service in D365/X++ . Response is in the JSON format.  Developer needs to DeSerialize incoming JSON so that values can be used later in the system.

Sample Code:

  1. Create a contract class
    [DataContract]
    class JSONContract
    {
        String50 parmFirst, parmSecond;
        [
            DataMemberAttribute('parmFirst')
        ]
        public String50 parmFirst(String50 _parmFirst = parmFirst)
        {
            parmFirst = _parmFirst;
            return parmFirst;
        }
        [
            DataMemberAttribute('parmSecond')
        ]
        public String50 parmSecond(String50 _parmSecond = parmSecond)
        {
            parmSecond = _parmSecond;
            return parmSecond;
        }
    }
  2. Sample Class
    class RunnableClassABC
    {
        /// <summary>
        /// Runs the class with the specified arguments.
        /// </summary>
        /// <param name = "_args">The specified arguments.</param>
        public static void main(Args _args)
        {
            str             json;
            List            values = new List(Types::String);
            ListEnumerator  value;      
            json = '[{"FieldValues": [{"parmFirst": "ValueFirst", "parmSecond": "ValueSecond", "parmList":[{"parmFirst": "ValueFirst", "parmSecond": "ValueSecond"}] }]}]';
            // Deserializing Json
            values = FormJsonSerializer::deserializeCollection(classnum(List), json, Types::Class, classStr(JSONContract));
            value = values.getEnumerator();
            while(value.moveNext())
            {
                JSONContract    JSONContractCurrent = value.current();
                info(strFmt("%1    %2",JSONContractCurrent.parmFirst(), JSONContractCurrent.parmSecond()));
            }
        }
    }

D365/AX7: Create Inventory Movement Journal Using X++ Code

public static void main(Args _args)
{ 
InventTable inventTable;
InventSite inventSite;
InventJournalTrans inventJournalTrans;
InventDim inventDimFrom;
InventJournalTable inventJournalTable;
int lineNum;
InventParameters inventParameters = InventParameters::find();
inventJournalTable.clear();
inventJournalTable.initValue();
inventJournalTable.initFromInventJournalName(InventJournalName::find(inventParameters.MovementJournalNameId));
if (inventJournalTable.validateField(fieldNum(InventJournalTable, JournalNameId)))
{
inventJournalTable.modifiedField(fieldNum(InventJournalTable, JournalNameId));
inventJournalTable.JournalType = InventJournalType::Movement;
inventJournalTable.insert();
}

lineNum ++;
inventJournalTrans.clear();
inventJournalTrans.initValue();
inventJournalTrans.initFromInventJournalTable(inventJournalTable);
inventJournalTrans.TransDate = today();
inventJournalTrans.ItemId = '1104X';
inventJournalTrans.LineNum = lineNum;
inventJournalTrans.modifiedField(fieldNum(InventJournalTrans, ItemId));
inventDimFrom.clear();
inventDimFrom.initValue();
inventDimFrom.InventSiteId = 'Site 1';
inventDimFrom.InventLocationId = '11';
inventDimFrom = InventDim::findOrCreate(inventDimFrom);
inventJournalTrans.InventDimId = inventDimFrom.inventDimId;
inventJournalTrans.modifiedField(fieldNum(InventJournalTrans, inventDimId));
inventJournalTrans.Qty = 2;
inventJournalTrans.modifiedField(fieldNum(inventJournalTrans, Qty));
inventTable = InventTable::find(inventJournalTrans.ItemId);
inventJournalTrans.DefaultDimension = LedgerDimensionDefaultFacade::serviceMergeDefaultDimensions(inventTable.DefaultDimension, inventDimFrom.inventSite().DefaultDimension);
inventJournalTrans.JournalType = InventJournalType::Movement;
inventJournalTrans.insert();
}

D365/AX7: Calculate On Hand Stock Of Item Using X++ & Code

public static void main(Args _args)
{
InventDimParm inventDimParm;
InventDim inventDim; 
Qty availPhysicalCalculated; ;
inventDim.InventLocationId = 'MLT-WH';
inventDim = InventDim::findOrCreate(inventDim);
inventDimParm.initFromInventDim(InventDim::find(inventDim.inventDimId));
availPhysicalCalculated = InventSum::findSum("ALLOY",inventDim,inventDimParm).availPhysicalCalculated();
info(strfmt("availPhys:%1 ",availPhysicalCalculated));
}

D365/AX7: X++ Code To Create Item or Released Product Using Data Entity EcoResReleasedProductCreationV2Entity

Requirement

X++ Code To Create Item or Released Production Using Data Entity EcoResReleasedProductCreationV2Entity

Sample Code

class CreateReleasedProduct
{ 
/// <summary>
/// Runs the class with the specified arguments.
/// </summary>
/// <param name = "_args">The specified arguments.</param>
public static void main(Args _args)
{ 
EcoResReleasedProductCreationV2Entity ecoResReleasedProductCreationV2Entity;
;

ecoResReleasedProductCreationV2Entity.clear();
ecoResReleasedProductCreationV2Entity.initValue();
ecoResReleasedProductCreationV2Entity.ItemNumber = '1104X';
ecoResReleasedProductCreationV2Entity.ItemModelGroupId = 'Stocked';
ecoResReleasedProductCreationV2Entity.InventoryUnitSymbol = 'ea';
ecoResReleasedProductCreationV2Entity.BOMUnitSymbol = 'ea';
ecoResReleasedProductCreationV2Entity.ProductGroupId = 'FG';
ecoResReleasedProductCreationV2Entity.ProductType = EcoResProductType::Item;
ecoResReleasedProductCreationV2Entity.ProductNumber = '1104X';
ecoResReleasedProductCreationV2Entity.ProductSearchName = '1104X';
ecoResReleasedProductCreationV2Entity.ProductName = '1104X';
ecoResReleasedProductCreationV2Entity.TrackingDimensionGroupName = 'None';
ecoResReleasedProductCreationV2Entity.ProductSubType = EcoResProductSubtype::Product;
ecoResReleasedProductCreationV2Entity.SearchName = '1104X';
ecoResReleasedProductCreationV2Entity.SalesUnitSymbol = 'ea';
ecoResReleasedProductCreationV2Entity.StorageDimensionGroupName = 'SiteWH';
ecoResReleasedProductCreationV2Entity.PurchaseUnitSymbol = 'ea';
ecoResReleasedProductCreationV2Entity.insert();

}

}

D365/AX7: Inventory Registration & Un-Registration Using X++ Class InventTransWMS_register

Hey Visitors,

Here is the sample code for Inventory Registration & Un-Registration Using X++ Class InventTransWMS_register.

cheers,

piyush adhikari – +91-7995802472

public static void operations(PurchId _purchId)
{
PurchLine purchLine;
InventTrans inventTrans;
InventDim inventDim;
InventTransWMS_Register inventTransWMS_register;
TmpInventTransWMS tmpInventTransWMS;
;

ttsbegin;
while select RecId, InventTransId from purchLine
where purchLine.PurchId == _purchId
&& purchLine.IsDeleted == NoYes::No
&& purchLine.PurchQty > 0
{
inventTrans = InventTrans::findTransId(purchLine.InventTransId);
if(inventTrans && inventTrans.StatusReceipt != StatusReceipt::Registered)
{
inventDim = inventTrans.inventDim();
tmpInventTransWMS.clear();
tmpInventTransWMS.initFromInventTrans(inventTrans);
tmpInventTransWMS.InventQty = inventTrans.Qty;
tmpInventTransWMS.InventDimId = inventDim.inventDimId;
tmpInventTransWMS.insert();

inventTransWMS_register = inventTransWMS_register::newStandard(tmpInventTransWMS);
inventTransWMS_Register.createFromInventTrans(inventTrans, inventDim);
inventTransWMS_register.writeTmpInventTransWMS(tmpInventTransWMS, inventTrans, inventDim);
inventTransWMS_register.updateInvent(inventTrans);
}
}
ttscommit;
}



D365/AX7: Create A Batch Job Using SysOperation Framwork

  1. Create a data contract class  
    
    [DataContract]
    class AcxSysOperationContract
    { 
    PurchId purchId; 
    [DataMemberAttribute("PurchId")]
    public PurchId parmpurchId(PurchId _purchId = purchId)
    {
    purchId = _purchId;
    return _purchId;
    }
    
    }
  2. Create a service class where you will place your custom business logic
    
    class AcxSysOperationService
    {
    public void operation(AcxSysOperationContract _dataContract)
    {
    info(_dataContract.parmpurchId());
    //your business logic
    }
    
    }
  3. Create a controller class
    
    class AcxSysOperationController extends SysOperationServiceController
    {
    public static void main(Args args)
    {
    AcxSysOperationController sysOperationController = new AcxSysOperationController();
    sysOperationController.parmClassName(classStr(AcxSysOperationService));
    sysOperationController.parmMethodName(methodStr(AcxSysOperationService, operation));
    sysOperationController.parmExecutionMode(SysOperationExecutionMode::Synchronous);
    sysOperationController.startOperation();
    }
    
    }

D365/AX7:Get Parameters (DataEntityRuntimeContext/DataEntityRuntimeContext) in Data Entity Events

[DataEventHandler(tableStr(AcxBrandEntity), DataEventType::MappingEntityToDataSource)]
public static void AcxBrandEntity_onMappingEntityToDataSource(Common _sender, DataEventArgs _eventArgs)
{
DataEntityContextEventArgs dataEntityContextEventArgs = _eventArgs;
DataEntityRuntimeContext runtimeContext = dataEntityContextEventArgs.parmEntityContext();
DataEntityDataSourceRuntimeContext DataEntityDataSourceRuntimeContext = dataEntityContextEventArgs.parmEntityDataSourceContext();

}

Get Parameters (DataEntityRuntimeContext/DataEntityRuntimeContext) in Data Entity Events

D365/AX7: Create Repeated Header In Header Line Data Entity (Bypass Insert of Data Entity Header Table)

D365/AX7: Create Repeated Header In Header Line Data Entity (Bypass Insert of Data Entity Header Table)

_dataSourceCtx.setBuffer(retailTransctionTableLocal);
_dataSourceCtx.setDataSaved(true);
_dataSourceCtx.setDatabaseOperation(DataEntityDatabaseOperation::Update);