• Drew Salazar 5
  • NEWBIE
  • 20 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 5
    Replies
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?
I want to have a visualforce page open a standard account detail view except write in code that defaults the twistsections as hidden so the sections are collapsed by default.
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?
I want to have a visualforce page open a standard account detail view except write in code that defaults the twistsections as hidden so the sections are collapsed by default.