SysQuery::findOrCreateRange() - A better way to create dynamic query ranges

When creating dynamic query ranges in X++, it is considered a best practice to create a range only if it has not been already created. I have seen examples of code where the addRange() method is called inside an executeQuery() method. This has a performance hit and should be avoided. The best approach is to create the range in the init() of the datasource. In the executeQuery(), we can find the range first, if it does not exists or has not been created at that point, create a new one and apply the values.

I found a helper method on the SysQuery class which did exactly this. The method is SysQuery::findOrCreateRange(). The method takes two parameters, a datasource to add the range to and the range itself. It returns a range object.

Comments

Popular posts from this blog

How to add empty ranges in query

Get selected records in Dynamics AX 2012

The field with ID '0' does not exist in table - Cause and resolution.