February 08, 2019

How to get QueryRange from Query object X++

Hi Folks,

This post is about how to get QueryRange from a query object in x++ code. This is very useful when you have to pass a query on another object and perform a certain operation based on this query and its ranges.

To understand this code sample better, let's take a scenario. On FormA I have to build a query (OR use AOT query) on run time based on certain ranges. When the user clicks on a button system will pass this query to FormB and on FormB we have to use this query and its ranges to perform other sets of operations.

Now let's see the code, which may need to write on Form B to take a query as a parameter and fetch the ranges and its values in code.

Query                                        query = New Query();
QueryBuildDataSource             qbds1;
QbueryBuildRange                   qbr1;

query              = queryStr(<AOT Query Name OR query object that you passed from Form A>) ;
qbds1             = query .dataSourceNo(1); //Dont use dataSourceName(<DS name>) it will give you run time error. Double check the data source sequence on your query.
qbr1               = qbds1.range(1); //If there are more than one ranges on DS use 1, 2, 3, and so on.

info(strFmt('Range field is %1: Range values is%2:', qbr1.fieldName(), qbr1.value()));


Now let's assume there are more than one data source on the query and each data source may have more than one ranges or ranges are unknown. In such case, you can use a for loop to get all data sources on query and ranges.

Query                                        query = New Query();
QueryBuildDataSource             qbds;
QbueryBuildRange                   qbr;
int                                              totalDS, totalRanges;

query              = queryStr(<AOT Query Name OR query object that you passed from Form A>) ;
totalDS           = query.dataSourceCount();

for(int ds =1; ds<= totalDS; ds++)
{
      qbds = = query.dataSourceNo(ds);
       totalRanges = qbds.rangeCount();
      for(int r =1; r<= totalRanges; r++)
      {
          qbr =  qbds.range(r);
          info(strFmt('Range field is %1: Range values is%2:', qbr.fieldName(), qbr.value()));
      }
}

I hope this will help.

-Cheers!!!
Harry
PS: My VM is down for some reason and I couldn't prove the code and have to write it manually so you might get some syntax issues in the above code.

No comments:

Post a Comment

Thanks

Note: Only a member of this blog may post a comment.