Jan 9, 2012

How to change the date format


static void dateJob(Args _args)
{
    date currentDate = today();
    str s;
    ;
    s = date2Str
        (currentDate,
        123,
        DateDay::Digits2,

        DateSeparator::Dot, // separator1
        DateMonth::Short,
        DateSeparator::Dot, // separator2

        DateYear::Digits2
        );
    info("Today is:  " + s);
}

Filter through the query


static void Medical_Info(Args _args)
{
    Query q;
    QueryRun qr;
    QueryBuildDatasource qbd;
    QueryBuildRange qbr;
    Medicine m;
    ;
    q = new Query();
    qbd = q.addDataSource(tablenum(Medicine));
    qbr = qbd.addRange(fieldNum(medicine,Item_Expired));
    qbr.value(queryValue(Item_Expired::No));
    qr = new SysQueryRun(q);

    while(qr.next())
    {
    m = qr.get(tableNum(Medicine));
    info(m.Item_id);

    }

}


Lexical and Syntax errors in Macros


When you are developing code that contains macros, you must understand whether an error message is generated during the precompile or the compile phase. The two key words to look for are:
            
                           Lexical – This indicates a precompile error.
                           Syntax – This indicates a compile error.

#define.MyMacro1(info("Hello");)  -  Lexical error caused by the first closing parenthesis, which marks the end of the directive. Therefore the precompiler is confused by the last two characters ;)

#define.MyMacro2(++++iTest;)  (or) #MyMacro2  -  A Syntax error caused by using the non-existent ++++ operator. The X++ compiler encounters this operator after #MyMacro2 is replaced by the macro value.
The macro definition is correct even though its value is not accepted X++ syntax.

X++ Functions


In Microsoft Dynamics AX, the X++ language provides more than 100 system functions that are not part of any class. The two types of functions are as follows:
  • Run time – functions that are executed during run time.
  • Compile time – functions that are executed during compile time.

Run time functions used for data type conversions, mathematical operations, and so on. Some common run time functions are as follows:
                 str2Int – creates an int value from a str value.
                 abs – creates a positive real value from a real value that is either positive or negative.
                 conFind – retrieves the location of an element in a container.


Call Runtime Functions from .Net


       The logic of the X++ run time functions is also implemented in the following .NET assembly:
        Microsoft.Dynamics.AX.Xpp.Support.DLL


         Inside the previous assembly, the X++ run time functions are implemented as static methods of the following class:Microsoft.Dynamics.AX.Xpp.PredefinedFunctions


Compile time functions are executed early in the compile of X++ code. Most of these functions retrieve metadata about items that are in the Application Object Tree (AOT). Some common compile time functions are as follows:
                 classNum – retrieves the ID of a class.
                 classStr – verifies during compile time that a class by that name exists. This is better than discovering the error later during run time.
                 evalBuf – evaluates the input string of X++ code, and then returns the results as a string.
                 literalStr – retrieves a label ID when given the string representation of a label, such as the string "@SYS12345".       For example, myLabel.exists(literalStr("@SYS12345"));


Note:
1) Compile time functions are sometimes called intrinsic functions.
2) X++ compile time functions cannot be called from a .NET program.


How to doInsert the values to a table ?


A new record is inserted with the name 'My name' in the name field and the value 100 in the value field.
ttsBegin;
 
myTable.name = 'My name';
myTable.value = 100;
 
myTable.doInsert();
 
ttsCommit;

How to insert and update a record into a table?


Following example inserts a new record into the MyTable table, with the AccountNum set to 1000 and the Name set to MyName (other fields in the record will be blank).

MyTable  MyTable
;
ttsBegin;

select MyTable ;
MyTable.AccountNum = '1000';
MyTable.Name = 'MyName';
MyTable.insert();
  
ttsCommit;

Following example updates the record in the MyTable table  with the AccountNum  1000 ,AccountNum set 1000 to "100012" and  the Name set MyName to NewName 
MyTable  MyTable
;
ttsBegin;
  
select forUpdate MyTable where MyTable.AccountNum = "1000";
 ;
MyTable.AccountNum = '100012';
MyTable.Name = 'NewName';
MyTable.update();
  
ttsCommit;

How to update a table ?


For example selects the table myTable for update. Any records with the name equal to 'my name' are updated. The AmountMST field has 100 added to its current value, and the AccountNum field is changed to account number 1050.
tsBegin;
select forUpdate myTable
         where myTable.Name=='My name';
 
myTable.AmountMST += 100;
myTable.AccountNum = 1050;
 
MyTable.update();
 
ttsCommit;

how to design a form through the code


static void createForm(Args _args)
{
    Args args;
    Form form;
    FormRun formRun;
    FormBuildDesign formBuildDesign;
    FormBuildDataSource formBuildDataSource;
    FormBuildDataSource formBuildDataSource2;
    FormBuildGridControl formBuildGridControl;
    FormDataSource formDataSource;
    DictTable dictTable;
    DictTable dictTable2;
    int idx;
    ;
    // Create the form header.
    form = new Form();

    // Add data sources to the form.
    dictTable = new DictTable(tablenum(CustTable));
    formBuildDataSource = form.addDataSource(dictTable.name());
    formBuildDataSource.table(dictTable.id());
    dictTable2 = new DictTable(78);
    formBuildDataSource2 = form.addDataSource(dictTable2.name());
    formBuildDataSource2.table(dictTable2.id());

    // Create the form design.
    formBuildDesign = form.addDesign("Design");

    // Add a grid control.
    formBuildGridControl =
 formBuildDesign.addControl(FormControlType::Grid, "Table Grid");
    formBuildGridControl.dataSource(dictTable.name());

    // Add a data field to the grid control.
    formBuildGridControl.addDataField(formBuildDataSource.id(),
    dictTable.fieldName2Id("AccountNum"));

    args = new Args();
    args.object(form);

    // Create the run-time form.
    formRun = new FormRun(Args);
    formRun.run();
    formRun.detach();

    // Return an object for the first data source,
    // and then display a query form.
    formdatasource = formRun.dataSource(1);
    formdatasource.prompt();
}