Use Count properties on query builder results and find queries

As a performance best practice, Guidewire recommends that you obtain counts of items fetched from the application database by using the Count properties on query builder result objects. The same recommendation applies to find expression query objects. Do not iterate result or query objects to obtain a count.

Important: Guidewire recommends the query builder APIs instead of find expressions to fetch items from the application database whenever possible, especially for new code. For more information, see Query builder APIs.

Use Empty properties if you want to know whether anything was found

If you want to know only whether a result or query object fetched anything from the application database, use the Empty property instead of the Count property. The value of the Empty property returns to your Gosu code faster, because the evaluation stops as soon as it counts at least one fetched item. In contrast, the value of the Count property returns to your Gosu only after counting all fetched items.

The following sample Gosu code uses the Count property on a query builder API result object.

uses gw.api.database.Query 

var policyPeriodQuery = Query.make(PolicyPeriod) // Find all policy periods.
var result = policyPeriodQuery.select()

if (result.Empty) {  // Does the result fetch anything? 
  print ("Nothing found.")
}
else {
  print ("Got some!") 
}

The following sample Gosu code uses the Count property on a find expression query object.

var policyPeriodQuery = find(p in PolicyPeriod)  // Find all policy periods.

if (policyPeriodQuery.Empty) {  // Did the query fetch anything? 
  print ("Nothing found.")
}
else {
  print ("Got some!") 
}

Use Count properties if you want the number found

The following sample Gosu code uses the Count property on a query builder API result object.

uses gw.api.database.Query

var policyPeriodQuery = Query.make(PolicyPeriod) // Find all policy periods.
var result = policyPeriodQuery.select()
print("Number of policy periods: " + result.Count)

The following sample Gosu code uses the Count property on a find expression query object.

var policyPeriodQuery = find(p in PolicyPeriod)  // Find all policy periods.
print("Number of policy periods: " + policyPeriodQuery.Count)