Alfasith AX

Alfasith AX
اللَّهُمَّ انْفَعْنِي بِمَا عَلَّمْتَنِي، وَعَلِّمْنِي مَا يَنْفَعُنِي، وَزِدْنِي عِلْمًا

Thursday, August 13, 2015

Dynamic query by linking 2 tables in AX

Hi,

Query by linking two tables

static void queryEmpDept(Args _args)

{
Query query;
QueryBuildDataSource queryBuildDataSource1,queryBuildDataSource2;
QueryBuildRange queryBuildRange;
QueryBuildLink queryBuildLink;
QueryRun queryRun;
DeptTrainingTable deptTrainingTable;
EmpTrainingTable empTrainingTable;
;
// Create a new query object
query = new Query();
// Add the first data source to the query
queryBuildDataSource1 = query.addDataSource(tablenum(DeptTrainingTable));
// Add the range to this first data source
queryBuildRange = queryBuildDataSource1.addRange(fieldnum(DeptTrainingTable, DeptId));
// Set the range
queryBuildRange.value("ID002");
// Add the second datasource to the first data source
queryBuildDataSource2 =queryBuildDataSource1.addDataSource(tablenum(EmpTrainingTable));
// Add the link from the child data source to the parent data
//source
queryBuildLink = queryBuildDataSource2.addLink(fieldnum(DeptTrainingTable,DeptId),fieldnum(EmpTrainingTable, DeptId));
// Create a new QueryRun object based on the query definition
queryRun = new QueryRun(query);
// Loop through all the records returned by the query
while (queryRun.next())
{
// Get the table data by using the get() method
deptTrainingTable = queryRun.get(tablenum(DeptTrainingTable));
empTrainingTable = queryRun.get(tablenum(EmpTrainingTable));
info (strfmt("Dept Id %1, Dept Name %2, Employee Id %3, Employee Name %4 ", deptTrainingTable.DeptId,deptTrainingTable.DeptName,empTrainingTable.EmpTrainingId,empTrainingTable.Name));
}
}

Regards,


Write a text file in AX

Hi,

static server void WriteTextFile(Args _args){TextIo file;// Using the @ before the filename// enables us to use single path// delimiters. If you don't use it// you will have to write the path like this:// "c:\\temp\\cars.txt"FileName filename = @"C\Desktop\Reports.txt"; //Here give your location where to saveEmpTrainingTable empTrainingTable;container con;
FileIoPermission permission;#File;try{    // Create the permission classpermission = new FileIoPermission(filename, #io_write);// Add a request for permission before new TextIo()permission.assert();// Create the TextIo objectfile = new TextIo(filename, #io_write);if (!file)
throw Exception::Error;
// Specify the delimitersfile.outRecordDelimiter(#delimiterCRLF);file.outFieldDelimiter(";");// Loop through the data sourcewhile select empTrainingTable
{// Empty the containercon = connull();// Set the data into the containercon = conins(con, 1, empTrainingTable.DeptId);con = conins(con, 2, empTrainingTable.EmpTrainingId);con = conins(con, 3, empTrainingTable.Name);con = conins(con, 4, empTrainingTable.Phone);con = conins(con, 5, empTrainingTable.Date);// Write the container to the filefile.writeExp(con);}}catch(Exception::Error)
{error("You do not have access to write the file to the selected folder");}// Revert the access privilegesCodeAccessPermission::revertAssert();}

Regards,

SQL code to upate one Legal entity banner to all the legal entity in D365

 Hi, update companyimage set  companyimage.Image  = companyimageA.Image  from  ( select Image from companyimage where dataAreaid = 'USF...