Tuesday, July 16, 2019

table relations set up in Dynamics Ax 2012

when you add a relation this is what you see:

image

the relation works perfectly but it is not best practice to leave it like this.

for full details look in the documentation: https://docs.microsoft.com/en-us/previous-versions/dynamics/ax-2012/reference/hh803130(v=ax.60)

as a quick guide here my notes:

Entity Relationship Diagram (ERD):

Cardinality defines the possible number of occurrences in one entity which is associated with the number of occurrences in another.

image

Example:

image

so this is how you will set it up:

image

RelatedCardinality
corresponds to the notation on the parent table (the one we where we set the relation, HcmBenfit)
[you have two options:

  1. Exactly one
    use it when the field is mandatory yes
    image
    or when is assured unique by an index
  2. ZeroOne
    use it when the field is not mandatory
    image
    image

Cardinality
corresponds to the notation on the child table (the one we are referencing, HcmBenefitOption)

RelationshipType
the option you will commonly use are

  1. Association (is the basic one. you can always use it)
  2. Aggregation
  3. Composition

Aggregation use it only when

  • The parent table has a delete action node that is defined to use this relation node.
    image
  • Any of the foreign key fields for this relation in the child table have their Mandatory property set to Yes.
    image

Composition Is a stronger type of Aggregation. A table must not have more than one Composition relation. For example, a building is composed of rooms, and a given room cannot exist in more than one building.


Roles

you can leave the default

image

unless you need to use the same table related table in multiple relations.

see this post: https://community.dynamics.com/365/financeandoperations/b/axaptavsme/posts/use-one-table-for-creating-multiple-relation

Requirement: I need to use the single table to add multiple relation on a table. For eg. I have a parent table Table1 with field ItemId and ProcessItem. Now I need to add relation to InventTable to both fields. Hence I have to create two relation with InventTable.

How to do: Add two new relation in your table as shown below.

clip_image001
You might get an error message after this,
'RelatedTableRole' conflicts with another 'RelatedTableRole' on relation InventTable_Item on table ProcessedItemTable.
clip_image002
Solution to this error:
Set below two properties to each relation
1. UseDefaultRoleNames- set it to “NO”
2. Role: Give any logical name here, for eg. “Item”

clip_image003
clip_image004

Now compile your table, error must gone.


CreateNavigationPropertyMethods


The CreateNavigationPropertyMethods if this is set to Yes, then Dynamics AX
will automatically create navigation methods for the related table.

This method allows us to link two table buffer (parent and child) instances.

The value set in the RelatedTableRole property is used to define the name of the
navigation method.

If the UseDefaultRoleNames property is set to Yes (and the RelatedTableRole
property is blank), then the table names are effectively used as the navigation
methods.

In addition, the NavigationPropertyMethodNameOverride property can be
used to specify a navigation method name. This property is typically used if the
relatedTableRole is already a method or if the table name is too long.


It's important to note that the navigation methods are neither displayed in the
Methods node, nor are they visible under the table buffer methods displayed by
IntelliSense. They are similar to Map methods in the sense that the developer can
type them in and the compiler we know, despite the fact that the method name is
not available in the list of methods.

image

now I can do like this:

[SysClientCacheDataMethodAttribute(true)]
display Email email()
{
    return this.HcmWorkerRelation().email();
}

Wednesday, July 3, 2019

Macro parameters and strFmt in Dynamics ax 2012

static void JobEF_MacroWithParameters(Args _args)
{
    #define.MyMacro("ciao  %1")
    
    //use localMacro only when in a class declaration. 
    //in a method can also use #define
    #localMacro.Pippo
        //must escape to use it in a strFmt: 
        //see http://www.axaptapedia.com/Macro
        'ciaoooo \%1' 
    #endMacro
    
    str pippo = 'pippo1';
    
    //it does not take the variable pippo, but it use it a simple text
    info(#MyMacro(pippo)); 
    info(strFmt(#Pippo, pippo));
}

Monday, July 1, 2019

dialog addField vs addFieldValue in Dynamics Ax

I think an example speaks by itself

class EF_DialogTrial2
{
    String20 myString;
}
public static void main(Args _args)
{
    EF_DialogTrial2 cl = new EF_DialogTrial2();
    cl.dialog();
}
protected void dialog()
{
    Dialog dialog;
    DialogField dialogField1, dialogField2;
    
    dialog = new Dialog();
    
    myString = 'ciao';
    
    //the difference between addFieldValue and addField is that in the first you can 
    //initialize the dialogue field with some value. 
    dialogField1 = dialog.addFieldValue(extendedTypeStr(String20), myString, 'my value');
    dialogField2 = dialog.addField(extendedTypeStr(String20), 'my value2');
        
    if (dialog.run())
    {
        myString = dialogField1.value();
        
        info(myString);
        info(dialogField2.value());
    }
}