+ Start a Discussion
LearnerrrLearnerrr 

Any Other way to refer record type except record type ID because sand box will not refer the same id to select record type

else if(ApexPages.currentPage().getParameters().get('RecordType') =='0127F000000erM4'){
             PageReference pageRef1 = new PageReference('/apex/Case_CustomEditPage');
             pageRef1.setRedirect(true);
             return pageRef1;
             }

 
Best Answer chosen by Learnerrr
Newbie__SalesforceNewbie__Salesforce
Hey Sweta G,

Never use hardcoded Ids 
you can use following for ur requirement - 
Id recordTypeId = 
            Schema.SObjectType.Case.getRecordTypeInfosByName().get('Assignment').getRecordTypeId();

Here Case is the Object name, Assignement is my record type name

All Answers

Newbie__SalesforceNewbie__Salesforce
Hey Sweta G,

Never use hardcoded Ids 
you can use following for ur requirement - 
Id recordTypeId = 
            Schema.SObjectType.Case.getRecordTypeInfosByName().get('Assignment').getRecordTypeId();

Here Case is the Object name, Assignement is my record type name
This was selected as the best answer
salesforcelearner1salesforcelearner1
Hi Sweta G,
you can Query from recordtype object with name to filter the results.
select id,name from recordtype where name='xxxxx'
store it in variable and check it in the if condition.
 
Vikash GoyalVikash Goyal
Yes, You can use following best practices to specify the record id instead of hard coding in code itself :

1. By using custom label :
Just create a custom label with recordtype id value and use same custom label in your apex class 
e.g.  Let say  label created --> ACCOUNT_RECORDTYPE
ApexPages.currentPage().getParameters().get('RecordType') == Label.ACCOUNT_RECORDTYPE

2. Using Schema Class :
E.g. Suppose you want to retrieve Id of a record type of Account object and record type label is 'IT Industry'
Id recTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('IT Industry').getRecordTypeId();
Here to avoid hard-coding you can use record type’s label as custom label, custom settings or custom metadata.

3. As a utility method (Similar to 2nd one) :
You can add a utility method in your class or in common utility class in which you can pass name of sObject and record type label which returns recordtypeid.
public Id getRecordTypeId(String objectName, String recordTypeLabel){
  Schema.SObjectType objType = Schema.getGlobalDescribe().get(objectName);
  Schema.DescribeSobjectResult objTypeDesc = objType.getDescribe();
  map<Id, Schema.RecordTypeInfo> recTypeMap = objTypeDesc.getRecordTypeInfosById();
  Schema.RecordTypeInfo rtByName =  recTypeMap.get(recordTypeLabel);
  return rtByName.getRecordTypeId();
}