Calling Static methods in EP AX

 Table static methods :

boolean   exists = (bool)AxSession.AxaptaAdapter.CallStaticRecordMethod(“CustTable”, “exist”, CustAccount.Text);

 Class static methods:

int i = (int)AxSession.AxaptaAdapter.CallStaticClassMethod(“Example”, “triple”, Convert.ToInt16(IntValue.Text));

Refresh Parent Datasource when called from window.open command in EP AX

Use this code in child close button . It will refresh the Parent Page,

ScriptManager.RegisterStartupScript(this, this.GetType(), “CloseWindow”, “window.close(); if (window.opener && !window.opener.closed) { window.opener.location.reload(); }”, true);

Passing Complex Query to Lookup in c# EP

protected void Unit_LookUp(object sender, AxLookupEventArgs e)

{

AxLookup lookup = e.LookupControl;

// to bring query from the dataset

object queryObj = this.dsEMSSubstanceFlowCreate.GetDataSet().DataSetRun.AxaptaObjectAdapter.Call(“getUnitOfMeasureConversionDataSource”);

//Passing query

using (ApplicationProxy.Query appQuery = new ApplicationProxy.Query(queryObj))

{

 

//Create the lookup dataset – we will do a lookup in the UnitOfMeasure table

using (SysDataSetBuilder sysDataSetBuilder = SysDataSetBuilder.constructLookupDataSet(this.AxSession.AxaptaAdapter, TableMetadata.TableNum(this.AxSession, “UnitOfMeasure”)))

{

// Set the run time generated data set as the lookup data set

lookup.LookupDataSet = new Microsoft.Dynamics.AX.Framework.Portal.Data.DataSet(this.AxSession, sysDataSetBuilder.toDataSet());

lookup.LookupDataSet.DataSetViews[0].MasterDataSource.query(new Query(this.AxSession.AxaptaAdapter,

ApplicationProxyHelper.CastToObjectAdapter(this.AxSession, appQuery)));

}

}

lookup.LookupDataSet.Init();

// Specify the lookup fields used

lookup.Fields.Add(AxBoundFieldFactory.Create(this.AxSession, lookup.LookupDataSetViewMetadata.ViewFields[“Symbol”]));

 

// Specify the select field

lookup.SelectField = “Symbol”;

 

lookup.DefaultLookupGrid.ShowFilter = false;

}

Lookups in EP

  1. Simple Lookup :- (DataSet Level)

In Dataset –>Datasource–>Table–>Fields–>method

void dataSetLookup(SysDataSetLookup sysDataSetLookup)
{

Query qr = new Query();

qr.addDatasource(tablenum(CustTable));

 sysDataSetLookup.parmLookupFields(new List(Types::String));

sysDataSetLookup.parmLookupFields().addEnd(fieldStr(CustTable, AccountNum));
sysDataSetLookup.parmLookupFields().addEnd(fieldStr(CustTable, CustGroup));

sysDataSetLookup.parmQuery(query);

}

[OR]

void dataSetLookup(SysDataSetLookup sysDataSetLookup)

{

    List list = new List(Types::String);

    Query query = new Query();

    QueryBuildDataSource    queryBuildDataSource;

    QueryBuildRange qbr;

    // Add the table to the query.

    queryBuildDataSource  = query.addDataSource(tableNum(CustTable));

//add the range

    qbr = queryBuildDataSource.addRange( fieldnum(CustTable, AccountNum));

    qbr.value(“1234”);

    qbr.status(RangeStatus::Locked);

     // Specify the fields to use for the lookup.

    list.addEnd(fieldStr(CustTable,AccountNum));

    list.addEnd(fieldStr(CustTable,CustGroup));

    list.addEnd(fieldStr(CustTable,Currency));

     sysDataSetLookup.parmLookupFields(list);

     // Specify the field that is returned from the lookup.

    sysDataSetLookup.parmSelectField(‘AccountNum’);

     // Pass the query to the SysDataSetLookup so that the query is used.

    sysDataSetLookup.parmQuery(query);

}

2. Lookup in Visual Studio

 OnLookup=”ContactForParty_Lookup”
AutoPostBack=”True” LookupButtonDisplaySettings=”Always”>

protected void ContactForParty_Lookup(object sender, AxLookupEventArgs e)
{
AxLookup lookup = e.LookupControl;
DataControlField lookupField;

try
{
lookup.LookupDataSet = new Microsoft.Dynamics.AX.Framework.Portal.Data.DataSet(this.AxSession, “MzkContactPersonLookup”); // MzkContactPersonLookup – dataset contain 3 tables

// DataSet has to be init’ed before accessing the data sources
lookup.LookupDataSet.Init();

//adding ranges to the query Dataset
using (Proxy.Query query = lookup.LookupDataSet.DataSetViews[0].MasterDataSource.query())
{
using (Proxy.QueryBuildDataSource dataSource = query.dataSourceNo(1))
{
using (Proxy.QueryBuildRange range = dataSource.addRange(
TableDataFieldMetadata.FieldNum(this.AxSession, “ContactPerson”, “ContactForParty”)))
{
//AxBoundField VendAccount = (AxBoundField)GetField(this.DealerVendGrp.Fields, “DealerVendAccount”);

range.value = Convert.ToString(this.Mzk_EPSubDevicesDS.GetDataSet().DataSetRun.AxaptaObjectAdapter.Call(“getVendParty”));

}
}
}
// Add fields to the lookup
lookupField = AxBoundFieldFactory.Create(lookup.LookupDataSetViewMetadata.ViewFields[“ContactPersonId”]);
lookupField.SortExpression = “ContactPersonId”;
lookup.Fields.Add(lookupField);

lookupField = AxBoundFieldFactory.Create(lookup.LookupDataSetViewMetadata.ViewFields[“DirPartyLookupGridView!Name”]);
lookupField.SortExpression = “DirPartyLookupGridView!Name”;
lookup.Fields.Add(lookupField);

lookupField = AxBoundFieldFactory.Create(lookup.LookupDataSetViewMetadata.ViewFields[“ContactForParty”]);
lookupField.SortExpression = “ContactForParty”;
lookup.Fields.Add(lookupField);

lookup.LookupDataSet.DataSetViews[0].Sort = “ContactPersonId, DirPartyLookupGridView!Name”;

lookup.SelectField = “DirPartyLookupGridView!Name”;
}
catch (System.Exception ex)
{
AxExceptionCategory exceptionCategory;
// This returns true if the exception can be handled here
if (!AxControlExceptionHandler.TryHandleException(this, ex, out exceptionCategory))
{
// The exception is system fatal – in this case we re-throw.
throw;
}
}
}

[OR]

For using SysDataSetBuilder , use the below namespace

                     using Microsoft.Dynamics.Framework.BusinessConnector.Proxy;

protected void LookupName_Lookup(object sender, AxLookupEventArgs e)

    {

        AxLookup xLookup = e.LookupControl;

        using (Proxy.SysDataSetBuilder mydataSetBuilder = Proxy.SysDataSetBuilder.constructLookupDataSet(this.AxSession.AxaptaAdapter,

                        TableMetadata.TableNum(this.AxSession, “DirPersonName”)))

        {

            xLookup.LookupDataSet = new Microsoft.Dynamics.AX.Framework.Portal.Data.DataSet(this.AxSession, mydataSetBuilder.toDataSet());

        }

        xLookup.LookupDataSet.Init();

        // add fields

        xLookup.Fields.Add(AxBoundFieldFactory.Create(this.AxSession, xLookup.LookupDataSetViewMetadata.ViewFields[“FirstName”]));

        xLookup.Fields.Add(AxBoundFieldFactory.Create(this.AxSession, xLookup.LookupDataSetViewMetadata.ViewFields[“LastName”]));

        xLookup.SelectField = “FirstName”;

        //TextBox1.Text = xLookup.SelectField;

    }

Validate Page without the default button clicked in EP

bool ret = true;

try

{

this.Page.Validate();   //Validate the page

ret = this.Page.IsValid;

if (ret)

{

this.AxForm1.InsertItem(true);

this.AxDataSource1.GetDataSourceView(“CustTable”).RefreshData(true);

}

}

catch (Exception ex)

{

ret = false;

}

Connecting External Database from X++

Before starting few configuration need to set :- 1. Create DSN : To create a Data Source Name (DSN) go to Administrative Tools > Data Sources (ODBC). click add select Sql server native client – provide server name -Next- click on  change default Database and select the database “MyExternalDB”.   2. CODE

// X++, Main method in a class.
static public void Main(Args _args)
{
    LoginProperty loginProperty;
    OdbcConnection odbcConnection;
    Statement statement;
    ResultSet resultSet;
    str sql, criteria;
    SqlStatementExecutePermission perm;
    ;

    // Set the information on the ODBC.
    loginProperty = new LoginProperty();
    loginProperty.setDSN("dsnName");
    loginProperty.setDatabase("MyExternalDB");

    //Create a connection to external database.
    odbcConnection = new OdbcConnection(loginProperty);

    if (odbcConnection)
    {
        sql = "SELECT * FROM MYTABLE WHERE FIELD = "
            + criteria
            + " ORDER BY FIELD1, FIELD2 ASC ;";

        //Assert permission for executing the sql string.
        perm = new SqlStatementExecutePermission(sql);
        perm.assert();

        //Prepare the sql statement.
        statement = odbcConnection.createStatement();
        resultSet = statement.executeQuery(sql);

        //Cause the sql statement to run,
        //then loop through each row in the result.
        while (resultSet.next())
        {
            //It is not possible to get field 3 and then 1.
            //Always get fields in numerical order, such as 1 then 2 the 3 etc.
            print resultSet.getString(1);
            print resultSet.getString(3);
        }

        //Close the connection.
        resultSet.close();
        statement.close();
    }
    else
    {
        error("Failed to log on to the database through ODBC.");
    }
}

Using AX Classes in EP

Using Proxies :-

For first time using classes :

1. Start Visual Studio, and click File > New Project.
2. In the project types window, select Visual C#.
3. In the Visual Studio installed templates section, select Class Library.
4. Click File > Add <project name> to Dynamics AX.
5. In Solution Explorer, select the project, and set the Deploy to EP property of the project to Proxies.
6. In Solution Explorer, right-click the project, click Properties, and then set the Default namespace property to Microsoft.Dynamics.Portal.Application.Proxy.
7. Delete the Class1.cs file that comes with the ClassLibrary project.
8. Click View > Application Explorer, right-click the object for which you need a proxy, such as tables, EDTs, enums, or classes, and then click Add to Project.
9. Save this project, and then add a reference to this project in your Enterprise Portal web control project.

In Web control :-

add namespace:

using Proxy = Microsoft.Dynamics.Framework.BusinessConnector.Proxy;
using ApplicationProxy = Microsoft.Dynamics.Portal.Application.Proxy;

 

calling methods :

ApplicationProxy .CustTransDetails  custDt =  new ApplicationProxy .CustTransDetails();

custDt.method(input);