Sunday 23 September 2012

Finding unused labels in Dynamics Ax 2009

static void FindUnUsedLabels()
{
    str 50          labelId;
    str             labelString;
    int             i;
    //set max label to the highest number of labelId in your application
    int             maxLabel = 2000;
    xRefNames       names;
    XRefReferences  ref;

    ;

    while (i <= maxLabel)  
    {
        //Sequential generation
        labelId = "@IFC" + int2str(i);
        
        //Find if the label id has an cross reference
        //record
        select recid from names
            where names.Name == labelid
        exists join ref
            where names.RecId == ref.xRefNameRecId;

        labelString = SysLabel::labelId2String(labelId);
        
        //If there is no record in cross reference then log it
        if (! names.RecId &&
        //avoid logging labels that are already deleted (This is because of sequential check like IFC1, IFC2, IFC3 etc...)
            labelString != labelId)
        {
            info(strfmt("%1 - %2\n", labelId, SysLabel::labelId2String(labelId)));
        }

        i++;
    }

}

Thursday 20 September 2012

use display method as titlefield

1. in the caption() method of table write the below code
public str caption()
{
str ret;
ret = super();
ret = this.news() ; // news is a display method
return ret;
}
In the form design mention the datasource of table name
you can use it if  you need even a single field ,not more than 2 field.. you can display any number of fields
just  use and return the display method in the caption method of table


or

Code to disply the emp_id on the title bar of the form

Path : MyEmplForm-->Data Sources-->MyEmplTable-->Methods-->active()


public int active()
{
    int  ret;
    ret  = super();
    element.design().caption(EmpLeaveDetails.Emp_Id);
    return  ret;
}
 

Monday 17 September 2012

Processing multiple records

In my practice, I was asked a number of times to create various application functions that
process user-selected records in a form grid. Regardless of what function is being created, the
first step was always to determine what records were selected by the user.
In this recipe, we explore several ways of achieving this goal. We will modify the Items form
in the Inventory management module by adding a new button to it, which lists currently
selected records in the overview grid.
How to do it…

1. In AOT, open the InventTable form and create a new method with the following code:
void processSelectedItems()
{
InventTable inventTableLocal;
;
for (inventTableLocal = InventTable_ds.getFirst(true) ?
InventTable_ds.getFirst(true) :
InventTable;
inventTableLocal;
inventTableLocal = InventTable_ds.getNext())
{
info(strfmt(
“You’ve selected item ‘%1′”,
inventTableLocal.ItemId));
}
}
2. Add a new Button to the form’s ButtonGroup group:
Property Value
Name ProcessSelectedItems
Text Process
MultiSelect Yes
3. Override its clicked() with the following code:
void clicked()
{;
super();
element.processSelectedItems();
}
4. To test the record selection, open Inventory management | Item Details, select
several records using SHIFT or CTRL and click the Process button. The selected
items should be displayed in the Infolog:
How it works…
The key element in this recipe is a for statement in processSelectedItems(). First, it
checks if more than one record is selected by calling getFirst(true) on the InventTable
form data source. If yes, the for loops all the selected records from the data source,
otherwise it uses the cursor, which corresponds to the currently selected single record. All
selected records are then stored in the local variable inventTableLocal. In this example,
we just output inventory item numbers into the Infolog using the global info() function.
The ProcessSelectedItems button is used to call the function above once the user clicks it.
Notice that its property MultiSelect is set to Yes to ensure it is still enabled when multiple
records are selected.
There’s more…
I have also experienced that sometimes Dynamics AX users struggle to select multiple records
using SHIFT or CTRL. Sometimes, it is not clear that the form itself supports multiple record
processing. In such cases a more convenient way of selecting multiple records could be to add
a new checkbox in front of each record. This would remove all confusion and improve user
experience. In order to implement that, we need to make few changes to the example above.
First, we add a new Map variable to the form’s class declaration:
Map marked;
And in the form’s init() right after the variable declaration section we create its instance:
marked = new Map(Types::Int64, Types::String);
Then, we create a new edit method on the InventTable data source with the following code:
edit boolean editMark(
boolean _set,
InventTable _inventTable,
boolean _mark)
{;
if (_set)
{
if (!_mark)
{
if (marked.exists(_inventTable.RecId))
{
marked.remove(_inventTable.RecId);
}
}
else
{
marked.insert(
_inventTable.RecId,
_inventTable.ItemId);
}
}
return marked.exists(_inventTable.RecId);
}
We also add a new CheckBox to the top of the form’s Grid in the Overview tab page with the
following properties:
Property Value
Name Mark
Label Select
DataSource InventTable
DataMethod editMark
Finally, we have to modify processSelectedItems() to loop though the map. Replace the
method with the following code:
void processSelectedItems()
{
MapEnumerator mapEnumerator;
;
mapEnumerator = marked.getEnumerator();
while (mapEnumerator.moveNext())
{
info(strfmt(
“You’ve selected item ‘%1′”,
marked.lookup(mapEnumerator.currentKey())));
}
}
Open the Items form again and notice that now it has a new checkbox Select in front of
each record. Pick several records using it and click Process. The selected items should be
displayed in the Infolog:
The principle of this technique is that we use a Map type object to store the list of selected
item numbers. The editMarked() method is bound to the checkbox control and is
responsible for adding a record to the map upon user selection and removing it from the map
if the user deselects the checkbox.
We also use the MapEnumerator class to retrieve item numbers from the map for
further processing.

Wednesday 12 September 2012

adding table for search.

Adding table for search.

Its a new feature in ax 2012,which it makes our table to be searchable.means using this feature we have chance to search data in table .

this is possible using as following ,


steps involved:

1: go to aot -->tables--> properties-->modifieddatetime property to yes save it
2:create a query which is having name with good meaning make the datasource as your table and go to properties of datasouce change the name to table name that you want to search and change the dynamic property of fields to yes.and go to properties of query and make searcheble option to yes.
3.at last restart the service that is Dynamics ax  object server .

Wednesday 5 September 2012

how to use menu item dynamically ?

how to use menu item directly ?

we will use menuitem directly using a class called menufuntion .

menufunction=new menufunction(menuitemoutputstr(reportname));//in case of report
                                                  menuitemdisplaystr //in case of form
                                                  menuitemactionstr //in case of class

Working with external database.

Working with external database :-


In this example i will explain how to access external sqlserver database tables in ax .

Steps:

Before going to work with database we have to first create a DNS through which we access external database .to create dns follow these steps,
 1)Administration Tools-->Database(odbc)
2)Dsn-->add (which opens a wizard)-->select sqlserver-->finish
3)(opens another window consists of) referdatasource name : give any name let us it is AX
     a)description:AX
    b)server:(its your server name) to which you want to connect
   c)next,next,finish-->testdatasource -->test connection
This finishes DNS creation

step2:

create a new job as follows:

static void odbcTest(Args _args)
{
  odbcconnection odbc;
LoginProperty login;
Statement st;
ResultSet rt;
;
login=new LoginProperty();
login.setDSN("AX");
login.setDatabase("AX593");
odbc=new odbcconnection(login);
st=odbc.createstatement();
rt=st.executequery("select * from custtable");
while(rt.next())
{
 info(rt.getstring(1));//here 1-represents coloumn number
info(rt.getstring(2))
}
}
}

Edit Method Example on table.

Edit Method on table methods:-

We have 2 methods which we used to access another table data in our form in which we don't have that table as data source.Those are display and edit methods.

Edit method example:

 Suppose take  2 tables  cartable and rental table

 cartable fields-->1)carid as edt and primary index
                           2)Milege as edt

Rental Table Fields-->1)Carid 
                                 2)Custid

steps:

make a relation between cartable and rental table (normal relation in rental table with carid)
create a find method in cartable as

public static cartable find(carid _carid )
{
 cartable car;
 ;
 select car where car.carid==_carid;
 return car;
}

now create an edit method in rental table to edit milege of carid in cartable ,the method looks like

edit milege editmilege(boolean set,milege value)
{
  cartable cartable;
  milege ret;
  ;
cartable=cartable::find(this.carid);

if(set)
{
cartable.selectForUpdate(true);
 ttsbegin;
 cartable.milege=value;
 cartable.update();
 ttscommit;
 return value;
}
else
{
 ret=cartable.milege;
}
return ret;

}

now to test this create a form with rental table as data source and create a grid in design .Drag this edit method into grid it creates a column mileage .

open form and change the milege value it reflects in cartable .this is possible in case of edit method .if we use display method we cannot have a chance to change value .