function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
steve456steve456 

How to give Record Type in test class

I had to write a record type  in a test class.What i did was I hard coded the record type id.Is is this a proper way or is there any alternate way to do it

 

 

Example

 

RecordTypeId='47uyfkfkhs65fufdu'

 

 

Is hard coding a record type a good practice

kiranmutturukiranmutturu

try this 

Schema.SObjectType.sObjectApiName.getRecordTypeInfosByName().get('Record Type Name').getRecordTypeId();

 

example

 

system.debug(Schema.SObjectType.account.getRecordTypeInfosByName().get('person').getRecordTypeId());

Sandeep001Sandeep001

You need to avoid hardcoding the recordtype ids in your apex code. By doing so, if the record IDs change between environments, the logic can dynamically identify the proper data to operate against and not fail.

 

Now, to properly handle the dynamic nature of the record type IDs, the following example queries for the record types in the code, stores the dataset in a map collection for easy retrieval, and ultimately avoids any hardcoding.

//Query for the Account record types
     List<RecordType> rtypes = [Select Name, Id From RecordType 
                  where sObjectType='Account' and isActive=true];
     
     //Create a map between the Record Type Name and Id for easy retrieval
     Map<String,String> accountRecordTypes = new Map<String,String>{};
     for(RecordType rt: rtypes)
        accountRecordTypes.put(rt.Name,rt.Id);
     
      for(Account a: Trigger.new){
     	 
     	  //Use the Map collection to dynamically retrieve the Record Type Id
     	  //Avoid hardcoding Ids in the Apex code
     	  if(a.RecordTypeId==accountRecordTypes.get('Healthcare')){     	  	
     	  	 //do some logic here.....
     	  }else if(a.RecordTypeId==accountRecordTypes.get('High Tech')){
     	  	 //do some logic here for a different record type...
     	  }
     	 
     } 

 Please refer http://wiki.developerforce.com/page/Apex_Code_Best_Practices for more information.

 

Thanks,

Sandeep

priyanka.mv26priyanka.mv26

Hi Kiran,

 

This is not working.. 

Mohith Kumar ShrivastavaMohith Kumar Shrivastava
Map <String,Schema.RecordTypeInfo> recordTypesadjustment = SampleInventoryTransaction__c.sObjectType.getDescribe().getRecordTypeInfosByName();
    Id RecTypeIdinadj = recordTypesadjustment.get(Label.SFA_InboundRecordType).getRecordTypeId();

 I guess Kiran just missed getDescribe there.No worries Hope this helps.

sunny522sunny522
Hi Steve,
    Please Go through an example in the link http://salesforceglobe4u.blogspot.in/2016/06/how-to-get-recordtypeid-in-test-class.html (http://salesforceglobe4u.blogspot.in/2016/06/how-to-get-recordtypeid-in-test-class.html" target="_blank)
Ajay K DubediAjay K Dubedi
Hi steve,
You can't give hardcode id in test class and the following line help to find recordtype Id in test class:
Id RecordTypeIdContact = Schema.SObjectType.Account.getRecordTypeInfosByName().get('RecordTypeContactClientOnly').getRecordTypeId();

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
Varsha Dhage 11Varsha Dhage 11
Thanks Ajay , it helped me. 
pratiksha tamrakarpratiksha tamrakar
Hi kiranmutturu, it helped me.