1
     Id recordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByDeveloperName().get('ZS_Lead').getRecordTypeId();
     
     List<Lead> leads  = [Select Id, Name, LeadSource, OwnerId 
                          From Lead
                          Where RecordTypeId =: recordTypeId AND CreatedDate = Today]; 

VS

        List<Lead> leads  = [Select Id, Name, LeadSource, OwnerId 
                 From Lead
                 Where RecordType.Name = 'ZS_Lead' AND CreatedDate = Today];  

Both the SOQL statements above do the same thing - Get a list of Lead records of a particular record type.

I am wondering if it is a personal choice or best practice to get a list of Leads based on RecordTypeName vs RecordTypeId.

2
  • 1
    I suggest to run the query analyser on both. The one that uses RecordTypeId will almost certainly be more efficient than the one using the RecordType.Name. Why should you care? Well, every request has limits against it, so an inefficient query may reduce the through-put you can get or cause your logic to be aborted if you actually hit limits. See this blog post for some more detail.
    – Phil W
    Mar 9 at 22:52
  • While your question is about performance, there are other things to consider. One advantage of using Name or DeveloperName is for deployments. If you are deploying a record type at the same time as your code, you won't know the ID of the new record type. This is especially important for unlocked and managed packages. Also, Ids make the code harder to understand. Mar 10 at 1:31
2

The Name for RecordType is a label and it can be easily changed by admin and your app can fail if this is done.

Also note developer name is unique for a record type record so it is better than Name as that is not unique!

Hence it is better to use RecordType.DeveloperName in your query or the first approach you have as shown below

Id recordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByDeveloperName().get('ZS_Lead')?.getRecordTypeId();

The advantage of the above code is you can use this recordTypeId variable for other queries or other logic in your transactions if needed.

While if you use RecordType.DeveloperName = 'ZS_Lead' in your SOQL you will have to run the above code again!

5
  • Yes, but the developer name can also be easily changed by an admin. This is why admins shouldn't be allowed to do that, lol.
    – sfdcfox
    Mar 10 at 3:59
  • I think developer name is fixed once you create it! I will recheck on that! Mar 10 at 4:38
  • Developer name is not easy to mess with and are most importantly unique! Same is not true with Name! Mar 10 at 4:41
  • DeveloperName is editable unless in a package where it gets locked. Still, DeveloperName is preferable.
    – sfdcfox
    Mar 10 at 4:44
  • Ah ya in package context! I will still prefer the uniqueness capability over using the label! Label also can have spaces which sometimes if double spaced may cause issues! Mar 10 at 4:46
3

Using getRecordTypeInfosBy<something>() means you'll get a runtime error (a Null Pointer Exception) if the recordtype you're trying to use isn't present in the current org.

Using RecordType.Name = <some rec type> means your query would return 0 records if the recordtype isn't present. One thing to note is that RecordType.Name is the label of the record type. While you can use underscores in the label, that's not typical. Be sure you're using the appropriate field in your filter (Name or DeveloperName).

In both situations, if your record type name changes, you need to update code.

While there may be a difference in performance between using RecordTypeId = <id> vs RecordType.Name = <name> in the WHERE clause, I'd imagine such a difference is negligible. Don't chase micro-optimizations.

Less typing = less chance to screw something up, so I'd prefer WHERE RecordType.Name = 'My Rectype'

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.