Here is the code I am trying to convert to dynamic query-

List<RecordType> jobRecordTypes  = [SELECT Id, DeveloperName 
                                    FROM   RecordType 
                                    WHERE (DeveloperName=:FieldPro_Library.WORK_ORDER_RECORD_TYPE 
                                        OR DeveloperName=:FieldPro_Library.MAKE_READY_RECORD_TYPE) 
                                        AND SObjectType=:FieldPro_Library.RESOURCE_REQUEST_API_NAME];

I am confused how to implement the OR AND logic into single dynamic query

Example of dynamic query I want-

List<RecordType> jobRecordTypes = Schema.SObjectType.RecordType.getRecordTypeInfosByName().get(FieldPro_Library.WORK_ORDER_RECORD_TYPE).getRecordTypeId();

1 Answer 1


Below is an example of how you can make use of Database.query(...) with a prebuilt SOQL extended by an input parameter representing the WHERE clause.

String whereClause = '(DeveloperName=:FieldPro_Library.WORK_ORDER_RECORD_TYPE' +
        ' OR DeveloperName=:FieldPro_Library.MAKE_READY_RECORD_TYPE)' +
        ' AND SObjectType=:FieldPro_Library.RESOURCE_REQUEST_API_NAME';

List<RecordType> recordTypes = getRecordTypes(whereClause);

private static List<RecordType> getRecordTypes(String whereClause) {
    String query = 'SELECT Id, DeveloperName' +
            ' FROM RecordType';

    if (whereClause != null)  query = query + ' WHERE ' + whereClause;
    return Database.query(query);

This allows you to call the private method with many filter variations on the shared RecordType object. You can also extend the method to receive a LIMIT variable if needed and then apply such value to the SOQL.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .