Query.apex

  • Docs
  • Tutorials
Docs Menu
  • Query class
    • Constructor
    • Result
    • Field Selection
    • Condition
    • Subquery
    • Aggregate Functions

Condition Guide

Condition methods in Query

byId

Add a condition to the query, filtering the result by a specific ID or a collection of ID. Alias to addConditionEq('Id', ...)

public Query byId(Id id)

id: The specified ID of the SObject


Query query =
    new Query('Account').
    byId();

public Query byId(List idList)

idList: A List of specified IDs


Query query =
    new Query('Account').
    byId(new List{'001O000000qkv3KIAQ'});

public Query byId(Set idSet)

idSet: A Set of specified IDs


Query query =
    new Query('Account').
    byId(new Set{'001O000000qkv3KIAQ'});

lookup

Add a condition to the query, filtering the result by a foreign key.

public Query lookup(String fieldName, Id id)

fieldName: The lookup field name

id: The Id to filter the lookup field


Query query =
    new Query('Contact').
    lookup('AccountId', '001O000000qkv3KIAQ');

public Query lookup(String fieldName, List idList)

fieldName: The lookup field name

idList: The list of Id to filter the lookup field


Query query =
    new Query('Contact').
    lookup('AccountId', new List{'001O000000qkv3KIAQ'});

public Query lookup(String fieldName, Set idSet)

fieldName: The lookup field name

idSet: The Set of Id to filter the lookup field


Query query =
    new Query('Contact').
    lookup('AccountId', new Set{'001O000000qkv3KIAQ'});

public Query lookup(String fieldName, SObject sobj)

fieldName: The lookup field name

sobj: The SObject record, whose Id will be taken to filter the lookup field


Query query =
    new Query('Contact').
    lookup('AccountId', new Account(Id = '001O000000qkv3KIAQ'));

public Query lookup(String fieldName, List sObjectList)

fieldName: The lookup field name

sObjectList: The list of SObject records, whose Id will be taken to filter the lookup field


Query query =
    new Query('Contact').
    lookup('AccountId', new List{new Account(Id = '001O000000qkv3KIAQ')});

addConditionXX

Add a condition to the query, limiting the sObject. 'XX' can be one of these comparison operators: Eq, NotEq, In, Lt, Le, Gt, Ge, Like.

By default, all the conditions added by this method will be joined by the boolean operator 'and', i.e. the result is true if and only if all the conditions are true, unless calling the switchToDisjunction method.

public Query addConditionEq(String lhs, Object rhs)

Add a condition lhs = rhs.

The result is true if and only if the left hand side value equals to the right hand side value.

lhs: Left hand side of the binary expression, which can only be a field name.

rhs: Right hand side of the binary expression, which can only be a primitive type or a Query.Literal.


Query query =
    new Query('Account').
    addConditionEq('Id', '001O000000qkv3KIAQ');

public Query addConditionNotEq(String lhs, Object rhs)

Add a condition lhs != rhs.

The result is true if and only if the left hand side value does not equal to the right hand side value.

lhs: Left hand side of the binary expression, which can only be a field name.

rhs: Right hand side of the binary expression, which can only be a primitive type or a Query.Literal.


Query query =
    new Query('Account').
    addConditionNotEq('Id', '001O000000qkv3KIAQ');

public Query addConditionIn(String lhs, Object rhs)

Add a condition lhs IN rhs.

The result is true if and only if the left hand side value equals to any value in the right hand side collection.

lhs: Left hand side of the binary expression, which can only be a field name.

rhs: Right hand side of the binary expression, which can only be a List or a Set.


Query query =
    new Query('Account').
    addConditionIn('Id', new Set{'001O000000qkv3KIAQ'});

public Query addConditionNotIn(String lhs, Object rhs)

Add a condition lhs NOT IN rhs.

The result is true if and only if the left hand side value does not equal to any value in the right hand side collection.

lhs: Left hand side of the binary expression, which can only be a field name.

rhs: Right hand side of the binary expression, which can only be a List or a Set.


Query query =
    new Query('Account').
    addConditionNotIn('Id', new Set{'001O000000qkv3KIAQ'});

public Query addConditionLt(String lhs, Object rhs)

Add a condition lhs < rhs.

The result is true if and only if the left hand side value is less than the right hand side value.

lhs: Left hand side of the binary expression, which can only be a field name.

rhs: Right hand side of the binary expression, which can only be a primitive type or a Query.Literal.


Query query =
    new Query('Account').
    addConditionLt('NumberOfEmployees', 10);

public Query addConditionLe(String lhs, Object rhs)

Add a condition lhs <= rhs.

The result is true if and only if the left hand side value is less than, or equals the right hand side value.

lhs: Left hand side of the binary expression, which can only be a field name.

rhs: Right hand side of the binary expression, which can only be a primitive type or a Query.Literal.


Query query =
    new Query('Account').
    addConditionLe('NumberOfEmployees', 10);

public Query addConditionGt(String lhs, Object rhs)

Add a condition lhs > rhs.

The result is true if and only if the left hand side value is greater than the right hand side value.

lhs: Left hand side of the binary expression, which can only be a field name.

rhs: Right hand side of the binary expression, which can only be a primitive type or a Query.Literal.


Query query =
    new Query('Account').
    addConditionGt('NumberOfEmployees', 10);

public Query addConditionGe(String lhs, Object rhs)

Add a condition lhs >= rhs.

The result is true if and only if the left hand side value is greater than, or equals the right hand side value.

lhs: Left hand side of the binary expression, which can only be a field name.

rhs: Right hand side of the binary expression, which can only be a primitive type or a Query.Literal.


Query query =
    new Query('Account').
    addConditionGe('NumberOfEmployees', 10);

public Query addConditionLike(String lhs, Object rhs)

Add a condition lhs LIKE rhs.

The result is true if and only if the left hand side value matches the right hand side value, in a way similar to the LIKE operator in SQL.

lhs: Left hand side of the binary expression, which can only be a field name.

rhs: Right hand side of the binary expression, which can only be a String.


Query query =
    new Query('Account').
    addConditionLike('Name', '%Sam%');

addCondition

public Query addCondition(Condition condition)

Add a condition expression, which is a Query.Condition type instance.

condition: A Query.Condition type, which can be constructed by a staticconditionXX method.


Query q =
    new Query('Account').
    addCondition(Query.conditionEq('Name', 'Sam'));

conditionXX

Creates a Query.Condition instance. 'XX' can be one of these comparison operators: Eq, NotEq, In, Lt, Le, Gt, Ge, Like.

public static Condition conditionEq(String lhs, Object rhs)

Creates a condition lhs = rhs.

The result is true if and only if the left hand side value equals to the right hand side value.

lhs: Left hand side of the binary expression, which can only be a field name.

rhs: Right hand side of the binary expression, which can only be a primitive type or a Query.Literal.


Query.Condition condition =
    Query.conditionEq('Name', 'Sam');

Query q =
    new Query('Account').
    addCondition(condition);

public static Condition conditionNotEq(String lhs, Object rhs)

Creates a condition lhs != rhs.

The result is true if and only if the left hand side value does not equal to the right hand side value.

lhs: Left hand side of the binary expression, which can only be a field name.

rhs: Right hand side of the binary expression, which can only be a primitive type or a Query.Literal.


Query.Condition condition =
    Query.conditionNotEq('Name', 'Sam');

Query q =
    new Query('Account').
    addCondition(condition);

public static Condition conditionIn(String lhs, Object rhs)

Creates a condition lhs IN rhs.

The result is true if and only if the left hand side value equals to any value in the right hand side collection.

lhs: Left hand side of the binary expression, which can only be a field name.

rhs: Right hand side of the binary expression, which can only be a List or a Set.


Query.Condition condition =
    Query.conditionIn('Name', new Set{'Sam'});

Query q =
    new Query('Account').
    addCondition(condition);

public static Condition conditionNotIn(String lhs, Object rhs)

Creates a condition lhs NOT IN rhs.

The result is true if and only if the left hand side value does not equal to any value in the right hand side collection.

lhs: Left hand side of the binary expression, which can only be a field name.

rhs: Right hand side of the binary expression, which can only be a List or a Set.


Query.Condition condition =
    Query.conditionNotIn('Name', new Set{'Sam'});

Query q =
    new Query('Account').
    addCondition(condition);

public static Condition conditionLt(String lhs, Object rhs)

Creates a condition lhs < rhs.

The result is true if and only if the left hand side value is less than the right hand side value.

lhs: Left hand side of the binary expression, which can only be a field name.

rhs: Right hand side of the binary expression, which can only be a primitive type or a Query.Literal.


Query.Condition condition =
    Query.conditionLt('NumberOfEmployees', 10);

Query q =
    new Query('Account').
    addCondition(condition);

public static Condition conditionLe(String lhs, Object rhs)

Creates a condition lhs <= rhs.

The result is true if and only if the left hand side value is less than, or equals the right hand side value.

lhs: Left hand side of the binary expression, which can only be a field name.

rhs: Right hand side of the binary expression, which can only be a primitive type or a Query.Literal.


Query.Condition condition =
    Query.conditionLe('NumberOfEmployees', 10);

Query q =
    new Query('Account').
    addCondition(condition);

public static Condition conditionGt(String lhs, Object rhs)

Creates a condition lhs > rhs.

The result is true if and only if the left hand side value is greater than the right hand side value.

lhs: Left hand side of the binary expression, which can only be a field name.

rhs: Right hand side of the binary expression, which can only be a primitive type or a Query.Literal.


Query.Condition condition =
    Query.conditionGt('NumberOfEmployees', 10);

Query q =
    new Query('Account').
    addCondition(condition);

public static Condition conditionGe(String lhs, Object rhs)

Creates a condition lhs >= rhs.

The result is true if and only if the left hand side value is greater than, or equals the right hand side value.

lhs: Left hand side of the binary expression, which can only be a field name.

rhs: Right hand side of the binary expression, which can only be a primitive type or a Query.Literal.


Query.Condition condition =
    Query.conditionGe('NumberOfEmployees', 10);

Query q =
    new Query('Account').
    addCondition(condition);

public static Condition conditionLike(String lhs, Object rhs)

Creates a condition lhs LIKE rhs.

The result is true if and only if the left hand side value matches the right hand side value, in a way similar to the LIKE operator in SQL.

lhs: Left hand side of the binary expression, which can only be a field name.

rhs: Right hand side of the binary expression, which can only be a String.


Query.Condition condition =
    Query.conditionLike('Name', '%Sam%');

Query q =
    new Query('Account').
    addCondition(condition);

doAnd

Join two or more Query.Condition instances with boolean operator 'and'.

The result is true if and only if all the conditions are true.

Because and has been a reserved keyword in Apex, we have to rename the method to doAnd.

public static Condition doAnd(Condition lhs, Condition rhs)

lhs: Left hand side of the boolean expression

rhs: Right hand side of the boolean expression

The result is true if and only if both lhs and rhs are true.


Query.Condition condition =
    Query.doAnd(
        Query.conditionLike('Name', '%Sam%'),
        Query.conditionGe('NumberOfEmployees', 10)
    );

Query q =
    new Query('Account').
    addCondition(condition);

public static Condition doAnd(Condition condition0, Condition condition1, Condition condition2)

condition0: First condition in the boolean expression condition1: Second condition in the boolean expression condition2: Third condition in the boolean expression

The result is true if and only if all three condtions are true.


Query.Condition condition =
    Query.doAnd(
        Query.conditionLike('Name', '%Sam%'),
        Query.conditionEq('Phone', '123 456 789'),
        Query.conditionGe('NumberOfEmployees', 10)
    );

Query q =
    new Query('Account').
    addCondition(condition);

public static Condition doAnd(Condition condition0, Condition condition1, Condition condition2, Condition condition3)

condition0: First condition in the boolean expression condition1: Second condition in the boolean expression condition2: Third condition in the boolean expression condition3: Fourth condition in the boolean expression

The result is true if and only if all four condtions are true.


Query.Condition condition =
    Query.doAnd(
        Query.conditionLike('Name', '%Sam%'),
        Query.conditionEq('Phone', '123 456 789'),
        Query.conditionIn('Id', new Set{'00190000012KXI2AAO'}),
        Query.conditionGe('NumberOfEmployees', 10)
    );

Query q =
    new Query('Account').
    addCondition(condition);

public static Condition doAnd(List conditions)

conditions: A list of conditions in the boolean expression

The result is true if and only if all the conditions in the list are true.


List conditions =
    new List{
        Query.conditionLike('Name', '%Sam%'),
        Query.conditionEq('Phone', '123 456 789'),
        Query.conditionIn('Id', new Set{'00190000012KXI2AAO'}),
        Query.conditionGe('NumberOfEmployees', 10)
    };

Query.Condition condition =
    Query.doAnd(conditions);

Query q =
    new Query('Account').
    addCondition(condition);

doOr

Join two or more Query.Condition instances with boolean operator 'or'.

The result is true if and only if at least one of the conditions is true.

Because or has been a reserved keyword in Apex, we have to rename the method to doOr.

public static Condition doOr(Condition lhs, Condition rhs)

lhs: Left hand side of the boolean expression

rhs: Right hand side of the boolean expression

The result is true if and only if either lhs or rhs is true.


Query.Condition condition =
    Query.doOr(
        Query.conditionLike('Name', '%Sam%'),
        Query.conditionGe('NumberOfEmployees', 10)
    );

Query q =
    new Query('Account').
    addCondition(condition);

public static Condition doOr(Condition condition0, Condition condition1, Condition condition2)

condition0: First condition in the boolean expression condition1: Second condition in the boolean expression condition2: Third condition in the boolean expression

The result is true if and only if at least one of the three conditions is true.


Query.Condition condition =
    Query.doOr(
        Query.conditionLike('Name', '%Sam%'),
        Query.conditionEq('Phone', '123 456 789'),
        Query.conditionGe('NumberOfEmployees', 10)
    );

Query q =
    new Query('Account').
    addCondition(condition);

public static Condition doAnd(Condition condition0, Condition condition1, Condition condition2, Condition condition3)

condition0: First condition in the boolean expression condition1: Second condition in the boolean expression condition2: Third condition in the boolean expression condition3: Fourth condition in the boolean expression

The result is true if and only if at least one of the four conditions is true.


Query.Condition condition =
    Query.doOr(
        Query.conditionLike('Name', '%Sam%'),
        Query.conditionEq('Phone', '123 456 789'),
        Query.conditionIn('Id', new Set{'00190000012KXI2AAO'}),
        Query.conditionGe('NumberOfEmployees', 10)
    );

Query q =
    new Query('Account').
    addCondition(condition);

public static Condition doAnd(List conditions)

conditions: A list of conditions in the boolean expression

The result is true if and only if at least one condition in the list is true.


List conditions =
    new List{
        Query.conditionLike('Name', '%Sam%'),
        Query.conditionEq('Phone', '123 456 789'),
        Query.conditionIn('Id', new Set{'00190000012KXI2AAO'}),
        Query.conditionGe('NumberOfEmployees', 10)
    };

Query.Condition condition =
    Query.doOr(conditions);

Query q =
    new Query('Account').
    addCondition(condition);

Misc

public Query switchToDisjunction()

After calling this function, all expression added by addCondition oraddConditionXX will be joined with the boolean or operator, i.e. the query condition is true if and only if at least one of these expression are true.


Query q =
    new Query('Account').
    addCondition(Query.doAnd(
        Query.conditionEq('Name', 'Jack'),
        Query.conditionEq('Phone', '123 456 789')
    )).
    addConditionEq('Name', 'David').
    switchToDisjunction();

The example above is equivalent to this query:


SELECT Id FROM Account WHERE (Name = 'Jack' AND Phone = '123 456 789) OR (Name = 'David')

public Query switchToConjunction()

Reverse the effect of switchToDisjunction.

After calling this function, all expression added by addCondition oraddConditionXX will be joined with the boolean and operator, i.e. the query condition is true if and only if all these expression are true.


Query q =
    new Query('Account').
    addCondition(Query.doOr(
        Query.conditionEq('Name', 'Jack'),
        Query.conditionEq('Phone', '123 456 789')
    )).
    addConditionEq('Name', 'David').
    switchToDisjunction().
    switchToConjunction();

The example above is equivalent to this query:


SELECT Id FROM Account WHERE (Name = 'Jack' OR Phone = '123 456 789) AND (Name = 'David')

public Query resetCondition()

Clear all conditions.


Query q =
    new Query('Account').
    addConditionEq('Name', 'David').
    resetCondition();

Date Literals

The second parameter of the addConditionXX and conditionXX methods is usually a binding variable, but in some cases, we can use date literals to build date or date time related conditions more easily.

Query q =
    new Query('Account').
    addConditionLe('LastModifiedDate', Query.TODAY).
    addConditionEq('CreatedDate', Query.LAST_N_WEEKS(3));

The predefined Date Literals includes ones with no parameters, and some with an Integer as a parameter.

public static final DateLiteral YESTERDAY
public static final DateLiteral TODAY
public static final DateLiteral TOMORROW
public static final DateLiteral LAST_WEEK
public static final DateLiteral THIS_WEEK
public static final DateLiteral NEXT_WEEK
public static final DateLiteral LAST_MONTH
public static final DateLiteral THIS_MONTH
public static final DateLiteral NEXT_MONTH
public static final DateLiteral LAST_90_DAYS
public static final DateLiteral NEXT_90_DAYS
public static final DateLiteral THIS_QUARTER
public static final DateLiteral LAST_QUARTER
public static final DateLiteral NEXT_QUARTER
public static final DateLiteral THIS_YEAR
public static final DateLiteral LAST_YEAR
public static final DateLiteral NEXT_YEAR
public static final DateLiteral THIS_FISCAL_QUARTER
public static final DateLiteral LAST_FISCAL_QUARTER
public static final DateLiteral NEXT_FISCAL_QUARTER
public static final DateLiteral THIS_FISCAL_YEAR
public static final DateLiteral LAST_FISCAL_YEAR
public static final DateLiteral NEXT_FISCAL_YEAR
public static DateLiteral LAST_N_DAYS(Integer n)
public static DateLiteral NEXT_N_DAYS(Integer n)
public static DateLiteral LAST_N_WEEKS(Integer n)
public static DateLiteral NEXT_N_WEEKS(Integer n)
public static DateLiteral LAST_N_MONTHS(Integer n)
public static DateLiteral NEXT_N_MONTHS(Integer n)
public static DateLiteral LAST_N_QUARTERS(Integer n)
public static DateLiteral NEXT_N_QUARTERS(Integer n)
public static DateLiteral LAST_N_YEARS(Integer n)
public static DateLiteral NEXT_N_YEARS(Integer n)
public static DateLiteral LAST_N_FISCAL_QUARTERS(Integer n)
public static DateLiteral NEXT_N_FISCAL_QUARTERS(Integer n)
public static DateLiteral LAST_N_FISCAL_YEARS(Integer n)
public static DateLiteral NEXT_N_FISCAL_YEARS(Integer n)

Contribute on Github! Edit this section.