I'm wondering what is the best practice in a situation where I have a conditional query - do I use an if/else to create my query string and use dynamic query in one place, or use static query and write the query out 2x. What is the ideal dev practice here? I always like to see static queries, but I hear that this really is the use-case for dynamic SOQL.
Lead ref;
if(checkWithSSN){
ref = [SELECT Id FROM Lead WHERE Patient_SSN__C = :ssn ORDER BY CreatedDate DESC Limit 1];
}else{
ref = [SELECT Id FROM Lead WHERE FirstName =:firstName AND LastName = :lastName AND Patient_DOB__c = :dob ORDER BY CreatedDate DESC Limit 1];
}
Or this
if(checkWithSSN){
queryStr += 'Patient_SSN__C = :ssn';
}else{
queryStr += 'FirstName = :firstName AND LastName = :lastName AND Patient_DOB__c = :dob'
}
queryStr += 'ORDER BY CreatedDate DESC Limit 1';
List<Lead> refList = Database.query(queryStr);