3

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)

3 Answers 3

7

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

caseToAdd.RecordTypeId = '012600000005DYN';
0
5

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 =
                Schema.SObjectType.Case.getRecordTypeInfosByName();
String caseRecordTypeId = caseRecordTypeInfo.get('RecordTypeName').getRecordTypeId();

Where RecordTypeName is the name of the record type.

4
  • 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, 2013 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) Mar 20, 2013 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, 2019 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, 2019 at 5:53
1

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

2
  • 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, 2016 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, 2016 at 20:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .