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

Record type in Test classes

I have two record types name as Doctor and Other.

How to achieve the Record type IDs in test class to achieve step1 controller?

Error:-  System.QueryException: List has more than 1 row for assignment to SObject

Stack trace:- Class.newpatientController.testnewpatientController: line 72, column 1


public class newpatientController {

Patient__c patient;
Contact contact;
Order__C order;

public Contact getContact() {
if(contact == null) contact = new Contact();
return contact;

public Patient__c getpatient() {
if(patient == null) patient = new Patient__c();
return patient;

public Order__C getorder() {
if(order == null) order = new Order__C();
return order;

public PageReference step() {
return Page.recordtype;

public PageReference step1() {
return Page.newdoctor;
return Page.newpatient;

public PageReference step2() {
return Page.newpatient2;

public PageReference step3() {
return Page.newpatient3;

public PageReference save() {

insert patient;

insert contact;

insert order;

PageReference orderPage = new PageReference('/';

return orderPage;
private static testmethod void testnewpatientController(){
newpatientController controller = new newpatientController ();

Patient__C pat = new Patient__C(id='a0a90000000SbUN');
id idRT = [select Id from RecordType where SobjectType = 'Patient__c' ].Id;
pat.RecordTypeId = idRT;
pat.Name = 'a0a90000000SbUN';

Order__C ord = new Order__C(id='a0Z9000000C5bNx');
ord.Patient_Name__c = 'a0a90000000SbUN';
ord.Type_of_Study__c = 'Sleep';
ord.Date_of_Study__c = Date.newInstance(2010,12,31);

Contact cont = controller.getContact();
cont.LastName = 'Testlast';
cont.FirstName = 'name1'; = '5551211234';



Thanks In advance.



Hello Tarun,


First of all, I see there's a Id hard-coded in your code, never do that. You will not have same Id's in all salesforce instances, so in such cases, query the Id from the server, take it in a variable and use it instead of hard-coding it.


This is the code I'm referring to:


public PageReference step1() {


Now, the exception you're getting is because as you've said there are two record-types for object 'Patient'. When you're querying here 'id idRT = [select Id from RecordType where SobjectType = 'Patient__c' ].Id;' , you are simply asking the server to return you all the RecordTypes that Patient object has. So it returns you 2 records. 


Instead, add a filter saying the Name of the RecordType.


For example, if you want to retreive the Doctor Record Type Id, your query should be

id idRT = [select Id from RecordType where SobjectType = 'Patient__c'  WHERE DeveloperName = 'Doctor'].Id;


Hope this helps. Let me know if any questions!