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
swapna muthiyaluswapna muthiyalu 

how to convert the following query into dynamic query?

set<string>mEmpId = new set<string>();
set<string>hEmpId = new set<string>();
if (mEmpId .size() == 0){
list<Contact> mgrHrRecs = [select id, empid__c,AccountId, Type__c from contact where (empid__c IN : hEmpId and AccountId IN :accountids and indexh__c = ‘yes’) ];
} else if(hEmpId.size() == 0){
list<Contact> mgrHrRecs = [select id, empid__c,AccountId, Type__c from contact where (empid__c IN : mEmpId and AccountId IN :accountids and indexh__c = ‘yes’) ];
}else if (mEmpId.size() > 0 && hEmpId.size() > 0)
{list<contact>listcon = [select id, empid__c,AccountId, Type__c from contact where (empid__c IN : mEmpId and AccountId IN :accountids and indexm__c = ‘yes’) or (empid__c IN : hEmpId and AccountId IN :accountids and indexh__c = ‘yes’) ];} ;
Best Answer chosen by swapna muthiyalu
Raj VakatiRaj Vakati
Try some think like below 
 
set<string>mEmpId = new set<string>();
set<string>hEmpId = new set<string>();
String query ='' 

if (mEmpId .size() == 0){
query = 'select id, empid__c,AccountId, Type__c from contact where (empid__c IN : hEmpId and AccountId IN :'+accountids +'and indexh__c = '\yes'\’;
} else if(hEmpId.size() == 0){
query = 
 }else if (mEmpId.size() > 0 && hEmpId.size() > 0)
{ ;
query = 

} ;

Datebase.execute(query);

 

All Answers

Raj VakatiRaj Vakati
Try some think like below 
 
set<string>mEmpId = new set<string>();
set<string>hEmpId = new set<string>();
String query ='' 

if (mEmpId .size() == 0){
query = 'select id, empid__c,AccountId, Type__c from contact where (empid__c IN : hEmpId and AccountId IN :'+accountids +'and indexh__c = '\yes'\’;
} else if(hEmpId.size() == 0){
query = 
 }else if (mEmpId.size() > 0 && hEmpId.size() > 0)
{ ;
query = 

} ;

Datebase.execute(query);

 
This was selected as the best answer
swapna muthiyaluswapna muthiyalu
what is the difference in using the ordinary if loop and this static method?
what will be the impact?
swapna muthiyaluswapna muthiyalu
Thanks Raj