How to Execute a Fluent Query

To execute a fluent query in a server automation script:

  1. Use the server.query.getAlias method to define an entity alias.
  2. Use the server.query.from method to run the query on the desired entity.
  3. Use the .selectColumns method to select the returned attributes.
  4. Use the .execute() method to run the query.
Copy
var A = server.query.getAlias('Account');

var myFluentQuery = server.query.from('Account', A)
.selectColumns (A.Name, A.Email)
.execute();

log(myFluentQuery)

The code above will log an output similar to the following in the trace_roll.log file:

Copy
-[START]---------------------------------------
Timestamp: 7/1/2020 5:01:51 PM
 Message: INFO [CID=200524bb-8d28-4079-80c3-a003a9ecad7d] [
{
"values": {
"A_Name": "Jane Doe",
"A_Email": "janedoe@fintechos.com"
}
},
{
"values": {
"A_Name": "Andrew Jones",
"A_Email": "adrew.jones@fintechos.com"
}
},
{
"values": {
"A_Name": "John Doe",
"A_Email": "john.doe@fintechos.com"
}
}
]
 Severity: Information
-[END]---------------------------------------

The entity alias definition enables intelligent code completion for the corresponding entity attributes:

Comparison Operators

The following comparison operators are supported in fluent queries: equals (eq), notEquals (neq), greaterThan (gt), greaterThanOrEquals (gt), lessThan (lt), and lessThanOrEquals (lte)

Logical Operators

The following logical operators are supported in fluent queries: and, or, andNot, and orNot.

Inner Joins

Use the .innerJoin and .on methods to define inner join clauses for the queried entities.

Copy
var O = server.query.getAlias('optionset');
var A = server.query.getAlias('attribute');


var myFluentQuery = server.query.from('attribute', A)
.innerJoin('optionset', O)
.on(A.OptionSetId.eq(O.OptionSetId))
.top(5)
.orderBy(A.Name)
.execute()

Left Joins

Use the .leftJoin and .on methods to define left outer join clauses for the queried entities.

Copy
var O = server.query.getAlias('optionset');
var A = server.query.getAlias('attribute');

var myFluentQuery = server.query.from('attribute', A)
.leftJoin('optionset', O)
.on(A.OptionSetId.eq(O.OptionSetId))
.top(5)
.selectColumns(A.Name, O.DisplayName)
.orderBy(A.Name)
.execute();

Attribute Aliases (Projections)

To define aliases for the queried attributes, use the .selectProjection method instead of .selectColumns and the .executeAndMap method instead of .execute.

Projections are useful to customize the result set field names when different entities have similar attribute names or when attribute names are not expressive (see Working with Fluent Query Result Sets for more details about fluent query outputs).

Where Clauses

Where clauses are implemented using the .where, .wherenot, .andWhere, .orWhere, .andWhereNot, and .orWhereNot methods.

Aggregate Functions

To define aggregate functions on a set of values from the result set, use the .getCountAlias, .getSumAlias, .getMaxAlias, and .getMinAlias methods of the server.query property.