mercredi 14 mars 2012

How to: Create Queries by Using X++ [AX 2012]


In Microsoft Dynamics AX, you can create a query to retrieve data by using the query classes. For more information, see Query Object Model. In addition, by using X++ code, you can create a query and then save that query to the Application Object Tree (AOT).
You can also create a query by using the AOT. For more information, see How to: Create Queries by Using the AOT.
  1. Add a class to the AOT. For more information about adding a class, see Declaration of Classes.
  2. Add a class method that creates a query by using the Query system classes.
    In the following example, the QueryRun class runs the query specified by the q object. The addDataSource method specifies CustTable as the data source for the query. The addRange and value methods specify account number 4005 for the query range. The addSortField method specifies that data is sorted on the Name field.
    public void runMyDynamicQuery2()
    {
    Query q;
    QueryRun qr;
    QueryBuildDataSource qbd;
    QueryBuildRange qbr;

    q = new Query();
    qbd = q.addDataSource(TableNum(CustTable));

    qbr = qbd.addRange(FieldNum(CustTable, AccountNum));
    qbr.value(">=4000"); // Default operator is ==.

    qbr = qbd.addRange(FieldNum(CustTable, AccountNum));
    qbr.value("<=4022");

    qbd.addSortField(FieldNum(CustTable, DlvMode));

    qr = new QueryRun(q);
    qr.prompt();

    pause;
    }
  1. Add a main method to the class, and call the method that you created in step 2.
  2. Create an action menu item to reference the class by clicking Menu Items, right-clicking Action, and then clicking New Menu Item in the AOT.
  3. Right-click the menu item, click Properties, and set ObjectType to Class. Then select the class that you created in step 1 from the Object property list.
    You can include the menu item in forms and reports by configuring form and report controls to reference the menu item. For more information, see Form Control Properties.
The following procedure is a job that you can run in the AOT to create a query called MyQuery1, provided MyQuery1 does not already exist in the Queries node.
  1. In the AOT, right-click Jobs, and then click New Job. The Code editor window opens.
  2. In the Code editor window, copy the following code, and then paste it in the Code editor.
    static void CreateQuery5Job(Args _args)
    {
    TreeNode treeNode;
    Query query;
    QueryBuildDataSource qbds;
    QueryBuildRange qbr;
    str queryName = "MyQuery1";

    // Macro.
    #AOT

    treeNode = TreeNode::findNode(#QueriesPath);
    query = treeNode.AOTfindChild(queryName);
    if (!query)
    {
    treeNode.AOTadd(queryName);
    query = treeNode.AOTfindChild(queryName);

    qbds = query.addDataSource(tablenum(CustTable));
    qbr = qbds.addRange(fieldnum(CustTable, DlvMode));
    qbr.value(">4005");

    query.AOTcompile(1);
    query.AOTsave();
    }
    else
    {
    Global::info("A query with that name already exists.");
    }
    }
  3. Press F7 to compile, and then press F5 to run the job. A query named MyQuery1 is created in the Queries node.

Aucun commentaire:

Enregistrer un commentaire