Using a Predefined View Criteria on a Standard Object

Standard objects may predefine named view criteria you can use in your scripts to simplify common searches. See a particular standard object's documentation to learn whether it defines any named view criteria.

After learning the name of the view criteria you want to employ, use the copyNamedViewCriteria() function to use it in your script. The example below shows the code you need to work with a fictitious ServiceTicket standard object that predefines an AllSeverityOneOpenTickets named view criteria.

/*
 * Query all open severity 1 service tickets
 * using a predefined view criteria
 */
// 1. Use newView() to get a view object
def vo = newView('ServiceTicket')
// 2. Copy predefined named view criteria
def vc = copyNamedViewCriteria(vo,'AllSeverityOneOpenTickets')
// 3. Append view criteria to the view object
vo.appendViewCriteria(vc)
// 4. Execute the query
vo.executeQuery()

Named view criteria may reference named bind variables in their filter criteria. Your code can (or sometimes must!) assign a value to one or more these bind variables for the view criteria to work correctly. After consulting the documentation for the standard object you are working with, if it mentions that named bind variables must be set, then use the setBindVariable() function to assign a value to these in your script before executing the query. The example below shows the code you need to work with a fictitious ServiceTicket standard object that predefines an AllOpenTicketsByAssigneeAndPriority named view criteria. The Bind_MaxPriority bind variable might default to the value 4, so it may be optional to set it in your script. In contrast, the Bind_Assignee bind variable would likely not have a default value and your script must provide a value. Failure to do this would result in the query's returning no rows.

/*
 * Query all open tickets less than a given priority 
 * assigned to a given support engineer
 */
// 1. Use newView() to get a view object
def vo = newView('ServiceTicket')
// 2. Copy predefined, named view criteria
def vc = copyNamedViewCriteria(vo,'AllOpenTicketsByAssigneeAndPriority')
// 3. Append view criteria to the view object
vo.appendViewCriteria(vc)
// 4. Set optional Bind_Priority bind variable
setBindVariable(vo,'Bind_MaxPriority',2)
// 5. Set mandatory Bind_Assignee bind variable
setBindVariable(vo,'Bind_Assignee','psmith')
// 6. Execute the query
vo.executeQuery()