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

13 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. This is axapta ERP blog for Technical and functional fields and includes Microsoft Dynamics Axapta tutorials and Dynamics Axapta Coverage. This blog also contains x++ code help for Ax developer and solution of technical and functional daily issues. This blog is specific for Microsoft dynamics programming. Enterprise portal, SharePoint services, business connectors and Enterprise Resource Planning applications and sql database.It will help to get Microsoft Business Solutions.

    http://daynamicsaxaptatutorials.blogspot.com/

    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
  4. 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
  5. It helps me a lot!!

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

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

    ReplyDelete
  8. This is axapta ERP blog for Technical and functional fields and includes Microsoft Dynamics Axapta tutorials and Dynamics Axapta Coverage. This blog also contains x++ code help for Ax developer and solution of technical and functional daily issues. This blog is specific for Microsoft dynamics programming. Enterprise portal, SharePoint services, business connectors and Enterprise Resource Planning applications and sql database.It will help to get Microsoft Business Solutions.


    axapta ERP blog

    ReplyDelete
  9. 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