function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
chandrashekar Jangitichandrashekar Jangiti 

Hi Folks..I need here one solution for below code for best practices we need to add constant variable for Recordtype names how we can add?

code is:
List<Id> rectypeId=new List<Id>();
            for(RecordType r:[Select Id,Name from RecordType where sObjectType ='Contact' AND DeveloperName IN('Contact')]){
              rectypeId.add(r.Id);
              }
Here how we can add constant variable for "DeveloperName IN('Contact')".

thanks,
Chandu
Ashish Singh SFDCAshish Singh SFDC
Hello Chandu,

Incase you want to add filter in the query and add a variable, then you can write your query in the form of String and then concatenate.

Sample Code:
String query = 'Select Id,Name from RecordType where sObjectType =\'Contact\' ';
String var = ' AND DeveloperName IN (\'Contact\')';
query =  query + var;   
List<RecordType> rt = Database.query(query);
System.debug('RT Size =>'+rt.size());
for(RecordType r:rt){
    System.debug('RT =>'+r);
}

Thanks,
Ashish Singh.
chandrashekar Jangitichandrashekar Jangiti
thanks for quick reply Ashish...And here one more thing if i have two recordtype needs to be filter then that case how we can add

String query = 'Select Id,Name from RecordType where sObjectType =\'Contact\' ';
String var = ' AND DeveloperName IN (\'Contact\')';
String var1 = ' AND DeveloperName IN (\'test1\')';
query =  query + var+var1;  --------------------------------------------------------------------> like this or how it is?
List<RecordType> rt = Database.query(query);
System.debug('RT Size =>'+rt.size());
for(RecordType r:rt){
    System.debug('RT =>'+r);
}

thanks,
Chandu
Ashish Singh SFDCAshish Singh SFDC
Hi Chandu,

Why don't you just modify your var. Just add all in condition in it.
 
String query = 'Select Id,Name from RecordType where sObjectType =\'Contact\' ';
String var = ' AND DeveloperName IN (\'Contact\',\'Contact_Record_Type_2\')';
query =  query + var;   
List<RecordType> rt = Database.query(query);
System.debug('RT Size =>'+rt.size());
for(RecordType r:rt){
    System.debug('RT =>'+r);
}


You can refer to the Dynamic SOQL (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_soql.htm) for more information.

Thanks,
Ashish Singh