String queryString =
            'SELECT Id,IsPersonAccount,RecordTypeId FROM Account WHERE LastModifiedDate < ' + 
            Ke.Job_Start_Date_Time__c.formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'') +
                'AND RecordTypeID IN (\'' + PersonAccount + '\', \'' + BusinessAccount + '\')';

I am learning the difference between static and dynamic SOQL and I am interested to change this query from dynamic to static but I see I cannot do that since static SOQL does not provide INCLUDES. Please correct me if I am wrong


Yes, you can. You can continue to use the IN keyword but you need to present it slightly differently with bind variables. In fact, in this case, I actually think it makes life easier for you:

Set<Id> recordTypeIds = new Set<Id>{PersonAccount,BusinessAccount};
List<Account> accounts = [SELECT Id,IsPersonAccount,RecordTypeId FROM Account WHERE LastModifiedDate < :Job_Start_Date_Time__c AND RecordTypeId IN :recordTypesIds];
  • Thank you but I would like to format the field Job_Start_Date_Time__c as I have stated above and when I do that I get the error as 'Invalid bind expression type of String for column of type Datetime' . Can I not format the field ? – user3920709 Jun 8 at 7:58
  • Would like to suggest take out the Datetime assigning it to a variable e.g. Datetime dt = Ke.Job_Start_Date_Time__c.formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\''); and refer it in where. – Pasan Eeriyagama Jun 8 at 8:13
  • Oh now I get the error as 'Illegal assignment from String to Datetime' – user3920709 Jun 8 at 8:24
  • @user3920709 why do you need to format it first? If both Job_Start_Date_Time__c and LastModifiedDate are Datetime then you wouldn't need to do anything. The error you get when you format it is because you've formatted the datetime to a string and compared it to a datetime (LastModifiedDate) – Phil Hawthorn Jun 8 at 8:25
  • You are right, I was mixing things up. Thanks – user3920709 Jun 8 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.