How to Discover and Use SysQueryRangeUtil Methods in D365FO

How to Discover and Use SysQueryRangeUtil Methods in D365FO

D365FO contains many useful query range functions, such as currentSessionDate(), dayRange(), and currentUserId(). Microsoft, ISVs, and customers can also add new functions.

This creates a simple problem: how can we build a lookup that always shows every available function without maintaining a hardcoded list?

The answer is X++ metadata reflection. In this article, we will discover methods marked with QueryRangeFunctionAttribute, filter them by the selected field type, and cache the result for fast lookup access.

Decorating supported methods

A query range function is implemented as a static X++ method decorated with QueryRangeFunctionAttribute. The method can be placed in SysQueryRangeUtil or in another class.

Here are two examples:

  • Code sample from SysQueryRangeUtil standard class

  • Code sample from DocQueryRangeUtil custom class

X++ lets the Attribute suffix be omitted when applying an attribute. This is why you can decorate in two different ways, but the latter is cleaner.

Collecting methods with X++ reflection

Once we know what identifies a query range function, we can scan the X++ class metadata. For each class, we inspect its methods and check whether a method is decorated with QueryRangeFunctionAttribute.

Code sample:

Query range functions are not limited to SysQueryRangeUtil. Because Microsoft, SI partners, ISVs, and customers can place attributed methods in other classes, the discovery process scans all available classes.

Filtering functions by field type

Query range methods have different return types.

Depending on the selected range field type, we filter the lookup with relevant methods to be used in the criteria.

To do so, we follow the next steps:

  1. Read the method return type from SysDictMethod.
  2. Compare it with the selected query field type.
  3. Include methods with a compatible return type.
  4. Always include methods returning Types::String or Types::RString.

Code sample:

Doing so, you will still see string return types as it is a special case we’ll talk next.

Special handling for String and RString

A query range function does not always return the field’s underlying type. Many functions return a formatted range expression as String or RString. That expression can represent a single value (e.g., number, date, etc.), multiple values, an interval, or a comparison operator.

Therefore, methods returning Types::String or Types::RString must remain available for every field type.

Code sample from SysQueryRangeUtil standard class:

Supporting custom query range methods

As mentioned, anyone can create new query range methods.

Create a class, add a static method, and decorate it with QueryRangeFunctionAttribute.

The following standard Microsoft class demonstrates that query range functions are not limited to SysQueryRangeUtil.

The following query range function code sample comes from AssetLeaseSysQueryRangeUtil:

Caching the result

Scanning all X++ classes and methods is too expensive to perform every time the user opens the lookup.

For this reason, the query range method metadata is loaded asynchronously and stored in SysGlobalObjectCache using the DocGlobalObjectCacheHelper class.

You can learn more about SysGlobalObjectCache in this article.

Code sample of a runAsync call performed when cache does not exist:

Code sample warning the user that the cache is not loaded yet:

Code sample extracting the cache:

Conclusion

By using X++ metadata reflection, we can discover query range functions without maintaining a hardcoded list.

The discovery process finds static methods decorated with QueryRangeFunctionAttribute, reads their metadata, and filters them according to the selected query field. Methods returning String or RString remain available because they can produce range expressions for any field type.

Because the scan covers all D365FO X++ classes across all models, the result is loaded asynchronously and cached. New custom functions outside the SysQueryRangeUtil class can then appear automatically in the Docentric Query Browser lookup.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Docentric respects your privacy. Learn how your comment data is processed >>

Docentric respects your privacy. Learn how your comment data is processed >>