Thursday, 14 June 2012

AX2012 : X++ code to access marked records on a form

In AX2012 there is a default mark check-box on forms showing data in grid, which allows user to mark some/all records. There are situations where we need to identify which records are marked by user and do some processing on them.

This is a short post to show how to access marked records on a form in AX2012.

I have created a button on "Production order" form, which shows the marked records when it is clicked. In order to do this, we need to:
1. Add a new button on the form.
2. Set property "MultiSelect" to Yes.
3. Override the clicked method of the button and code as shown below :

Inside the for loop you can write the logic which you want to process.

Tuesday, 12 June 2012

Use edit method to mark/unmark all records

In continuation to my previous post (Edit method - I), I'll now use edit methods to mark all and unmark all the records of a form. It is a very common requirement to have such ability on forms.
In this post we'll use the edit method created in previous post (Edit method - I) and continue to create new buttons on customer details form to mark all and unmark all records.

To achieve this,we'll be doing the following:
1. Create new buttons on customer details form to mark all and unmark all records.
2. Create new functions on customer details form to mark all and unmark all records.

Let's start:

1. Create 2 new buttons on the customer table form, and change their text property to "Mark All" and "Unmark All" respectively. The CustTable form design should look as shown below:

2. Now we create a new function on this form to mark all the records. Let's name this function as PAMarkAll. We traversed through all the records one by one using a while loop and called the edit method for each record to mark it. The code should look like:

In the above code, we have declared a variable of type custTable which will help us to traverse through all the records shown on the form.
Next we initialize it with the first record using
          custTable_ds.getFirst() function.
Then we start the while loop.
Please note the way we have called the edit method. The first parameter of the edit method signifies that the value is changed, the second parameter is the custTable variable which we are using to iterate the records on the form, the third parameter is to signify that value returned  from the edit method needs to be turned on.
After marking the currently selected record we will get the next record using
        custTable_ds.getNext() method.
The above loop will be executed till the next record is found.
At last we used element.redraw() method to redraw the form.

3. In the similar way we create a new function to unmark all the records, the only change is that we need to pass false as the third parameter in the edit method call. The function to unmark all the records should look as below:

4. The functions that we created above should be called when we click the mark all and unmark all buttons. To do this we override the clicked method of both the buttons. The clicked methods should look like:

Now that we are done with the modifications required to mark all/unmark all the records, let's open the customer master form and then play with mark all and unmark all buttons. On clicking mark all button we'll notice that all the records are marked.

On clicking Unmark all button we notice that all the records are unmarked.

So in the post we saw a good example where we can use edit methods in real time situations. In my coming posts, I'll show how we can use combination of edit and display method to achieve some good features to enhance user experience on Dynamics Ax forms.

Sunday, 3 June 2012

How to use edit methods to mark records

In this post I’ll show how we can use edit methods to mark records on a form and display all the marked records on a click of a button.

To achieve this we’ll :
1. Use a map to store the references of marked records. (PS: we can use other data types, like container , array, lists. We can also use temporary tables. Depends on your comfort level.)
2. Create a new edit method.
3. Add a check box control on Customer Details form (Forms\CustTable).
4. Add a new button to display the information of marked records.

Lets begin:
1. In class declaration of the form declare a map.

2. In the init method of the form , we will initialize this map 

We'll use RecId as the key and AccountNum as the value.Hence the first parameter in map initialization is of type Int64 and second parameter is of type String.

3. Under the methods node of \Forms\CustTable\Data Sources\CustTable, create a new edit method as shown below . 
As we have created this edit method under a form datasource method node we have to provide 3 parameters:
    a) Boolean  - this is used to determine whether the user has entered anything into the control.
    b) CustTable - parameter for the datasource.
    c) Boolean - as the return type of edit method is boolean so the 3rd parameter should also be of type boolean. This holds the boolean value that the user has entered into the control.

Lets understand the logic mentioned in the ablove slide for the edit method.
If the user has amended the value of check box (if(_set)) then:
a)      If user unmark a check box (if(!_mark)) then check if the RecId of unmarked CustTable record exists as a keyvalue in the map. If it exists then remove the corresponding keyvalue (RecId) from the map.
b)      If user mark a check box (else part) then insert the RecId as keyvalue and AccountNum as valuevalue in the map for the marked CustTable record.
Return a boolean: true if the RecId of the current record exists in the map as a keyvalue, false if it does not exists.
Exists method of a map always returns boolean.

4. Now we add a new check box control in the form design and set the properties as shown below. 

5. Now we need a new function to display the information(valuevalue i.e AccountNum) of the records marked by the user. In this function we will iterate through the map(paMarked) using mapEnumerator class and display the information to the user. We can also use mapIterator class for this purpose.

6.Last step is to create a new button on the form, define properties and call the above function(paShowMarked) on its clicked event.

7. Now we are ready to run the process J. We open the customer details form, notice that there is a new check box (Mark) in the overview grid.Now we mark some of the records and then click on show marked button.
An InfoLog will display the AccountNum of all the marked records.

But this is not all. There is lot more to do with the edit methods.
In coming days I'll continue with our journey of edit and display methods .

Until then....
Keep exploring the depths of AX!!!