Using String Substitution for Literal Values into a View Criteria Expression Used Only Once
If you're using a view object only a single time after calling
newView()
, use Groovy's built-in string substitution feature to replace
variable or expression values directly into the view criteria expression text as shown
below:
def vo = newView('StaffMember')
def loSal = 13500
def anon = 'Anonymous'
vo.appendViewCriteria("(Salary between ${loSal} and ${loSal + 1}) and LastName != '${anon}'")
vo.executeQuery()
Notice that you must still include single quotes around the literal string values. The
string subsitution occurs at the moment the string is passed to the
appendViewCriteria()
function, so if the values of the
loSal
or anon
variables change, their new values
are not reflected retroactively in the substituted string filter criteria expression. In
this example below, Groovy substitutes the values of the loSal
and
anon
into the view criteria expression string before passing it to
the appendViewCriteria()
function. Even though their values have
changed later in the script, when the vo.executeQuery()
is performed a
second time, the view object re-executes using the exact same filter expression as it
did before, unaffected by the changed variable values.
def vo = newView('StaffMember')
def loSal = 13500
def anon = 'Anonymous'
vo.appendViewCriteria("(Salary between ${loSal} and ${loSal + 1}) and LastName != '${anon}'")
vo.executeQuery()
// ... etc ...
loSal = 24000
anon = 'Julian'
// The changed values of 'loSal' and 'anon' are not used by the
// view criteria expression because the one-time string substitutions
// were done as part of the call to appendViewCriteria() above.
vo.executeQuery()
If you need to use a view object with appended view criteria filter expression multiple times within the same script, use named bind variables as described in the following section instead of string substitution. Using named bind variables, the updated values of the variables are automatically used by the re-executed query.