
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
|
1 2 3 4 5 |
[QueryRangeFunctionAttribute()] public static str currentLanguageId() { return LanguageTable::defaultLanguage(); } |
- Code sample from DocQueryRangeUtil custom class
|
1 2 3 4 5 6 7 8 9 |
[QueryRangeFunction] public static str isRunningInBatch() { if (isRunningOnBatch()) { return #AllRangeValue; } return #NoRangeValue; } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
SysDictionary dictionary = new SysDictionary(); int classCount = dictionary.classCnt(); for (int classIndex = 1; classIndex <= classCount; classIndex++) { ClassId classId = dictionary.classCnt2Id(classIndex); SysDictClass dictClass = new SysDictClass(classId); boolean includeObjectMethods = false; boolean includeStaticMethods = true; boolean includeInheritedMethods = false; Set methods = dictClass.methods(includeObjectMethods, includeStaticMethods, includeInheritedMethods); SetEnumerator methodsEnum = methods.getEnumerator(); while (methodsEnum.moveNext()) { SysDictMethod dictMethod = methodsEnum.current(); if (dictMethod.getAttribute(classStr(QueryRangeFunctionAttribute))) { str methodName = dictMethod.name(); str parameters; int parameterCount = dictMethod.parameterCnt(); for (int parameterIndex = 1; parameterIndex <= parameterCount; parameterIndex++) { if (parameters) { parameters += ', '; } str parameterTypeStr = dictMethod.parameterTypeName(parameterIndex); if (!parameterTypeStr) { parameterTypeStr = enum2Str(dictMethod.parameterType(parameterIndex)); } parameters += strFmt('%1 %2', parameterTypeStr, dictMethod.parameterName(parameterIndex)); } Types returnType = dictMethod.returnType(); str returnTypeStr = dictMethod.returnTypeName(); if (!returnTypeStr) { returnTypeStr = enum2Str(returnType); } else if (returnType == Types::UserType) { returnType = extendedTypeId2Type(extendedTypeName2Id(returnTypeStr)); } str fullMethod = strFmt('%1(%2)', methodName, parameters); } } } |
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:
- Read the method return type from SysDictMethod.
- Compare it with the selected query field type.
- Include methods with a compatible return type.
- Always include methods returning Types::String or Types::RString.
Code sample:
|
1 2 3 4 |
boolean isApplicable = methodReturnType == fieldType || methodReturnType == Types::String || methodReturnType == Types::RString; |
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:
|
1 2 3 4 5 |
[QueryRangeFunctionAttribute()] public static str currentSessionDate() { return SysQueryRangeUtil::value(DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone())); } |
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:
|
1 2 3 4 5 6 7 |
[QueryRangeFunction()] internal static str liabilityPostingTypeRange() { return strFmt('%1,%2', Enum2int(AssetLeasePostingTypes::LeaseLiability), Enum2int(AssetLeasePostingTypes::ShortTermLeaseLiability)); } |
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.
Code sample of a runAsync call performed when cache does not exist:
|
1 2 3 4 5 6 |
if (!DocGlobalObjectCacheHelper::existStatic(classNum(DocQueryRangeMethodTmpCache))) { runAsync(classNum(DocQueryRangeMethodTmpCache), staticMethodStr(DocQueryRangeMethodTmpCache, populate), conNull()); // no parameters needed for this case } |
Code sample warning the user that the cache is not loaded yet:
|
1 2 3 4 5 |
str scope = classStr(DocQueryRangeMethodTmpCache); if (!DocSysGlobalObjectCacheHelper::exist(scope, DocSysGlobalObjectCacheHelper::minKey())) { DocGlobalHelper::handleWarning("@DocentricAX5:QueryRangeMethodsStillLoading", true, funcName(), true); } |
Code sample extracting the cache:
|
1 2 3 4 5 |
container cachedValue = DocSysGlobalObjectCacheHelper::get(scope, DocSysGlobalObjectCacheHelper::minKey()); if (cachedValue) { // calls to methods to extract the data } |
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.
Tags: D365FO, X++