Tuesday, February 12, 2013

SSRS report using Ax-Query and Ranges

http://community.dynamics.com/product/ax/axtechnical/b/axsantoshkumar/archive/2010/01/08/dynamics-ax-simple-ssrs-report-example-by-using-ax-query-and-ranges.aspx

Dynamics Ax - Simple SSRS report example by using Ax-Query and Ranges
------------------------------------------------------------
In this report I am going to use the Ax-Query in the SSRS business logic and report will be generated by using SSRS Business logic.

For this report…

1) Define the sample Ax Query in Dynamics Ax as follows...





2) In the above SampleCustomers query I had defined the ranges for the Name Field.


3) Now go to VS-2008 Create New Project for Dynamics Ax SSRS reports.


4) Open the report from the Solution explorer and Add the new DataMethod to the report as follows




5) Rename the DataMethod and do the code as follows

[DataMethod(), AxSessionPermission(SecurityAction.Assert)]

public static DataTable CustomerDetails(String custAccName)

{

DataTable custDataTable = new DataTable();

IDictionary<string, object> ranges = new Dictionary<string, object>();

DataRow customersRow;

DataTable dtASCAllRows = new DataTable("Customers");

DataView dvASC = new DataView();



//Defining ranges

object[] rangeName = new object[] { "CustTable.Name" };

object[] rangeValue = new object[] { custAccName };


ranges.Add(rangeName[0].ToString(), rangeValue[0]);


// execute query - "SampleCustomers" is our Ax Query

dtASCAllRows = AxQuery.ExecuteQuery((String.Format("Select * from {0}", "SampleCustomers"))

, ranges);


dvASC.Table = dtASCAllRows;


custDataTable.Columns.Add("AccountNum", typeof(string));

custDataTable.Columns.Add("AccountName", typeof(string));

custDataTable.Columns.Add("CustGroup", typeof(string));


// Loop for fetching data for every record

for (int intLoopCounter = 0;

intLoopCounter < dvASC.Count; intLoopCounter++)

{


customersRow = custDataTable.NewRow();


customersRow["AccountNum"] = Convert.ToString(dvASC[intLoopCounter]["AccountNum"]);

customersRow["AccountName"] = Convert.ToString(dvASC[intLoopCounter]["Name"]);

customersRow["CustGroup"] = Convert.ToString(dvASC[intLoopCounter]["CustGroup"]);


custDataTable.Rows.Add(customersRow);

}


return custDataTable;

}

6) Define the new Dataset for the report.

7) Assign the properties of the dataset as follows and Map our DataMethod (CustomerDetails) to the Query Property of the DataSet.




8) Save the solution. Drag and drop the Dataset to the Designs node of the report.


9) Now we can see the preview of the report by using the Customer name as filter condition.

Assign secuirty key for unassigned AOT objects

AOT Object Assign New SecKey
static void EXU_AOTObject_AssignNewSecKey_BUS(Args _args)
{
    #AOT
    #Properties
    TreeNode            path,treeNode,node;
    EXU_Tree            exuTree;
    TreeNodeIterator    iterator;
    Map                 map= new Map(Types::Integer,Types::String);
    MapEnumerator       mapEnumerator;
    UtilElementType     elementType;
    str                 name,newKeyName,newObjectName;
    boolean             ret,newObjectRet,checkSecurityKeyExist;
    TreeNode            newSecuirtyTreenode, setOnObject;
    #define.DEL('DEL_')
    #define.TMP('TMP')
    #define.EXU('EXU_')
    #define.SecurityKey("SecurityKey")
    #define.DisplayMenuItem("DisplayMenuItem")
    #define.ActionMenuItem("ActionMenuItem")
    #define.OutputMenuItem("OutputMenuItem")
    #define.Menu("Menu")
    #define.View("View")
    #define.Table("Table")
    ;
    map.insert(1,#DisplayMenuItem);
    map.insert(2,#ActionMenuItem);
    map.insert(3,#OutputMenuItem);
    map.insert(4,#Menu);
    map.insert(5,#View);
    map.insert(6,#Table);
    mapEnumerator = map.getEnumerator();
    while(mapEnumerator.moveNext())
    {
        iterator = null;
        treeNode = null;
        info(mapEnumerator.currentValue());
        switch(any2int(mapEnumerator.currentKey()))
        {
            case 1:
                path = treenode::findNode(#MenuItemsDisplayPath);
            break;
            case 2:
                path = treenode::findNode(#MenuItemsActionPath);
            break;
            case 3:
                path = treenode::findNode(#MenuItemsOutputPath);
            break;
            case 4:
                path = treenode::findNode(#MenusPath);
            break;
            case 5:
                path = treenode::findNode(#ViewsPath);
            break;
            case 6:
                path = treenode::findNode(#TablesPath);
            break;
            default:
                continue;
         }
        iterator = path.AOTiterator();
        treeNode = iterator.next();
        while(treeNode)
        {
            name = SysTreeNode::applObjectName(treeNode.treeNodePath()); // Return the current Object Name
            if(strScan(name,#DEL,1,strLen(name))|| strScan(name,#TMP,1,strLen(name)))  // Check Object Name start with  'DEL_' or 'TMP'
            {
                ret = true;
            }
            if(SysTreeNode::isNodeInLayer(treeNode, UtilEntryLevel::bus) && //Check Object exist in BUS layer or not
               treeNode.AOTgetProperty(#PropertySecuritykey) == '' && // Check Object Property i.e.(Security Key is blank)
               !ret)
            {
                newKeyName    = #EXU + name ; // A new unique security key name
                newObjectName = treeNode.newObjectName(newKeyName); // Suggest a new name for the object if exist else return the same name
                if(newKeyName != newObjectName) // Check new Object Name exist in AOT or not
                {
                    checkSecurityKeyExist = true; // If name already exist then checkSecuirtyKeyExist = true;
                }
                if(newKeyName)
                {
                    /*if(!checkSecurityKeyExist)
                    {
                        newSecuirtyTreenode = TreeNode::findNode(#SecurityKeysPath); //Find the security key node in AOT
                        newSecuirtyTreenode.AoTadd(newKeyName); // Create and add a new Secruity Key to AOT
                        sqlDataDictionary::Synchronize();
                    }
                    setOnObject = TreeNode::findNode(treeNode.treeNodePath()); //Find the current Object in the AOT
                    setOnObject.AOTsetProperty(#SecurityKey,newKeyName); //Assign a newly created Security Key to Objects Properties
                    setOnObject.AOTsave();*/
                    info(strfmt("Object Name : %1 ,Security Key Name : %2 , Path : %3, Object type: %4, Layer : %5, Configuration Key : %6",
                    name,
                    newKeyName,
                    treeNode.treeNodePath(),
                    any2int(mapEnumerator.currentKey()) > 3 ? mapEnumerator.currentValue() : treeNode.AOTgetProperty(#PropertyObjectType),
                    'bus',
                    treenode.AOTgetProperty(#PropertyConfigurationkey)
                    ));
                }
            }
            treeNode = treenode.AOTnextSibling();
            checkSecurityKeyExist = false;
            ret = false;
        }
    }
}

AOT Objects which are not having Security Key

Finding AOT objects which are not having Security key

static void EXU_AOTObject_NoSecKey_BUS(Args _args)
{
    #AOT
    #Properties
    TreeNode            path,treeNode,node;
    EXU_Tree            exuTree;
    TreeNodeIterator    iterator;
    Map                 map= new Map(Types::Integer,Types::String);
    MapEnumerator       mapEnumerator;
    ;
    map.insert(1,"DisplayMenuItem");
    map.insert(2,"ActionMenuItem");
    map.insert(3,"OutputMenuItem");
    map.insert(4,"Menu");
    map.insert(5,"View");
    map.insert(6,"Table");
    mapEnumerator = map.getEnumerator();
    while(mapEnumerator.moveNext())
    {
        iterator = null;
        treeNode = null;
        info(mapEnumerator.currentValue());
        switch(any2int(mapEnumerator.currentKey()))
        {
            case 1:
                path = treenode::findNode(#MenuItemsDisplayPath);
            break;
            case 2:
                path = treenode::findNode(#MenuItemsActionPath);
            break;
            case 3:
                path = treenode::findNode(#MenuItemsOutputPath);
            break;
            case 4:
                path = treenode::findNode(#MenusPath);
            break;
            case 5:
                path = treenode::findNode(#ViewsPath);
            break;
            case 6:
                path = treenode::findNode(#TablesPath);
            break;
            default:
                continue;
         }
        iterator = path.AOTiterator();
        treeNode = iterator.next();
        while(treeNode)
        {
            if(SysTreeNode::isNodeInLayer(treeNode, UtilEntryLevel::bus))
            {
                if(treeNode.AOTgetProperty(#PropertySecuritykey) =='')
                {
                    info(strfmt("Path : %1, Object type: %2, Layer : %3, Configuration Key : %4",
                    treeNode.treeNodePath(),
                    any2int(mapEnumerator.currentKey()) > 3 ? mapEnumerator.currentValue() : treeNode.AOTgetProperty(#PropertyObjectType),
                    'bus',
                    treenode.AOTgetProperty(#PropertyConfigurationkey)
                    ));
                 }
            }
            treeNode = treenode.AOTnextSibling();
        }
    }
}

Query by code in AX

Query Using Dyna Link

http://community.dynamics.com/product/ax/axtechnical/b/axaptavsme/archive/2012/07/17/building-a-query-object.aspx
static void CustTableSales(Args _args)
{
Query query;
QueryBuildDataSource qbds1;
QueryBuildDataSource qbds2;
QueryBuildRange qbr1;
QueryBuildRange qbr2;
QueryRun queryRun;
CustTable custTable;
;
query = new Query();
qbds1 = query.addDataSource(tablenum(CustTable));
qbds1.addSortField(
fieldnum(CustTable, Name),
SortOrder::Ascending);
qbr1 = qbds1.addRange(fieldnum(CustTable,Blocked));
qbr1.value(queryvalue(CustVendorBlocked::No));
qbr2 = qbds1.addRange(fieldnum(CustTable,CustGroup));
qbr2.value(queryvalue(’10′));
qbds2 = qbds1.addDataSource(tablenum(SalesTable));
qbds2.relations(false);
qbds2.joinMode(JoinMode::ExistsJoin);
qbds2.addLink(
fieldnum(CustTable,AccountNum),
fieldnum(SalesTable,CustAccount));
queryRun = new QueryRun(query);
while (queryRun.next())
{
custTable = queryRun.get(tablenum(CustTable));
info(strfmt(
“%1 – %2″,
custTable.Name,
custTable.AccountNum));
}
}

Query Using without Dyna Link

static void Query_Example(Args _args)
{
    Query q;
    Queryrun qr;
    QueryBuildRange qbr;
    QueryBuildDataSource qbds;
    InventTrans iv;
    Real Total;
    str range;
    
    /* The following query produces the same results as:
    while select sum(qty) from inventTrans
        where (inventtrans.ItemId == "OL-1500") || inventtrans.ItemId == "OL-1000"
            join inventDim
                group by inventBatchId
                where inventDim.InventDimId == inventTrans.InventDimId */

    // Instantiate the query class.
    q = new query("Inventory_Transactions"); 
   
    // Create a data source by using the InventTrans table.
    qbds = q.addDataSource(tablenum(InventTrans));
 
    // Select only the Qty field, and then sum the Qty field.
    qbds.addSelectionField(fieldnum(InventTrans,Qty),selectionfield::Sum); 

    // Set the range to the ItemId field.
    qbr = qbds.addRange(fieldnum(InventTrans,ItemId));  

    // The range for the where statement specifies an 'or' statement.
    qbr.value(strfmt('((%1 == "%2") || (%1 == "%3"))',fieldstr(inventtrans,ItemId),'OL-1500','OL-1000'));

    // The following is the alternative way to enter the range.
    // This also limits the selection on the ItemId values.
    range = strfmt('((ItemId == "%1")||(ItemID =="%2"))',queryvalue('OL-1500'),queryvalue('OL-1000'));
    qbr.value(range);    

    // Create the join to the InventDim table.
    qbds = qbds.addDataSource(tablenum(InventDim));
  
    // Specify the table relationship.
    qbds.relations(true);
  
    // Indicate the order mode as the grouping.
    qbds.orderMode(ordermode::GroupBy);
 
    // Specify the grouping on the InventBatchId field.
    qbds.addSortField(fieldnum(InventDim,InventBatchId)); 

    // Instantiate the QueryRun class for the form.
    qr = new QueryRun(q);  

    // If the user clicks OK, continue.
    if (qr.prompt())  
    {

        // While there are records in the query, continue.
        while (qr.next())  
        {
            
           // Set the value of the query to the iv table. 
           bufferiv = qr.get(tablenum(InventTrans)); 

            // Create the value of the total field.
           total =  iv.Qty;  
        }
    }
    // Specify the quantity for the item.
    info(strfmt("Quantity: %1",total));  
    // Indicate the SQL string that is used for the query.
     info (qr.query().dataSourceNo(1).toString());  
}