Search This Blog

Monday, February 20, 2012

AX2012 Consume Query Service


Consume QueryService and get the results as same as our AOT Queryrun.
The following sample is used to display the customer records with the custgroup ranges.
Create the .Net Solution and add the console project for that solution and write the following program.

before writing the following code we have to take the service reference  http://localhost:8101/DynamicsAx/Services/QueryService.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using krishh_cosumeQueryService.QueryService;
using System.Data;
namespace krishh_cosumeQueryService
{
    class TestQueryService
    {
        static void Main(string[] args)
        {
            QueryServiceClient client = new QueryServiceClient();
            DataSet dataSet;

            //Set up paging so that 500 records are retrieved at a time
            Paging paging = new ValueBasedPaging() { RecordLimit = 500 };

            // Query object
            QueryMetadata query;
            //Datasource for the query
            QueryDataSourceMetadata CustTableds;
            // Ranges for the datasource
            QueryDataRangeMetadata range1, range2;
            //sorting object for the Datasource
            QueryDataOrderByMetadata sort;
            //fields for the query
            QueryDataFieldMetadata accountNum, bankAccount, commissionGroup, cashDisc, custGroup;

            query = new QueryMetadata();

            // Set the properties of the query.
            query.QueryType = QueryType.Join;

            query.DataSources = new QueryDataSourceMetadata[1];
            // Set the properties of the Customers data source.
            CustTableds = new QueryDataSourceMetadata();

            CustTableds.Name = "Customers DataSource";
            CustTableds.Enabled = true;
            //Add teh Table to the Datasource
            CustTableds.Table = "CustTable";

            //Add the data source to the query.
            query.DataSources[0] = CustTableds;
            // Setting DynamicFieldList property to false so I can specify only a few fields

            CustTableds.DynamicFieldList = false;

            CustTableds.Fields = new QueryDataFieldMetadata[5];

            //Add my fields
            accountNum = new QueryDataFieldMetadata();
            accountNum.FieldName = "AccountNum";
            accountNum.SelectionField = SelectionField.Database;
            CustTableds.Fields[0] = accountNum;


            bankAccount = new QueryDataFieldMetadata();
            bankAccount.FieldName = "BankAccount";
            bankAccount.SelectionField = SelectionField.Database;
            CustTableds.Fields[1] = bankAccount;

            commissionGroup = new QueryDataFieldMetadata();
            commissionGroup.FieldName = "CommissionGroup";
            commissionGroup.SelectionField = SelectionField.Database;
            CustTableds.Fields[2] = commissionGroup;

            cashDisc = new QueryDataFieldMetadata();
            cashDisc.FieldName = "CashDisc";
            cashDisc.SelectionField = SelectionField.Database;
            CustTableds.Fields[3] = cashDisc;

            custGroup = new QueryDataFieldMetadata();
            custGroup.FieldName = "CustGroup";
            custGroup.SelectionField = SelectionField.Database;
            CustTableds.Fields[4] = custGroup;


            //Add a range restriction on the query
            range1 = new QueryDataRangeMetadata();
            range1.TableName = "CustTable";
            range1.FieldName = "CustGroup";
            range1.Value = "DK Cust";
            range1.Enabled = true;

            range2 = new QueryDataRangeMetadata();
            range2.TableName = "CustTable";
            range2.FieldName = "CustGroup";
            range2.Value = "Empl";
            range2.Enabled = true;

            // add the ranges to the datasource
            CustTableds.Ranges = new QueryDataRangeMetadata[2];
            CustTableds.Ranges[0] = range1;
            CustTableds.Ranges[1] = range2;

            //Add a sort order on Customer Account number
            sort = new QueryDataOrderByMetadata();
            sort.DataSource = "Customers DataSource";
            sort.FieldName = "AccountNum";
            sort.SortOrder = SortOrder.Ascending;

            query.OrderByFields = new QueryOrderByMetadata[1];
            query.OrderByFields[0] = sort;
            // Execute the query
            int i = 0;
            do
            {
                dataSet = client.ExecuteQuery(query, ref paging);
                Console.WriteLine("Number of Records in CustTable: " + dataSet.Tables[0].Rows.Count.ToString());
                for(int J=0;J<dataSet.Tables[0].Rows.Count;J++)
                {
                    DataRow firstReturnedRow = dataSet.Tables[i].Rows[J];
                    Console.WriteLine("accountnum=" + firstReturnedRow[0].ToString());
                    Console.WriteLine("CustGroup=" + firstReturnedRow[4].ToString());
                }
                i++;
            }
            while (((ValueBasedPaging)paging).Bookmark != null);
            Console.ReadLine();
        }
    }
}

5 comments:

  1. Hi Krishh,

    in this program, i have to filter the records by "createdDateTime".

    Can you please tell me, how to apply filters/ Range for DateTime fields

    Thanks,
    Balachandar G

    ReplyDelete
  2. Hi Bala,
    When you want to use the DateTime states on Query service against table validTimeState then you have to use the following code
    Create StartDate, endDate variables.

    queryMetadata.ValidTimeStateQueryType = QueryMetadataServiceReference.ValidTimeStateQueryType.Range;
    queryMetadata.ValidTimeStateValidFromDateTime=startDate; queryMetadata.ValidTimeStateValidToDateTime = endDate;

    or if you have the Date Field on table
    first we have to retrive all fields
    This following line will retrieve all fields list in that table
    CustTableds.DynamicFieldList = true;
    after that you can add range of the Date field and assign the string Range for that column.

    ReplyDelete
  3. Hi Krishna,
    I have one question,
    where / how do you set the companyrange for a specific query ? I do not want to use the crosscompany reference.

    Thank's
    Alex

    ReplyDelete
    Replies
    1. Hi Alex,
      Normally you can set the query allowCrossCompany=No(by default it is no) so that it will work for that company.

      Regards,
      krishna.

      Delete
    2. Hi Krishna, Thanx's for our answer,
      problem is, that I then only get the data of the current user default company, is there a way to set ( just like a normal service when you use Callcontext ) a different company ? I saw something for the query called CompanyRanges
      any idea's one how to uses those ? i still cannot figure that one out. probebly in using the crosscompany ?

      Kind regards
      Alex

      Delete

Thanks for visiting my blog,
I will reply for your comment within 48 hours.

Thanks,
krishna.