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

  • 1
    It seems query made on RecordType is returning null, Did you tried running same query from Console?
    – Prasanna
    Dec 9, 2013 at 11:49
  • Double check your name. Is it 'Test Contact' or 'Test_Contact'? Also, if you simply creating a button that auto-selects the RecordType, it is easier to create a custom URL-button than create a VF page (no test code to write/maintain). Google "Salesforce URL hacking" for more info, and let me know if you like an example Dec 9, 2013 at 21:45

1 Answer 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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.