Search This Blog

Wednesday, August 24, 2011

Multi Selection Records in Grid

In Forms we will have the Grid with multiple records, so that we want to process the multiple records at the button click, do the following procedure.

Select button or menubutton and go to properties window and select multiselect=yes.

In Click of the button write the following code

void clicked()
{
      CustTable   custTable;
      ;
      super();
      if(CustTable_ds.anyMarked())
      {
              custTable= CustTable_Ds.getFirst(1,false);
              while(custTable)
              {
                  info(custTable.AccountNum);
                  custTable= CustTable_Ds.getNext();
               }
       }
}

Time Comparision using Dialog objects

The following sample code used to do Time values comparison and Time field values comparison using Dialog.

 static void TimeComparisonWithDialog(Args _args)
 {
  TimeofDay fromTime,toTime;

  Dialog dlg = new Dialog("TimeComparision");
  dialogField dlgField,dlgField2;
  TimeofDay fTime,tTime;

   ;
  dlgField = dlg.addFieldValue(typeid(TimeofDay),"","FromTime");
  dlgField2 = dlg.addFieldValue(typeid(TimeofDay),"ToTime","ToTime");

   if(dlg.run())
   {
       fromTime = dlgField.value();
       toTime = dlgField2.value();
    }
//str2Time("") this function is used to convet time into string.   if(fromTime < toTime)
  {
       info("This is the small time");
       info(time2str(fromTime,1,1));
  }
  else if(fromTime > toTime)
  {
       info("From Time should be less than To time ");
       info(time2str(toTime,1,1));
   }
   else

  {
      info("Enter the time");

   }
}

Build Query Ranges with Wild card Ranges

Sample code to retrive Items withe the wild card searches using Query Object

static void WildCardQuery()
{
Query query = new Query();
QueryRun queryRun;
InventTable inventTable;
;
query.addDataSource(tableNum(InventTable)).addRange(fieldNum(InventTable, ItemId)).value("900*, 1790*, 278*");
queryRun = new QueryRun(query);
    while(queryRun.next())
   {
      inventTable = queryRun.get(tableNum(InventTable));
      info(inventTable.ItemId);
   }

}

Hide/Unhide the TaskPane from Code

This piece of code will do the trick
Create the Job as below and Execute for Hide/Unhide

static void TaskPaneSet(Args _args)
{ boolean visible=true;
;
  new xInfo().taskPane().setVisiblity(visible);
}

 

How can a user id be linked to an employee?

Make sure you have created both an employee and a user. Now you are ready to associate them with one another. Go to the Administration - User form and select the desired user. Now press the button User relations. Ax will automatically create a new record, if no link already exists.
Go to the second tab page and under Employee, select the wanted Employee.
You have now successfully linked your Dynamics Ax user to an employee.

Linking a Ax user can either be internal (employee) or external (customer, vendor, business relation).
Same method applies for linking with external contacts.

 

Filter Records in Form using Code

The standard filter functionality in Ax forms is a neat and powerful feature.
Using this filter functionality in your code is something you'll definitely use at some point in time as a programmer.

Although it's possible to do it in a single line of code, I prefer a 3 step solution. That way it's more flexible.
Let me show you by example. We'll filter the customers records in form CustTable, only showing customers with Custgroup 20.

Step 1: Declare a class variable
In the ClassDeclaration method of the form, define a range.

QueryBuildRange CustGroupQBR;
Step 2: Instantiate the new range.
In the init method on the datasource of the form, you assign the range to a specific field (after the super call).

public void init()
{
super();

CustGroupQBR= this.query().dataSourceName('CustTable').addRange(fieldnum(CustTable,CustGroup));
}
Step 3: In the last step, you assign a value to the range.
This is done in the executeQuery method on the same datasource of the form. Before the super call. Like this:

public void executeQuery()
{ ;

CustGroupQBR.value(queryvalue('20'));

super();
}
You're done! When you open the form, your customer records are filtered, you only get the customers with CustGroup 20 set up.

How to use Query Ranges with Expressions

The standard filter functionality in Ax forms is very powerful and use full way to filter records. (Cntrl + G) is the shortcut for the filter functionality. Using this filter functionality in your code is something you’ll definitely use at some point in time as a programmer.

Technically this filter functionality is called adding ranges (range = where clause of the query). This is the very details topic which includes scenarios from Simple to very advance level.

Some time you need to show filtered records on the form (adding ranges to form datasource). This is very simple, you just need write some code.

this.query().dataSourceName(‘CustTable’).addRange(fieldnum(CustTable,Custgroup)).value(queryvalue(‘20));

Some time you need to use expressions or if you need OR clause in the query. Support of expressions is very good to implement the complex queries containing some complex joins and conditions. if you need to implement OR clause then again you need to use the expressions. If you add ranges to two different fields then it default supports the “And &&” clause, the “OR ||” clause is only supported if you add ranges to same field with different value. In all other cases we need to use the expressions to implement the OR clause. For details on implementing the expressions

this.query().documentaries(‘InventTable’).addRange(fieldnum(InventTable,RecId)).value(strFmt(‘(ItemId == “%1″)’, queryValue(“B-R14″))); http://www.axaptapedia.com/Expressions_in_query_ranges

Other than that there are two important classes in Dynamics ax 2009 which deals with the query ranges.

SysQueryRangeUtil
The SysQueryRangeUtil class is just the great enhancement to the AOT queries made in AX 2009. It is possible now to specify a range on the AOT query with the value taken from a method’s return. The requirements for such range method are quite simple: the method should be static, return string, be a member of the SysQueryRangeUtil class and not be related on the data fetched by the query

Tuesday, August 23, 2011

Trade and Logistics training

The Following link provides the intial training for AX learners to understand how Trade and Logistics setups are done.
http://www.dynamicsaxtraining.com/trade-and-logistics-training

Executing SQL directly from X++

The X++ language supports a number of ways to execute native SQL against any SQL data source. Below are two samples for retrieving data as well as manipulating data.
Following are the Example using Connection Class.
Example 1#Retrieve

void RetrieveSQLData()
{
   Connection con;
   Statement Stmt;
   ;
   Con = new Connection();
   Stmt = Con.createStatement();
   ResultSet Resultset =Stmt.executeQuery(‘SELECT VALUE FROM SQLSYSTEMVARIABLES’);
    while ( Resultset.next() )
    {
         print Resultset.getString(1);
     }
}

Example 2#Manipulation

void DeleteCustomerData()
{
    str sql;
    Connection conn;
    SqlStatementExecutePermission permission;
;

    sql = ‘delete from custTable where CustId='1005'’;
    permission = new SqlStatementExecutePermission(sql);
   conn = new Connection();
   permission = new SqlStatementExecutePermission(sql);
  //Define the Permission to be started for Executing any manipulation Statements directly
   permission.assert();
   conn.createStatement().executeUpdate(sql);
   // the permissions needs to be reverted back to original condition.
   CodeAccessPermission::revertAssert();

}

Performance improvement by changing position of dataAreaId field in index


Performance-wise, fields in the index should be sorted by selectiveness. 
Most selective(unique) fields needs to go first and least selective should be in the end of the list. 
By default AX kernel creates indexes with dataAreaId field at first position in the index which is good if you work with multiple companies. Position of DataAreaId field 
can be changed by adding the dataAreaId as a field in an index:
1. right click on the index and click "New Field"
2. select dataAreaId in the index field properties
When it is done dataAreaId can be placed at any position in the index(last one in my case). 
AX kernel will keep that position and won't override it.