Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hello and thank you for taking the time to read my question. I have a simple call to the recordType object that will have to results. The reason I have two results is because I need to verify of the opportunity has a certain type of record id and if so then I will create a serviceContract with a certain recordtypeID. Simple enough, however governor limits scare me right now. Here is the code below.

RecordType[] RecHyperStream = [SELECT 
                                    name, Description, id, SobjectType, BusinessProcessId          
                                   (SobjectType='Opportunity' AND Description='HyperStream Services') 
                                   (SobjectType='ServiceContract' AND Description='This is the record type assigned to HyperStream Agreement')

In order to differentiate the two do I need to do a for Loop?

for(integer i =0; i < RecHyperStream.size(); i++)
  if(RecHyperStream[i].SobjectType == 'Opportunity')
     //put in a variable for opp
     //put in a variable for serviceContract

Or is there a cleaner and simpler way that does not require a for-loop?

THanks again for your time.

share|improve this question
up vote 3 down vote accepted

First of all I would not recommend using the recordtype description to filter since that could easily be changed. You can use the developername instead. There's really no cleaner way to distinguish than going through a loop.

You should not have any issues with governor limits because you are getting the recordtypes in one query.

list<RecordType> RecHyperStream = [SELECT name, Description, id, SobjectType, BusinessProcessId FROM RecordType WHERE (SobjectType='Opportunity' AND DeveloperName='HyperStream') OR (SobjectType='ServiceContract' AND DeveloperName='HyperStream')];

//putting it in a map is so that if you have more 'HyperStream' recordtypes you can handle them.
map<String,RecordType> recordTypeByObject = new map<String,RecordType>();
for(RecordType r : RecHyperStream ) recordTypeByObject.put(r.sobjectType,r);
share|improve this answer
Good idea grigri, I will use the map instead and make a for loop with two if statements, this way i can just manage the serviceContract and the opportunity – jnoel10 Feb 18 '13 at 19:06

You can use describe call here instead of query and this will reduce your query.

Always for record types prefer record type infos

Schema.DescribeSObjectResult R = Account.SObjectType.getDescribe();
List<Schema.RecordTypeInfo> RT = R.getRecordTypeInfos();

share|improve this answer
Thanks for the reply Mohith, I am not familiar enough with this method and I will definitely investigate this further. – jnoel10 Feb 18 '13 at 19:13
Not sure I'd agree with using record type infos preferentially, primarily due to the fact they're grouped by record type label not developer which means things always break when people update the record type label. – Ralph Callaway Feb 19 '13 at 16:36
@Ralph There are disadvantages with not using it as it consumes queries .Certainly it will depend on situation and may be we can make admin aware of consequences of not changing labels . – Mohith Shrivastava Feb 19 '13 at 19:01
Agreed, but I'd counter that the governor limits for describes is smaller than the query limits. – Ralph Callaway Feb 19 '13 at 20:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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