+ Start a Discussion

build dynamic soql condition based on Datatypes

I need to build a soql condtion dynamically, 
E.g. if datatype is EMAIL then all same datatypes should be in OR condtion and other datatypes in AND condtion
 WHERE (email='test@gmail.com' OR email='test_1@gmail.com') AND (name='test' OR name='test_1')
and return it to a string 
Best Answer chosen by NewBie09
Asif Ali MAsif Ali M
Try the below code to capture the Object fields by Type and use the Map(fieldsByType) to build SOQL dynamically.  Curently this code captures the Account fields. You can change the code as per your need.  I Hope you can now build the SOQL dynamically.
Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get('Account').getDescribe().fields.getMap();
Map<String, List<String>> fieldsByType =  new Map<String, List<String>>();

for (String key: objectFields.keySet()) {
    Schema.SObjectField field = objectFields.get(key);
    String type = String.valueOf(field.getDescribe().getType());
    if(!fieldsByType.containsKey(type)) {
        fieldsByType.put(type, new List<String>());

// Get List of fields
String[] strings = fieldsByType.get('STRING');
String[] booleans = fieldsByType.get('BOOLEAN');