Creating a new record with giving record type

I want to create a new record on click of button by giving its record type.

please find the code below

public pageReference Test()
       RecordType rt =[SELECT ID, name FROM RecordType WHERE SObjectType = 'App__c' and name='Test_Contact'];

      this.ap.Client__c = account.Id;
      this.ap.RecordTypeId =rt.Id;
      insert (this.ap);

        pageReference pageRef = new pageReference('/oaoa/TestForm');

       return pageRef;


I am getting error as List has no rows for assignment to SObject
Rt is not returning any value


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Well, maybe the query really doesn’t return anything?

What does SELECT ID, name FROM RecordType WHERE SObjectType = 'App__c' return?

Is the name really ‘Test_Contact’? Look at your config and try with ‘Test Contact’. Even better – query by API name of the rec. type (less likely to be translated to French for example ;)):

WHERE SObjectType = 'App__c' and DeveloperName = 'Test_Contact'

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x