Dynamics AX 2012 - The AutoRefreshData property on buttons

In yesterday's post, we learnt about the NeedsRecord property on buttons. This is a new property introduced in Dynamics AX 2012. Today we will learn about another new property on buttons in Dynamics AX 2012 - the AutoRefreshData property.

So what does this property do? The help text for the property says "If yes, the data in the datasource associated with this button is refreshed after the button is clicked."

Well that explains it. With this property on, any datasource associated with the button is refreshed when the button is clicked.

Consider a scenario, where on clicking a button, an edit form opens and allows the user to edit certain field values. After the form is closed, the values in my master form will auto refresh. My initial assumption was that setting the AutoRefreshData property will eliminate the need to explictly call refresh/research after a button was clicked.

I found out that this isn't the case and I was wrong.

Let us look at this in more details with an example.

I have a simple master form called ProjectMasterForm which displays the ProjID and the Name fields in a grid. Editting the records isn't allowed.

I have another form called ProjectEditForm which just displays one ProjID and name. Editting of the name field is possible.

On my ProjectMasterForm, I have a menu item button to open the edit form. The AutoRefreshData property is set to Yes on the menu item button. The image below shows my objects.
AutoRefreshData in Dynamics AX 2012
Now I opened the ProjectmasterForm, and selected the first record and hit the "Open the Project Edit form" button. In the edit form, I changed the name to something else and closed the form. Hmm, the data on my master form hasnt changed. I hit F5 and then the data refreshes. So what did the AutoFreshData property do? Actually the AutoRefreshData property did refresh the datasource but before my edit form was called. If you recall the help text above for this property, it says when the button is clicked. So after the click event is completed, the datasource is refreshed. In our case, two things happened when the button was clicked

  1. The datasource was refreshed

  2. The edit form was called.


So how do we ensure that the datasource is refreshed only once the edit form was closed. As far as I know, this isn't possible with menu items. The AutoRefreshData property seems to be useful when we call a form through code.

So let us quickly add a new button, set its text to "Open edit form" and override the clicked() method to open the Edit form.


void clicked()
{
Args args;
FormRun formRun;

args = new args(formStr(ProjectEditForm));
args.record(ProjTable);

formRun = classFactory.formRunClass(args);
formRun.run();
formRun.wait();

super();
}


Run the form now, click the new button and change the project name. Close the form. Did the name change? No. Well that is because, we havent set the AutoRefreshData property on the new button. Change the property to Yes and repeat the exercise.

Did it work this time? You bet it did. There are two important things to notice here. First the code to open the form should be placed before super() in the clicked method. Second, we need to wait on the new form. If we use formRun.detach() instead of the formRun.wait() line above, it wont work.

The bottomline is that this method isn't useful for menu items. It is beneficial for buttons which open some form through code. Also, we need to wait() on the form.

I hope this post clears the behavior of the AutoRefreshData property. Thats all for today folks. Come back soon again as we learn new things.

Comments

Anonymous said…
Thanks for posting this article. The autoRefreshdata property saved my day! Was looking for a way to refresh my form after clicking a simple button(s) that calls a class with a dialog. Worked as a dream. Refreshes both grids in my form.
> Thor

Popular posts from this blog

How to add empty ranges in query

Get selected records in Dynamics AX 2012

The field with ID '0' does not exist in table - Cause and resolution.