Home » Code Snippets

Category Archives: Code Snippets


Hold/ Focus Form Until Close

Modal forms in Dynamics AX

I recently i had a scenario, where i had to run a Survey in Dynamics AX which will appear when user open his Dynamics client. Which i Achieved by calling my form on Info class.

The problem arrived when User minimizing the Survey and keep using Dynamics , So i have to Implement a check to hold the Dynamics Until he fill and Submit the Survey.

To Achieve this I simply added this command in Run method of my Form.



public void run()
    // Execution will resume at this point, only after the user has closed the form.


This done the trick, Happy Daxing.



Restrict non-admin users to open multiple sessions in AX 2012

Copy Paste the Following Code in startupPost method of info class in AOT

void startupPost()

xSession session;
SysClientSessions SysClientSessions;
UserId currentUserId;
int counter;


currentUserId = curUserId();

if (!isSystemAdministrator())// Allow Admin User to login multiple time
while select SysClientSessions
where SysClientSessions.userId == currentUserId
&& SysClientSessions.clientType == 0
session = new xSession(SysClientSessions.SessionId, true);
if (session && session.userId())

if (counter >= 2)
Box::stop(“Already Logged-in : The same user id can’t log in twice.”);


Hide Export file format : SSRS Report Viewer using X++

Sample Code to hide the Export option from SSRS report viewer using x++



STEP 1: First We need to add the reference “Microsoft.ReportViewer.WinForms” in AOT


STEP 2:  Add the below code into the Form “SRSReportViewer’



public void hideExportButton()public void hideExportButton()


System.Windows.Controls.UIElementCollection children;    Microsoft.Reporting.WinForms.ReportViewer reportViewer;

children = _axreportViewer_control.get_Children();    reportViewer = children.get_Item(0);



STEP 3: Now call this code into the run method of this form with the desired report name



Result :









Emploee Lookup – Legal entity specific – Active Emploee

Sample Code to generate Employee Lookup . Which contains Active employee, Legal entity specific and here i have an extra filter to exclude workers starting from 00.

You can also use the same code to get the Terminated employee just by Slight change in the query .

public void lookup()
Query query = new Query();
QueryBuildDataSource queryBuildDataSource;
QueryBuildRange queryBuildRange;
SysTableLookup sysTableLookup;

QueryBuildDataSource queryBuildDataSource1;
RecId _LegalEntity;

//Create an instance of SysTableLookup with the form control passed in
sysTableLookup = SysTableLookup::newParameters(tablenum(HcmWorker), this);

//Add the fields to be shown in the lookup form
sysTableLookup.addLookupfield(fieldnum(HcmWorker, PersonnelNumber), false);

//create the query datasource
queryBuildDataSource = query.addDataSource(tablenum(HcmWorker));
queryBuildDataSource.addRange(fieldNum(HcmWorker, PersonnelNumber))

queryBuildDataSource1 = queryBuildDataSource.addDataSource(tableNum(HcmEmployment));
queryBuildDataSource1.addRange(fieldNum(HcmEmployment, LegalEntity))

//add the query to the lookup form

// Perform lookup


Check physical existance of file

One of many ways to check file existance on folder for

Client side :


And for Server side

System.IO.FileInfo   fileInfo;

new InteropPermission(InteropKind::ClrInterop).assert();

fileInfo = new System.IO.FileInfo(_fileName);

return fileInfo.get_Exists();

Run RDP based report from Code & passing parameter using Data contract

Bsic Code to run RDP based report using x++ and passing parameters to data contract.

SrsReportRunController controller = new SrsReportRunController();
VendSOAReportTmpContract rdpContract = new VendSOAReportTmpContract ();
SRSPrintDestinationSettings settings;