You need to sign in to do that
Don't have an account?
steve456
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
try this
Schema.SObjectType.sObjectApiName.getRecordTypeInfosByName().get('Record Type Name').getRecordTypeId();
example
system.debug(Schema.SObjectType.account.getRecordTypeInfosByName().get('person').getRecordTypeId());
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.
Please refer http://wiki.developerforce.com/page/Apex_Code_Best_Practices for more information.
Thanks,
Sandeep
Hi Kiran,
This is not working..
I guess Kiran just missed getDescribe there.No worries Hope this helps.
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)
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