Tuesday, September 25, 2012

Simple AX container example

Below is a simple example of creating and inserting values into a container, determining the size of the container, and looping over that container to retrieve the values from it.

Since this is a job and I wanted to test out embedding methods into the job, I included that little complexity in here. All that it does is either fill the container with data or leave it empty based on the 'dataInCont' value which stands for data in container. Note that this variable is a global so when declared in that location and using embedded functions/methods, this location acts as the class declaration.

This is AX development 101 stuff but I was using it in a training sample so I decided I might as well slap the code on my blog as well as the resulting information log.

Enjoy!


static void daxTestJob(Args _args)
{
    container cont;
    int i;
    boolean dataInCont = true; // Do we want to run this job with container data? Note this is a global
   
    // This method will either return an empty container or one with data
    container test()
    {
        container contTest;
       
        if (dataInCont)
        {
            contTest += 5;
            contTest += 6;
            contTest += -52;
            contTest += 'DAXDUDE';           
        }
       
        return contTest;
    }
    ;
          
    cont = test();
   
    info (strFmt("Length of the container: %1", conLen(cont)));
   
    for (i=1; i <= conLen(cont); i++)
    {
        info (strFmt("container value %1 is %2", i, conPeek(cont, i)));  
    } 
}

Resulting infolog from code above:

Friday, September 21, 2012

AX 2012 Issue - New field to table and field group saying 'Unretrievable' and disabled

I encountered an issue when adding a new field to an AX table and putting it in a field group. When a form is opened that has a section for this field group where the new field was added has the field disabled and there is text in the field entry section that reads 'Unretrievable' in AX 2012.
QUICK SOLUTION: Weird table sync issue.

Field added to base AX table and field group disabled and reading 'Unretrievable'

I have never seen this happen before in any other AX 2012 environment and was occurring in a development environment where TFS was enabled. It's a pretty simple scenario so I'm not sure why I was seeing what I was seeing. It doesn't get much easier than this. The table was recompiled and synced, environment open and closed,  I actually even tried this in several other environments and was not able to replicate this issue. It was just in the TFS synced environment. I could write data in the table browser back-end but not via the form.

SOLUTION: Looks like this was an issue with the syncing of the EDT/Table. I was able to fix this by creating a new field and trying again. I eventually was able to get one to stick. Not sure what was going on. Looks like an issue with the synchronization.

After I had everything working, I changed the alignment property on the data type, synced the EDT, and this issue came back. I then changed the property back, synced the EDT again and the the issue was fixed. I then switched the alignment back to what I wanted, synced, and the issue was fixed then.


Thursday, September 20, 2012

Max AX method name and best practices

The maximum length for an AX method in AX is 40 characters.

If the name is too long you'll get the compilation error 'Name is too long. Truncate name to '[>40 character method name here]'.

Remember when you name your AX classes, the names should start with the three letter prefix [e.g. abc], then the module it belongs to [e.g. Sales], then the functionality that it follows where appropriately, ending with a brief description of the purpose. The ultimate goal is to be able to see the class name and know exactly what the class extends and if its custom and what it does.

The methods within this class then should be able to not only follow the base overrides (find, exist, modifiedField, etc), but also provide a directed purpose where appropriate. It is good to keep in mind the best practices behind a method when determining the name of the methods.

A refresher around the functions of AX methods best practices. There are a lot more than the below (like keeping variables as local as possible) but these are general.

MSDN: Best Practices for Methods [AX 2012]
Methods should:
  • Be logical.
  • Do a specific task.
  • Have no side effects.
  • Be well structured, especially when it comes to good places for overriding and for overlayering.
Make your methods small and logical. If you have a method that does more than one 'thing'; then consider splitting it up in two (or more) methods. It will then be easier to override or overlayer exactly the functionality that needs to be specialized or customized.
Do not have any unused variables in your methods.

Wednesday, September 19, 2012

Select statement where condition formatting and placement in Dynamics AX

In Dynamics AX X++, there are a few ways to format a select statement and place the where clauses when using multiple joins. See the two examples below. Both do exactly the same thing but note the difference in placement of the where conditions. Just making sure people know this. Apparently some people do not so at least now its out on the internet somewhere.

I don't believe there is a best practice around this but it should be consistent in your development either way. I prefer option 1 as it reads better in my opinion. The where conditions can be separated a little more and linked back to their initial data source.


OPTION 1:
public static Name storeOperatingUnitName(RecID _storeRecId)
{

    RetailChannelTable      retailChannelTable;
    DirPartyTable           dirPartyTable;
    OMOperatingUnit         operatingUnit;
    ;

    select firstOnly recId from retailChannelTable
        where retailChannelTable.RecId == _storeRecId
        join recId from operatingUnit
            where operatingUnit.RecId               == retailChannelTable.OMOperatingUnitID
               && operatingUnit.OMOperatingUnitType == OMOperatingUnitType::RetailChannel
            join name from dirPartyTable
                where dirPartyTable.RecId == operatingUnit.RecId;                  

    return dirPartyTable.Name;
}


 OPTION 2:
public static Name storeOperatingUnitName(RecID _storeRecId)
{

    RetailChannelTable      retailChannelTable;
    DirPartyTable           dirPartyTable;
    OMOperatingUnit         operatingUnit;
    ;      
   
    select firstOnly RecId from retailChannelTable       
        join RecId from operatingUnit           
            join Name from dirPartyTable
                where retailChannelTable.RecId           == _storeRecId
                    && operatingUnit.RecId               == retailChannelTable.OMOperatingUnitID
                    && operatingUnit.OMOperatingUnitType == OMOperatingUnitType::RetailChannel
                    && dirPartyTable.RecId               == operatingUnit.RecId;

    return dirPartyTable.Name;
}

Tuesday, September 18, 2012

AX 2012 Delete a record from table browser or grid when delete button is not available

In earlier versions of AX, you had access to the red X on the main AX tool bar to allow you to delete a record from a table via the table browser or a grid where there was no delete option.

In AX 2012, this option is not necessarily available. You need to use an AX shortcut now. I always forget what this is so I'm writing a post. Simple stuff I know but its different so I'll write about it. Highlight the record(s) of interest and hit ALT+F9. This will prompt you if you really want to delete the record(s). Unless you have a sudden change of heart, click yes.

Fin