Id recordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByDeveloperName().get('ZS_Lead').getRecordTypeId();
System.debug('The FS Lead record Type Id is ' +recordTypeId );  // 011kjhjkhjkhkh

List<Lead> leads  = [Select Id, Name, LeadSource, OwnerId 
                 From Lead
                 Where RecordTypeId = recordTypeId AND CreatedDate = Today];   // error
                //Where RecordTypeId = '011kjhjkhjkhkh' AND CreatedDate = Today --- works

enter image description here

  • P.S. RecordType.Name is indexed, so you technically can just write WHERE RecordType.Name = 'ZS_Lead' and skip the describe call. It's worth knowing when you need to describe and when you don't (and here, you don't).
    – sfdcfox
    Mar 9, 2021 at 22:46

1 Answer 1


Your missing the colon : when referencing the variable in your query.

That line should read Where RecordTypeId = :recordTypeId AND CreatedDate = Today];

See https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_variables.htm

  • Thank you for pointing that out!. Mar 9, 2021 at 22:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .