+ Start a Discussion

SOQL Filter by RecordType name example

If you don't want to hard code your recordtype ids in a SOQL query, here is an example on how to query using the record type name:


Select id, name,  type, RecordType.Name, AMOUNT, closedate, stagename

FROM Opportunity
WHERE recordtypeid in (Select Id From RecordType where sobjecttype = 'Opportunity' and name in ('Daily Open Quote Record Type', 'Prospect Record Type'))
limit 100

Select id, name, type, RecordType.Name, AMOUNT, closedate, stagename
FROM Opportunity
WHERE RecordType.Name IN ('Daily Open Quote Record Type', 'Prospect Record Type')

This should do the work

By using 'Recordtype.Name' you are joining every record in the Opportunity table with the RecordType table to get the name.  In my example we get better performance using a sub select to lookup RecordTypeid, as Salesforce executes it only once as a separate query and there is no table joining against the Opportunity table. 

Well am not what takes more resource.
or a Join.

Mariappan PerumalMariappan Perumal



Instead of querying and make use of record type 


There is one more smart to fetch the record type id with out  using soql query .


Here is the syntax:




Kindly let me know incase you assistance on this



Mariappan Perumal

David WaughDavid Waugh

I consider getRecordTypeInfosByName() to be completely broken and unusable because it uses the RecordType's LABEL and not it's Developer Name, which would be equivelent to a SObject's field's API name.  I recommend using the extra query ALWAYS to retreive RecordTypeId by Developer_Name and never refer to the label, which is subject to change.  Furthermore, to limit the RecordTypeId queries to 1 per Apex Context, it's best practice to cache the results of a query agains the entire RecordType table, mapped by object and Developer Name, using some Utility Methods class.  This static method would call the query only if it isn't already loaded into a static member of the utlity class, which would mean it was the first time being called within the APEX context.  Further calls to the method would not query, but rather simply return the static member data structure holding the RecordTypeIds appropriatly.  Note:  this wouldn't work as well if you were adding RecordTypes dynamically during run-time, if that is even a use case.
David Roberts 4David Roberts 4
Thanks All,
I needed a variation in my code:
Select Id,name From RecordType where sobjecttype = 'Event' and Name = 'Training'
Marcel BarrazaMarcel Barraza
@David Waugh

I think @Mariappan answer is a good approach to reduce the amount of DML queries if you want to user Developer_Name you can do this instead:


Best Regards
suren A 1suren A 1
Simple Query to fetch record types ...

select name,Id, RecordTypeId, recordtype.name from opportunity WHERE recordtype.name = 'quote'