I am trying to set a Case.RecordType and cannot figure out how.

I tried:

caseToAdd.RecordType = '012600000005DYN';

and for the heck of it I tried the Label itself which of course failed. With the ID, I'm getting:

Illegal assignment from String to SOBJECT:RecordType at line 17 column 16

Help? Thanks. (this is during a Class which CREATES the trigger initially)


You'll want to set the RecordTypeId field, as opposed to the RecordType.

caseToAdd.RecordTypeId = '012600000005DYN';

Always avoid using hardcoded ids into code as Ids can be different in different orgs (except Production and Full Copy Sandbox)

Map<String, Schema.RecordTypeInfo> caseRecordTypeInfo =
String caseRecordTypeId = caseRecordTypeInfo.get('RecordTypeName').getRecordTypeId();

Where RecordTypeName is the name of the record type.

  • 1
    That's not exactly true ;) Once rec. type is deployed / created in production all sandboxes refreshed afterwards will have same id so eventually it'll be "safe" to hardcode Ids as they'll be all in sync. But yeah, it's still a bad technique. Describe call (like you did), 1 query to RecordType table etc are the proper way. – eyescream Mar 20 '13 at 7:25
  • Probably still good to avoid hard coding ids since they'll change if your org shifts pods (happening with more and more frequency these days, particular for na1 and na3 customers). Also record type name is a terrible substitute, IMHO using a record type name (as opposed to it's api name) is more likely to break than the ID since admins will ALWAYS change labels, but fortunately can be trained to NEVER edit api names. No idea why the schema methods used record type name, it's a bit error on SFDC's part (IMHO) – Ralph Callaway Mar 20 '13 at 16:02
  • @eyescream in case you're writing a managed package for app exchange, hardcoding ids will just never work. and this is the way you should always code imo, because then even internal code can be ported to a new org ez-pz – zaitsman May 21 '19 at 0:41
  • @zaitsman you do realise it's a 6 years old question? Back then salesforce.stackexchange.com/a/12179/799 was the most awesome approach to recordtypes. Today I'd do it differently, with getrecordtypeinfosbydevelopername, I wouldn't waste a query for it... – eyescream May 21 '19 at 5:53

Devendra, I received this error when trying to use your code for Account just FYI.

Map<String, Schema.RecordTypeInfo> accRecordTypeInfo = Schema.SObjectType.Account.getDescribe().getRecordTypeInfosByName();
String accRecordTypeId = accRecordTypeInfo.get('Partner').getRecordTypeId();

Method does not exist or incorrect signature: [Schema.DescribeSObjectResult].getDescribe()

Instead just using this worked for me.

Id accRecordTypePartner = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Partner').getRecordTypeId();

Edit: ended up using this line below and had to make sure to use the =: operator.

RecordType accRecordTypePartner = [Select ID,Name,DeveloperName from RecordType Where SObjectType =: 'Account' and DeveloperName = 'Partner' LIMIT 1];
acc[0].RecordTypeId = accRecordTypePartner.Id;

Thought I'd share as I'm just getting started. Thanks Doug

  • Looks like a rev by someone else is responsible for that error. Good catch though. Eventually, you'll be able to just fix the wiki yourself, but I have gone ahead and done so in this case. Thanks for your contribution! – Adrian Larson Jun 16 '16 at 17:40
  • Thanks Adrian. Ended up using this and found the =: was what was hanging me up. Updated my reply. RecordType accRecordTypePartner = [Select ID,Name,DeveloperName from RecordType Where SObjectType =: 'Account' and DeveloperName = 'Partner' LIMIT 1]; – Doug D Jun 20 '16 at 20:48

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.