Category: EP Ax 2012

Method to write logic when Default Insert button clicked in EP


As we know in EP, insert and edit button will be generated automatically from the Ax form properties in C# design. Here is the requirement where i need to do some more process after the insertion.

Below is the sample code  :

//Insert code for CBL
public void formPersonalContactEdit_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
//check if insert is successful, then close dialog
if (e.AffectedRows == 1)
string totalseelcted = this.Category1.Text;


call the method in init()

protected void Page_init(object sender, EventArgs e)
this.AxForm2.ItemInserted += new DetailsViewInsertedEventHandler(formPersonalContactEdit_ItemInserted);


Passing values between URL in EP


Below code used in C# to pass the values between EP pages.

From A button click :

Response.Redirect(“URL?EmplId=” + EmplId);

Form B init : capture the passed record in c# page init method

protected void Page_init(object sender, EventArgs e)
_emplId = Request.QueryString[“EmplId”];
Proxy.Info objInfoLog = new Proxy.Info(this.AxSession.AxaptaAdapter);
objInfoLog.add(Proxy.Exception.Error, Emplid);


Passing the captured value back to the Data set which is used by form B :

protected void Page_init(object sender, EventArgs e)
this.AxDataSource1.CreatingDataSetRun += new EventHandler<CreatingDataSetRunEventArgs>(AxDataSource1_CreatingDataSetRun);

void AxDataSource1_CreatingDataSetRun(object sender, CreatingDataSetRunEventArgs e)
string parm;
e.DataSetRunArgs.parm = _emplId;

default code used in Ep

I)                   DLL

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Microsoft.Dynamics.Framework.Portal.UI.WebControls;

using Microsoft.Dynamics.Framework.Portal.UI.WebControls.WebParts;

using Proxy = Microsoft.Dynamics.Framework.BusinessConnector.Proxy;

using Microsoft.Dynamics.Framework.BusinessConnector.Session;

using Microsoft.Dynamics.Framework.BusinessConnector.Adapter;

using Microsoft.Dynamics.AX.Framework.Services.Client;

using Microsoft.Dynamics.Framework.Portal.UI;

using Microsoft.Dynamics.AX.Framework.Portal.Data;

//using ApplicationProxy = Microsoft.Dynamics.Portal.Application.Proxy;

using Microsoft.Dynamics.Framework.Portal;

II)                To get ax session ids:

private ISession AxSession




AxBaseWebPart webpart = AxBaseWebPart.GetWebpart(this);

return webpart == null ? null : webpart.Session;




III )  To get field value

#region Other functions

static AxBoundField GetField(DataControlFieldCollection fields, string name)


foreach (DataControlField field in fields)


AxBoundField boundField = field as AxBoundField;

if (boundField != null && String.Compare(boundField.DataField, name, true) == 0)

return boundField;


return null;



Example :

AxBoundField StartDate = (AxBoundField)GetField(this.AxGroup1.Fields, “StartDate”);

Useful links for EP development


Below links will be more useful for EP development.

Lookups in C#:

EP cook book:


Calling X++ methods and using in it c# :

Get look-up data based on other lookup in c# from two tables.


In below example, I have two tables  ‘Mzk_ExpManufacture’ & ‘Mzk_ExpTrainingMaster’.

AS per the selection for ‘mzk_manufactuire’ field the data should change in ‘Employer’ field. Second table is the set up table have data for these two fields. Based on the relation will get lookup of ’mzk_manufacture’ in 1st table, to get the data in ‘employer’ field from 2nd table fallow as below code.

Call this method on the field which we want to see filter, I.e.  filter and in properties make the Autopost back ‘true’ and ‘showlookupbutton’ as  ‘Allways’

call this menthd at control level in C# : OnLookyp =”Employer_lookup”

public void Employer_lookup(object sender, AxLookupEventArgs e)


DataSetViewRow record = this.AxDataSource1.GetDataSourceView(“Mzk_ExpManufacture”).DataSetView.GetCurrent();

string empployer = Convert.ToString(record.GetRecord().GetField(“Mzk_Manufacture”));

AxLookup employerlookup = e.LookupControl;



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


employerlookup.LookupDataSet = new DataSet(this.AxSession, sysDataSetBuilder.toDataSet());



Proxy.Query query = employerlookup.LookupDataSet.DataSetViews[0].MasterDataSource.query();

using (Proxy.QueryBuildDataSource qbds1 = query.dataSourceNo(1))


using (Proxy.QueryBuildRange qbr = qbds1.addRange(TableDataFieldMetadata.FieldNum(this.AxSession, “Mzk_ExpTrainingMaster”, “Mzk_Manufacture”)))


qbr.value = empployer;



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

employerlookup.SelectField = “Employer”;

employerlookup.CausesValidation = false;


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.






validation & info log in c#


This sample code gives the view of popups info logs in EP using C#

In below example, we need to throw info if the leave is already taken or for leave applicable for girls only. Code as follows:

public Boolean LeaveValidation(string Leavetypevalidate, Int64 EmplId)


AxBoundField StartDate = (AxBoundField)GetField(this.AxGroup1.Fields, “StartDate”);                 int validate;

//Getting the value from the static method written in X++

validate = (int)AxSession.AxaptaAdapter.CallStaticRecordMethod(“Mzk_LeaveHistory”, “LeaveValidate”, Leavetypevalidate, EmplId);

//initiating the inflog

Proxy.Info objInfoLog = new Proxy.Info(this.AxSession.AxaptaAdapter);

//EVEN If we are throwing info log, it will insert the record in C# if they click save, So we are making the button as disable and making it false at the time of info log

AxForm1.AutoGenerateInsertButton = true;

if (validate == 1)


//Info log to throw at top of EP Page

objInfoLog.add(Proxy.Exception.Error, “Leave already taken”);


AxForm1.AutoGenerateInsertButton = false;


else if (validate == 2)


objInfoLog.add(Proxy.Exception.Error, “Leave is applicable for Female Employees only”);

AxForm1.AutoGenerateInsertButton = false;


return true;


Calling Ax methods in C# and EP


Below are the sample codes to call the methods of X++ in C# from EP development,

//calling the modified method of the control in EP

public void getstartdate(object sender, EventArgs e)


string sdateAx;

sdateAx = (string)this.AxDataSource1.GetDataSet().DataSetRun.AxaptaObjectAdapter.Call(“modified”);    }

This method should be written under particular field in Data set in AOT. For that field only we need to assign this method in C# control. And put ‘Autopostback’ TRUE.

call this method on C# control : Onlookup = “getstartdate”.

//Calling static methods.

We can call the static methods in C# which are written at Table or at Class level only.

In below example, we are calling a static method which is written in table ‘Mzk_LeaveHistory’ table and the method name in ‘currentEmployeeId’. And we are catching the value which the method is returning for the validations in C#. Since we can’t do validations by using the code in X++.

Here, based on the login employee we need to validate the leave balance and validation.

public void LeaveBalanceValidation(object sender, AxBoundFieldDataChangedEventArgs e)


Int64 EmplId = (Int64)AxSession.AxaptaAdapter.CallStaticRecordMethod(“Mzk_LeaveHistory”, “currentEmployeeId”);


string LeavetypeBalance = Convert.ToString(e.Value);

//string Leavetypevalidate = Convert.ToString(DropDownList)e.BoundControl).SelectedValue);

string Leavetypevalidate = Convert.ToString(e.Value);

this.LeaveValidation(Leavetypevalidate, EmplId);

this.LeaveBalance(LeavetypeBalance, EmplId);

//  this.AxDataSource1.GetDataSet().DataSetRun.AxaptaObjectAdapter.Call(“modified”);


Need to call in controller in C# :  OnDataChanged = “LeaveBalanceValidation”

Leave Balance can come on the leave type they selected, we are calling a static method in the table by passing the employee id and leave type selected.

For more description, plebe fallow below link: