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
pradyprady 

How to create a recordtype and assign it to custom object in a test class

Hi,

 

I am trying to insert a record in a custom object which has a recordtype in my test class.

I am not sure on how to create a record type and assign it to the object.

 

Any ideas on how to go about doing it ?

 

Thanks

Prady

rohitsfdcrohitsfdc

You cannot create recordtype in a class, bcos this object doesnt support dml operations. here is the list of objects that doesnt support dml operations

sObjects That Do Not Support DML Operations in Apex

User**

SelfServiceUser

Profile

Territory**

RecordType

Translation

WebLink

BusinessHours

BusinessProcess

CategoryNode

Process*



mngmng

Just wanted to say that as far as I know Users and Territories DO support Dml in tests, which is what I assume those asterisks are for. Want to prevent the spread of misinformation from blatant copypasta

pradyprady

Ok, that would mean recordtypes cant be created in a test class, correct?

Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi..

 

We can not create a record type in Apex class or in Apex trigger also. So, when you need to use recordtype in test class then you can access it using SOQL query like following:

 

RecordType rt =[select id,Name from RecordType where sObjectType='ObjectName' AND Name='RecordTypeName' limit 1]

 

But this is work fine when you use IsTest(seeallData =true) in test class. This is only avail for Apex 23.0 or later.

 

 

If this is a solution for your question then mark it as a Solution.

 

 

 

 

mngmng

B K SINGH wrote:

Hi..

 

We can not create a record type in Apex class or in Apex trigger also. So, when you need to use recordtype in test class then you can access it using SOQL query like following:

 

RecordType rt =[select id,Name from RecordType where sObjectType='ObjectName' AND Name='RecordTypeName' limit 1]

 

But this is work fine when you use IsTest(seeallData =true) in test class. This is only avail for Apex 23.0 or later.

 

 

If this is a solution for your question then mark it as a Solution.

 

 

 

 


You do NOT need SeeAllData to query record types.

 

SeeAllData is only applicable for API v24 and after.

 

When querying for record types, it is best practice to use DeveloperName instead of the Label Name. You may also want to consider the IsActive flag as true to ensure you are not querying for deactivated Record Types.

 

Alternatively, Record Type Infos are now available through Schema.SObjectType, eschewing your need for even a query

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject_describe.htm#apex_recordTypeInfo_methods

Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi Mng,

 

Thanks for correction. seeAllData=true is available only in API V24.0 or later....