This is just a quick query that grabs the record type id and assigns it to a string. I am always needing this so decided to put it up on the blog for reference. You need something like this when specifying a record type because if you would hard code the id into the trigger/class in production it would fail because everything has a different id from Sandbox to Production.
RecordType r = [select id from RecordType where name = ‘Account Recortype’ AND SobjectType = 'Account'];
String AcctRecordtypeID= r.id;
So the string AcctRecordtypeID will now have the record id of the record type pacesetter.
If there are multiple record types out there with the same name associated to different object, you can just adjust the where statement to be more specific about where to pull the RecordType from.
Alternatively the same query could be used with a list. This will prevent an error in the event that there is no recordtype with the name specified.
List<RecordType> recType = [select id from RecordType where name = 'Pharma' AND sobjecttype = 'Account' AND IsActive = TRUE limit 1];
if(!recType.isempty()){
String PharmaRecordTypeID = rectype[0].id;
}
Alternatively the same query could be used with a list. This will prevent an error in the event that there is no recordtype with the name specified.
List<RecordType> recType = [select id from RecordType where name = 'Pharma' AND sobjecttype = 'Account' AND IsActive = TRUE limit 1];
if(!recType.isempty()){
String PharmaRecordTypeID = rectype[0].id;
}
No comments:
Post a Comment