Wednesday, September 23, 2009

How to open a form by code

A short tutorial on how to open a form in Dynamics Ax (Axapta) by code.

In the shortest example, all it takes is one line of code.

new MenuFunction(MenuItemDisplayStr(CustTable),MenuItemType::Display).run();

The above code will open the CustTable form. That's all it takes, it's that simple.
Now if you want to supply some arguments to the opening form, this is also possible with the optional args parameter.
Like this for example:

static void OpenFormByCodeA()
{ Args args = new Args();
;
args.record(CustTable::find('ABC'));
new MenuFunction(MenuItemDisplayStr(CustTable),MenuItemType::Display).run(Args);
}

This code will open the CustTable form and filter out the customer with accountnumber ABC.
Use the args methods like parm and parmEnum to provide your target form with more data.

If you want even more control on opening the form from code, this is also possible.
This next example gives the same result as the previous one.

static void OpenFormByCodeB()
{ FormRun formRun;
Args args = new Args();
;
args.name(formstr(CustTable));
args.record(CustTable::find('ABC'));

formRun = ClassFactory.formRunClass(args);
formRun.init();
formRun.run();
formRun.wait();
}


Now if we tweak this a little bit, we can add our code
Like this:

static void OpenFormByCodeB()
{ Object formRun;
Args args = new Args();
;
args.name(formstr(CustTable));
args.record(CustTable::find('ABC'));

formRun = ClassFactory.formRunClass(args);
formRun.init();

formRun.yourmethodgoeshere(); /* !!

formRun.run();
formRun.wait();
}


By changing the type of formRun from class FormRun to class Object, we can implement and execute extra methods on our destination form! This gives us extra possibilities for customizations. You can pass along extra parameters for example.
Only drawback: While programming, your method doesn't show up in the IntelliSense, showing all the available methods. So be carefull of typo's. (They don't give a compile error, but they will give you a run-time error.)

Next up: Run a report by code

21 comments:

  1. Thank you so much!!
    I've been looking for this line of code for hours :-(

    ReplyDelete
  2. You Made My Morning

    ReplyDelete
    Replies
    1. boss i have query,we have multiple company in ax 2009,when i create sales order for a company so for some company the delivery address value data insert in to street field from which automatic fetch that data into delivery address field by default,which meet our requirements,but for one company this process can not be occur ,that is no data can fetch from street field to delivery address which create inconsistency in sales confirmation report..
      sir please help me ,

      Delete
  3. Hi, I like your blog a lot. I've learned much from your work.
    But my question is about running a form that is already in favorities window.
    How to run a form with a user query that is already pin to particular form?
    Any ideas?

    Best regards
    Jacek Witkowski

    ReplyDelete
  4. It helps me a lot!!

    ReplyDelete
  5. Is it possible with this code or some similar code to open from WPF application AX form?

    ReplyDelete
  6. Ahh ! A master class of a true mentor ! Greatly done ! THANK YOU !

    ReplyDelete
  7. boss i have query,we have multiple company in ax 2009,when i create sales order for a company so for some company the delivery address value data insert in to street field from which automatic fetch that data into delivery address field by default,which meet our requirements,but for one company this process can not be occur ,that is no data can fetch from street field to delivery address which create inconsistency in sales confirmation report..
    sir please help me ,

    ReplyDelete
  8. boss i have query,we have multiple company in ax 2009,when i create sales order for a company so for some company the delivery address value data insert in to street field from which automatic fetch that data into delivery address field by default,which meet our requirements,but for one company this process can not be occur ,that is no data can fetch from street field to delivery address which create inconsistency in sales confirmation report..
    sir please help me ,

    ReplyDelete
  9. Subscribe to learn this new technique in Ax . Give like comments and share
    https://youtu.be/8dXjRzSk910

    ReplyDelete