+ Start a Discussion
Richa Upadhyay 29Richa Upadhyay 29 

Adding Record Type in Test Class Using developerName

Hi Folks,

Foe the testMethod: I need to use and access contact data based on the RecordType. I need to acess it Dynamically, i.e., using developerName. Please help if I need to make any chages.With current code I am getting Exception: System.ListException: List index out of bounds. Please help me fixing this. How exactly I should create:  recordtypeid=rlist[3].id

Thanks in advance

list<RecordType> rList = [Select id 
                                            From RecordType 
                                            Where sObjectType = 'Contact'and RecordType.Name = 'developerName'];
                                         
      
            Contact c1, c2, c3, c4, c5;
            c1 = TestUtils.createContact(new Contact(title = 'CEO', Salutation = 'Mr.', Lastname='Contact 1', AccountId=ac1.id, Email='test1@test.com', recordtypeid=rlist[3].id));
            c2 = TestUtils.createContact(new Contact(title = 'CEO', Salutation = 'Mr.', Lastname='Contact 1', AccountId=ac1.id, Email='test2@test.com', recordtypeid=rlist[3].id));
Sure@DreamSure@Dream
hi Richa,

Change your code to:
list<RecordType> rList = [Select id 
                                            From RecordType 
                                            Where sObjectType = 'Contact' and RecordType.DeveloperName= 'developerName'];
                                         
      
            Contact c1, c2, c3, c4, c5;
            c1 = TestUtils.createContact(new Contact(title = 'CEO', Salutation = 'Mr.', Lastname='Contact 1', AccountId=ac1.id, Email='test1@test.com', recordtypeid=rlist[0].id));
            c2 = TestUtils.createContact(new Contact(title = 'CEO', Salutation = 'Mr.', Lastname='Contact 1', AccountId=ac1.id, Email='test2@test.com', recordtypeid=rlist[0].id));

Since the rList has only one item, you can access only till 0th index. 

Mark this as the solution, if it helped solve your problem.