D365/AX7:Example Of Delegates For Solving Dependencies Between Models

Overview

Delegates in Microsoft Dynamics D365 are the methods for accessing elements in higher model  from a lower model & solving dependencies among models.Delegates serves as a contract between delegate instance & delegate handler.

For example

I have a scenario.I have 2 models i.e. Model A & Model B.In Model B i passed the reference of model A but vice-versa is not possible.It means i can call objects of model A in model B.But i cannot call objects of Model B in Model A.Here we can use delegates for accessing Model B values in Model A.

Sample Code & Explanation

  1. Create a Model A & create a delegate.Delegate method should be empty and void type.[code language = “cpp”]
    public class MyClassInModelA
    {
    delegate void myDelegate(EventHandlerResult _result)
    {
    }
    }
    [/code]
  2. Create a Model B & passed the reference of Model B using “Update Model Parameters”.It means i can call objects of model A.
  3. Create a class in Model B & write a delegate handler.[code language = “cpp”]
    class MyClassInModelB
    {
    [SubscribesTo(classStr(MyClassInModelA), delegateStr(MyClassInModelA, myDelegate))]
    public static void myDelegateHandler(EventHandlerResult _result)
    {
    str ret = “Hi!This is Model B Values”;
    _result.result(ret);
    }
    }
    [/code]
  4. Call the subscription of delegate in Model A & access the values of Model B in          Model A.In this way, i am calling Values of Model B in Model A without passing the reference of Model B in Model A.[code language = “cpp”]
    public class MyClassInModelA
    {

    delegate void myDelegate(EventHandlerResult _result)
    {
    }
    public static client server void main(Args args)
    {
    MyClassInModelA myClassInModelA = new MyClassInModelA();
    MyClassInModelA.myMethod();
    }
    public void myMethod()
    {
    EventHandlerResult result = new EventHandlerResult();
    this.myDelegate(result);
    str common = result.result();
    info(common);
    }
    }
    [/code]

D365/AX7:SQL IN Operator In X++ Where Clause (Enum Fields Only)

Purpose

The purpose is to demonstrate the use of SQL IN operator in X++.

Limitation

The use of IN operator in X++ is only applicable for fields of data type ENUM.

Sample Code

[code language = “cpp”]
SalesTable salesTable;
container con = [SalesType::Sales, SalesType::ReturnItem, SalesType::Subscription];
while select * from salesTable
where salesTable.SalesType in con
{
Info(salesTable.SalesId);
}
[/code]

D365/AX7:Connect To An External SQL Database Using X++

Requirement

Connect To An External SQL Database Using X++.

Sample Code

[code language = “cpp”]using System.Data.SqlClient;
class ACX
{
public static void main(Args _args)
{
SqlConnection conn = new SqlConnection(“Server=myIP; Database=myDatabase; User Id=username; Password=password; Min Pool Size = 5; Max Pool Size=100; “);
str sqlL = @”SELECT * FROM Master_Item”;
str update = “UPDATE master_item SET productcode = @newvalue WHERE productcode = @key”;
SqlCommand command = new SqlCommand(sqlL, conn);
SqlCommand updateCmd = new SqlCommand(update, conn); System.Data.SqlClient.SqlDataReader reader; System.Data.SqlClient.SqlParameterCollection parameterCollection; new InteropPermission( InteropKind::ClrInterop ).assert();
try
{
conn.Open();
try
{
reader = command.ExecuteReader();
while (reader.Read())
{
str prdcode = reader.get_Item(“ProductCode”);
info(prdcode);
//parameterCollection = command.get_Parameters();
//parameterCollection.AddWithValue(“@DATAAREAID”, “CEE”);
//if (needToUpdate)
//update=”UPDATE master_item SET productcode = ‘” + newvalue + “‘ WHERE productcode = ‘” + prdcode + “‘”;
//updateCmd.CommandText=update
//updateCmd.ExecuteNonQuery();
}
reader.Dispose();
}
catch
{
reader.Dispose();
}
catch(Exception::CLRError)
{
reader.Dispose();
}
conn.Dispose();
}
catch
{
conn.Dispose();
}
catch(Exception::CLRError)
{
conn.Dispose();
}
command.Dispose();
CodeAccessPermission::revertAssert();
}
}
[/code]

 

 

D365/AX7:Print SSRS Report Directly To The Active Network Printer Using X++

Purpose

The purpose is to demonstrate the working code sample for printing SSRS Report Directly To The Active Network Printer Using X++.

Prerequisite

  1. Document Routing Agent should be enable.
  2. There should be atleast one active network printer.

Untitled

Please refer below link for installing the document routing:

https://docs.microsoft.com/en-us/dynamics365/unified-operations/dev-itpro/analytics/install-document-routing-agent

Steps & Sample Code

You can modify below sample code as per your need.This is just a prototype.

[code language = “cpp”]
SrsReportRunController reportRunController = new SrsReportRunController();
dataContract = new AcxInvoiceDataContract();
reportRunController.parmReportName(ssrsReportStr(AcxInvoiceReport,dgsCredit));
reportRunController.parmDialogCaption(“Print CREDIT NOTE”);
args.menuItemType(MenuItemType::Output);
args.menuItemName(identifierStr(AcxCreditReport));
reportRunController.parmArgs(args);
reportRunController.parmLoadFromSysLastValue(false);
reportRunController.parmShowDialog(false);
dataContract.parmInvoiceId(custInvoiceJourLocal.InvoiceId);
dataContract.parmCopyName(“Original for Recipient”);
reportRunController.parmReportContract().parmRdpContract(dataContract);
reportRunController.parmReportContract().parmPrintSettings().printMediumType(SRSPrintMediumType::Printer);

// here is i am selecting only first active printer but you can make it configurable
select firstonly netPrinters
where netPrinters.PrinterName == printname
&& netPrinters.Active == NoYesCombo::Yes ;
//here i am passing printer name
reportRunController.parmReportContract().parmPrintSettings().printerName(netPrinters.PrinterName);
reportRunController.parmExecutionMode((SysOperationExecutionMode::Synchronous));
reportRunController.startOperation();
[/code]

 

 

 

D365/AX7:Extend Standard Item lookup in Sales Order, Transfer Order, Inventory Journals etc

Requirement

Some new custom fields are created on extension of table InventTable.Customer wants to see these custom fields in the item lookup of sales order, transfer order & inventory journals etc.

Steps

  1. Create the extension of table InventTable and add your custom fields.
  2. Create the extension of view InventItemIdLookupSimpleView and add your custom fields.
  3. Create the extension of form InventItemIdLookupSimple and add your custom fields in the GRID of the form.
  4. Create the extension of class InventItemIdLookupSimple

[code language = “cpp”]
[ExtensionOf(classstr(InventItemIdLookupSimple))]
final class AcxInventItemIdLookup_Extension
{
protected void addStandardFields()
{
List lookupFields1 = lookupFields;
next addStandardFields() ;
lookupFields1.addEnd((fieldNum(InventItemIdLookupSimpleView, AcxItemCode)));// added my custom field
lookupFields = lookupFields1;
}
}
[/code]

 

 

D365/AX7: Add a new custom Field In Standard or Base Data Entity

Purpose

There are many data entities available in dynamics D365 Finance & Operations (Example: EcoResReleasedProductV2Entity, InventWarehouseEntity).Requirement of updating or inserting values in custom field of standard table by extending standard or base Data Entity are very common.The purpose of this post is Adding  a new custom Field In Standard or Base Data Entity (Example: EcoResReleasedProductV2Entity, InventWarehouseEntity).

Requirement

A new custom field needs to be created on InventLocation Table.The same field has to be added in base or standard data entity InventWarehouseEntity.User will be able to update the value of custom field value in InventLocationTable by using base or standard data entity InventWarehouseEntity.

Steps

  1. Create a new field in extension of table InventLocation.Untitled
  2. Add the new custom field created in Step 1 into the extension of data entity InventWarehouseEntity.
  3. Untitled
  4. Add the new custom field created in Step 1 into the extension of staging table data entity InventWarehouseEntity.Untitled
  5. Refresh Entity List https://allaboutdynamic.com/2018/06/12/d365configuration-key-not-enabled-for-the-entity-xxxxx/

cheers 🙂

piyush adhikari

D365/AX7:Exclude test packages/models(DemoDataSuite) from VSTS build output

Issue

Some X++ hotfixes related to model “DemoDataSuite” belongs to your one-box development machine only.It means you cannot deploy it on UAT or on production environment else you will get below error related to applicationfoundationformadaptor dependency

The running command stopped because the preference variable “ErrorActionPreference” or common parameter is set to Stop: Unable to resolve dependency ‘dynamicsax-applicationfoundationformadaptor’.

Solution

To exclude package “DemoDataSuite” or any other model from VSTS Or DevOps build output you can set the parameter or variable PackagingExclusions before queuing the build.Once the build will complete, deployable packages or artifacts will not contain excluded models.

Untitled