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;

}

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);

Calling AX Methods in EP

Calling Methods defined in the Dataset :

 object ob = this.RequestEdit_DS.GetDataSet().DataSetRun.AxaptaObjectAdapter.Call(“getValue”, dtlReqType);

string s = (string)this.RequestEdit_DS.GetDataSet().DataSetRun.AxaptaObjectAdapter.Call(“getValue”, dtlReqType);

 

EP :- To get the AX Session Id, User Id , Company

Declare namespace :

Using Microsoft.Dynamics.Framework.BusinessConnector.Session;

 

Two ways :

For Current UserId :

string userid  = Microsoft.Dynamics.Framework.Portal.SessionHelper.Instance.GetSession().SessionInfo.UserId;

For SessionId :

int sessionId  = Microsoft.Dynamics.Framework.Portal.SessionHelper.Instance.GetSession().SessionInfo.SessionId;

For Company :

string comp = Microsoft.Dynamics.Framework.Portal.SessionHelper.Instance.GetSession().SessionInfo.Company;

Second Way :

new method:-

private ISession AxSession
{
get
{
AxBaseWebPart webpart = AxBaseWebPart.GetWebpart(this);
return webpart == null ? null : webpart.Session;
}
}

& call the AXSession to get info

string userid  =  this.AxSession.SessionInfo.UserId;

int sessionId  = this.AxSession.SessionInfo.SessionId;

string comp   =  this.AxSession.SessionInfo.Company;