Günü Sözü

"Hiçbir şey bilmeyen cahildir, ama bilip de susan ahlaksızdır. " Bertolt Brecht.
"İyilik yapabildiğim zaman mutlu olurum; ama en büyük mutluluk yapılan bir haksızlığı düzeltmektir." Tolstoy

6 Ağustos 2018 Pazartesi

List, Set, Map and Containers in Dynamics Ax 2012 (Collection classes), Macros

static void TDS_ListExample(Args _args)
{
    List l = new List(Types::String);
    ListIterator li;
    ;
   
    // limitation of list - it will give in the order u add - ascending or descending order
    // it all also allow duplicates
   
   
    l.addStart("Tendulkar");
    l.addEnd("sehwag");
    l.addEnd("gambhir");
    l.addEnd("yuvraj");
    l.addStart("haribhajan");
    l.addend("haribhajan");
   
    info(int2str(l.elements()));
   
    li = new ListIterator(l);
   
    while (li.more()) // this will return true or false -
    {
        info (li.value());
        li.next();
    }
   

}
http://dynamicsaxforu.blogspot.com/2014/03/list-set-and-map-in-dynamics-ax-2012.html


static void Erm_Set_kull(Args _args)
{
    CustTable       custTable;
    Set             set = new Set(Types::Record);
    SetEnumerator   setEnumerator;
    ;

    while select custTable
    {
       if (custTable && !set.in(custTable))
        {
            set.add(custTable);
        }

    }

   if (!set.empty())

    {
        setEnumerator = set.getEnumerator();
        setEnumerator.reset();
        while (setEnumerator.moveNext())
        {
            custTable = setEnumerator.current();
            info(strfmt("Customer: %1",custTable.AccountNum));
        }
    }

}

*--*

static Set dimensionCodesSet2DimensionTopicsSet(Set _dimensionCodesSet) { Set topicsSet; SetEnumerator setEnumerator; DimensionTopic dimensionTopic; ; topicsSet = new Set(Types::Class); setEnumerator = _dimensionCodesSet.getEnumerator(); while (setEnumerator.moveNext()) { dimensionTopic = DimensionTopic::construct( DimensionTopicType::Dimension, Dimensions::code2ArrayIdx(setEnumerator.current())); topicsSet.add(dimensionTopic); } return topicsSet; }

static void TDS_SetExample(Args _args)
{
    Set s1 = new Set(Types::String);
    SetIterator si;
   
    Set s2 = new Set(Types::String);
   
    Set resultSet;
   
    // set will always result in ascending order and it will not allow duplicates
    // set can perform additional operations - union , intersection and difference
   
   
    s1.add("A");
    S1.add("z");
    s1.add("B");
    s1.add("F");
    s1.add("F");
   
    // To find any value in a set use "in" method
    if (s1.in("b") == true)
    {
        info ("b exists");
    }
    else
    {
        error ("b doesnt exists");
    }
   
   
    s2.add("A");
    s2.add("k");
    S2.add("g");
   
    info(int2str(s1.elements()));
   
    si = new SetIterator(s1);
   
    while (si.more())
    {
        info(si.value());
        si.next();
    }
   
    resultSet = Set::intersection(s1, s2);
   
    si = null;
   
    info ("Intersection");
    si = new SetIterator(resultSet);
   
     while (si.more())
    {
        info(si.value());
        si.next();
    }

   
}

Map : 

        - Map can hold a key and associated value
        - Imp methods : elements, Key, insert

static void TDS_MapExample(Args _args)
{
    // Maps are used to hold the key and correspondiong value
   
    Map m = new Map(Types::Integer, Types::String);
    MapIterator mi;
    ;
   
    m.insert(100, "Nick");
    m.insert(200, "Leo");
    m.insert(400, "Eve");
   
    mi = new MapIterator(m);
   
    while (mi.more())
    {
        info(mi.key());
        info(mi.value());
        mi.next();
    }
}

static void MAP_LoadTableTable(Args _args)
{
   Map m = new Map(Types::String, Types::Real);
   MEE_CustTable k;
   MapIterator mi;
 
 
   ;
 
   while select k
   {
        m.insert(k.CustomerId, k.Creditlimit);
   }
 
   info(int2str(m.elements()));
 
   info ("_______________________");
 
    mi = new MapIterator(m);
   
    while (mi.more())
    {
        info(mi.key());
        info(mi.value());
        mi.next();
    }

}

Containers :

  - Containers are used to store different datatypes
  - Max columns in a container - 50

>> some important functions :

conlen - gives the length of the container
conpeek - gives the value of an element based on the index
conpoke - used for replacing any element with new element value
condel - willl delete the elements in cotnainer based on the start postion and number of elements to be deleted
connull - will empty the container
conins - will help to insert new elements to container
conview - to viuew the elements in the container - onyl for unit testing

confind - to find a particular element exists in container - it will give u the postiion/index

>> Just look at the following example to undestand how these methods can be applied :

static void TDS_Containers(Args _args)
{
    Container con = [1000, "Rahul", 5000.00, 56];
    container resultCon;
    int i;
    int position;
    // container limit is 50 columns
    ;
   
    // container index starts with 1
   
    // to get the length of the container
    info(int2str(conlen(con)));
   
    // to get any value based on the index use conpeek() function
    info(conpeek(con, 3));
   
    // to get all the values in one shot
   
    info("__________________________________");
   
    for ( i = 1; i <= conlen(con); i++)
    {
        info(conpeek(con,i));
    }

    info("__________________________");
   
    resultcon = condel(con, 2,2);
   
    conview(resultcon); // this should be only used for testing purpose
   
    // to nullify to empty the container
   
    //con = connull();
   
    //info(int2str(conlen(con)));
   
    //
    // To insert any new valuezs into container - use conins()
    // To replace any value in the container - use conpoke()
    // to find any value in container- use confind
   
    position= confind(con, "sreedhar");
   
    info(strfmt("sreedhar is found at %1 position", position));
}

Macros in Microsoft dynamics Ax

Macro :
  • Macros are reusable components
  • Macros are mostly used to remove hardcoding and for constant values
  • Macros are pre-compiled/FASTER
  • In AX, we have a macro processor
  • Macros cannot be debugged
  • Macros reduces line of code/optimizes the lines of code
  • Macros will not end with semicolon
Macros can be defined in 3 ways :


1) Local Macro
2) AOT Macro [Global macro - we can call it in all objects, tables, forms, jobs, reports etc]
3) Macro library

1) Local Macro :
                      Local macro is a mcaro which is local to that function and cannot be used outside the method/function
static void TDS_LocalMacro(Args _args)
{
#define.pi(3.142)
#define.name('Tony')
#define.address('SR Nagar, Hyd')
;
info(strfmt('%1',#pi));
info(#name);
info(#name + "is a bad person");
info(#address);
}
2) AOT Macro :
                          AOT Macros will help to resue the functions inside it.
>>Go to AOT >> Macros >> Right click on the Macros node >> New Macro >> rename it to TDSAdd 4 functions inside the macros by doubling clicking it

#define.college('SR College')
#define.age(30)
#define.inst('Vertex soft')
#define.cl(4000.00)
>>How to call AOT Macro
>>Create a new job as shown below
static void TDS_CallAOTMacro(Args _args)
{
#TDS
;
info(#college);
info(int2str#age));
}
>>How to use in any other object:
Go to TDS_CustTable >> Methods >> Override initvalue() method and paste the following code
public void initValue()
{
#TDS
;
super();
this.Creditlimit = #cl;
this.JoinedDate = systemdateget();
}
3) Macro library :
                          Microsoft has already given many macros in AOT >> Macros node
[sys] layer
we can go ahead and add any function to already existing macros [sys layer]
AOT >> Macros >> AOTExport >> open in editor and add a new function at the end
#define.marks(30)
How to call Macro library macro
static void TDS_MacroLib(Args _args)
{
#aotexport //#macrolib.aotexport
;
info(int2str(#marks));
}
How to pass values or nuMber of lines in Macros: possible
static void TDS_PassingValues_Macro(Args _args)
{
int c;
#localmacro.sum
c = %1 + %2;
#endmacro
;
#sum(10,20)
info(int2str(C));
#sum(1000,5466)
info(int2str(c));
}
Macros reduces number of lines of code as well
static void TDS_PassingValues_Macro(Args _args)
{
int c;
#localmacro.sum
c = %1 + %2;
c = c - 4;
c = c * 4/100;
#endmacro
;
#sum(10,20)
info(int2str(C));
#sum(1000,5466)
info(int2str(c));
}

Note : It is best practice to use macros only to define constants. Also TDS stands for Tushar Devendra Srivastava just using my initials as best pratice for creating my own customizations.
http://dynamicsnavax.blogspot.com/2010/08/ax-how-to-use-set-and-setenumerator.html

https://docs.microsoft.com/en-us/dynamicsax-2012/appuser-itpro/implementation-methodology?redirectedfrom=MSDN

Hiç yorum yok:

Yorum Gönder