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
Drew Salazar 5Drew Salazar 5 

Apex Class Extension error message issue?

I am new to Apex and I am having trouble with changing the record type of a record on a visualforce page.  Here is what is happening.


If I try to use this as an Apex Class,
 
public class LoadRecordType {

    Account a;
    
    public LoadRecordType(ApexPages.StandardController controller) {
        a=(Account)controller.getRecord();
    }
    
    public PageReference setRecordType(){
        a.RecordTypeId=[Select Id,SobjectType,Name From RecordType where Name ='VCHospital' and SobjectType ='Account'  limit 1].id;
        update a;
        return null;
    }
}

And reference this extension on the visualforce page like this.
 
<apex:page standardController="Account" extensions="LoadRecordType" action="{!setRecordType}">
    <apex:detail subject="{!account}"/>
</apex:page>

I get this error.

Visualforce Error
System.QueryException: List has no rows for assignment to SObject
Error is in expression '{!setRecordType}' in component <apex:page> in page accountdetailbutton: Class.LoadRecordType.setRecordType: line 10, column 1
Class.LoadRecordType.setRecordType: line 10, column 1


Im kind of a visualforce Newb.  Can someone help me figure out why this is happening?
Best Answer chosen by Drew Salazar 5
MissedCallMissedCall
a.RecordTypeId=[Select Id From RecordType where DeveloperName ='VCHospital' and SobjectType='Account' limit 1].id;

All Answers

Amritesh SahuAmritesh Sahu
Hi Drew,

Please use following code
public class LoadRecordType {

    Account a;
    
    public LoadRecordType(ApexPages.StandardController controller) {
        a=new Account();
        a=(Account)controller.getRecord();
        setRecordType();
    }
    
    public void setRecordType(){
        a.RecordTypeId=[Select Id From RecordType where Name ='VCHospital' and SobjectType ='Account' limit 1];
        update a;
    }
}
VF:
<apex:page standardController="Account" extensions="LoadRecordType">
    <apex:detail subject="{!account}"/>
</apex:page>

Thanks,
Amritesh
Drew Salazar 5Drew Salazar 5
Thank you Amritesh.  When I tried using this for the Apex class I got this error.  "Error: Compile Error: Illegal assignment from LIST<RecordType> to Id at line 12 column 9"
public class LoadRecordType {

    Account a;
    
    public LoadRecordType(ApexPages.StandardController controller) {
        a=new Account();
        a=(Account)controller.getRecord();
        setRecordType();
    }
    
    public void setRecordType(){
        a.RecordTypeId=[Select Id From RecordType where Name ='VCHospital' and SobjectType ='Account' limit 1];
        update a;
    }
}


 
Amritesh SahuAmritesh Sahu
Just replace the line with this line:
a.RecordTypeId=[Select Id From RecordType where Name ='VCHospital' and SobjectType='Account' limit 1].id;
Drew Salazar 5Drew Salazar 5
Ok I did that and the Apex Class saved fine.  When I attempted to use this Class as an extension with the VF code you gave me, I received this error message.

System.QueryException: List has no rows for assignment to SObject 
Class.LoadRecordType.setRecordType: line 12, column 1
Class.LoadRecordType.<init>: line 8, column 1
 
Ughhh!  

 
Amritesh SahuAmritesh Sahu
The error "List has no rows for assignment to SObject" comes if the soql returns no record.
Please check your query and name of the RecordType "VCHospital" and also check if this recordtype is present in account or not.

Thanks!
MissedCallMissedCall
+Amritesh, Name on record type object refer to record type label and DeveloperName refers to record type name.
LeadforceLeadforce
This is interesting because I am using the correct record type name.

User-added image

I'm not sure why it is not finding it.
MissedCallMissedCall
a.RecordTypeId=[Select Id From RecordType where DeveloperName ='VCHospital' and SobjectType='Account' limit 1].id;
This was selected as the best answer
LeadforceLeadforce
And the Account I am testing on has VCHospital as record type name.
Drew Salazar 5Drew Salazar 5
Yes!!!!! You are a God.!!!!
Drew Salazar 5Drew Salazar 5
Thank You!!!!