Skip to main content

Generating and running code RUNTIME.

How to create, compile and run code at RUNTIME.

In some situations the need for making "generic" code arises.

This is a little example that generates, compiles and runs code to delete the contents of table PBACustGroup.

The code is made as a job, but transforming this code into a method on a class, and calling this method with parameters that allows it to identify the table, suddenly makes us able to delete several selected tables with very little code.
It also allows for USER CONTROLLED actions, for example letting the user build a liste of tables he would like to delete, and then calling the code for each of these tables.


static void TestGenericCode(Args _args)
{
    XppCompiler XppCompiler;
    str code;
    str tableName = "PBACustGroup"; // Here we define the name of the table
;

    // A compile object is created
    XppCompiler = new XppCompiler();
    // Here the code to delete the tabel is created
code = "void x() {"+tableName+" "+tablename+"; ttsbegin; delete_from "+tableName+"; ttscommit; }";
    // And compiled
    if (XppCompiler.compile(code))
    {
        // If the compiler was satisfied with the code we run it
        XppCompiler.execute();
        // And inform the user that we deleted data
        info("Deleted all record in table "+tableName);
    }
    else
    {
        // Otherwise the compiler lets us know the code was errorneous
        info(XppCompiler.errorText());
info("fejl !!");
    }
}

Comments

  1. Hello,

    I've read your blog posts and I find them very interesting and helpful.

    I have a task to accomplish, but don't know how. I'll explain you what's about and how I'd like to solve it.

    I have to migrate selective subset of data from one aos to another (eg. all CustTable rows where name="John").
    I thought to create a method on the first aos that gathers the data and send the table container to a web service. The WS would then write into the second aos instance.
    I've also thought to code it using reflection to make it more generic and reusable, but since I'm not an expert X++ coder (I'm stronger in C# and Java) I'm stuck with this implementation.

    Do you know a better way to solve my problem? Can you give me some ideas?

    Thank you in advance and keep posting,
    Francesco

    ReplyDelete

Post a Comment

Popular posts from this blog

Suppressing the infolog

Supressing the infolog is often useful in D365FO when augmenting code. When augmenting code using COC (Chain Of Command), you can have new code run either before or after the code you are augmenting. This means that any infolog-messages that the standard application code does, will be shown to the user, even if your augmentation supports a scenario where there must be no infolog-messages. How do you avoid the standard application infolog-messages ? To the rescue comes temporary supression of the infolog. The suppression consists of: 1) Saving the current infologLevel 2) Setting the infologLevel to SysInfologLevel::None 3) Run your code 4) Restoring the saved infologLevel to the infolog For example a table could have a validatewrite-method that validates that you are only allowed to use 3 out of 6 options in an enum-field, and you need to allow for a fourth one. Table a - validateWrite method: boolean validateWrite() {     Switch (this.enumField)     {         boolean ret;         case

Indicating mandatory field in a dialog (RunBase) class.

A classical problem is indicating that a field is mandatory in a dialog, when the field is not bound to a datasource/field in a datasource. Normally fellow developers will tell you that, that is not possible. I found a way to do this. In your Runbase-based class you can implement the putToDialog-method e.g like this: protected void putToDialog() { super(); fieldMask.fieldControl().mandatory(true); } where fieldMask is a DialogField object in your dialog. This will make the field act like it was a mandatory field from a datasource in a form, showing a red-wavy line under the field, and requiring the field to have a value. Attention: Your class has to run on the client.If you set your class to run on the server, you get a run-time error, when the fieldMask.FieldControl()-call is made.

Dynamics ax 2012 traversing selected records in a form data source

A classical developer challenge in Dynamics AX is to enable a form button when multiple records have been selected in the form by the user. This usually involves writing some form of loop (for or while or do-while) that starts out with calling _ds.getFirst() and continuing the loop as long as _ds.getNext() returns a tablebuffer. Well things got a little bit easier in AX 2012. In AX 2012 you can use the MultiSelectionHelper class. One example is the following that I encountered in AX 2012: Can you make the customer collection letter print out run for each selected collection record in the Print/Post collection letters form (Accounts receiveable / Periodic / Collections / Print/Post Collection letters). If we ignore the possibility for setting up print destination for running each report we can do this in two steps: 1) Change the "Multiselect" property of the "MenuButton" and the "Menuitembutton" in the MenuButton in the form from "Auto&quo