Create a new method in runtime

Some one may need to use codes to create or edit a method in Axapta.
Here is an example to show how to create a lookup method for a form’s field in runtime.
static void CreateFieldMethod(Args _args)
{

TreeNode tn1,tnAddr, methodsNode;
MemberFunction memberFunction;
str source;
;

//Thanks for Max Belugin’s comments,
//it is really good to use Verbatim String as well.
//The reason why I use escape characters here is
//because this line of code is copied from standard Axapta application 🙂
tn1 = infolog.findNode(

\\Forms\\Address\\Data Sources\\Address\\Fields\\AddrRecId”);


tnAddr = infolog.findNode(
\\Forms\\Address );
methodsNode = tn1.AOTfindChild( ‘Methods’ );
methodsNode.AOTadd(‘lookup’);
memberFunction = methodsNode.AOTfindChild( ‘lookup’ );
source =
@”public void lookup(FormControl _formControl, str _filterStr)
{
super(_formControl, _filterStr);
}” ;
memberFunction.AOTsetSource(source, false);
memberFunction.AOTsave();
methodsNode.AOTsave();
tnAddr.AOTcompile();

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: