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
DJ 367DJ 367 

System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.Name

Hello All,

I am facing this issue while hitting edit butoon. I am not sure why this error is generating. can someone please look my code and help me to resolve this. Thanks
 
public class AccountSaveCont{

	private ApexPages.StandardController stdCtrl{get;set;}
	public Account acc{get;set;}
	public account accs {get;set;}
	//public string Aid {get;set;}
	public boolean editMode {get;set;}
	public Account accEdit{get;set;}
	//Public Id Id = ApexPages.currentPage().getParameters().get('id');
    public AccountSaveCont(ApexPages.StandardController std) {
		stdCtrl = std;
		acc = (Account) stdCtrl.getRecord();  
		editMode = false;
		if(acc.id != null){
			accs = [select Id,Name,site, type,accountNumber from Account where id=:acc.id]; 		
		} 	
    }

    public pageReference save1(){
      if(acc.site == null){
          acc.site.adderror('Please enter site');
      }else{
         upsert acc;    
           PageReference pg = new PageReference('/' + acc.Id);
            return pg;
      }
      
     return null;
    }
    
    public pageReference edit1(){
        pageReference pgEdit = new pageReference('/'+acc.Id+'/e?retURL=%2Fa'+acc.Id);
		editMode = true;
		if(acc.id != null){
		    accEdit = [select Id,Name,site, type,accountNumber from Account where id=:acc.id];
		} 	
        return null;
    }
    
}

Vf:
<apex:page standardController="Account" extensions="AccountSaveCont" >
    <apex:form >
        <!-- button -->
        <apex:pageBlock mode="maindetail" title="Account Detail">
            <apex:inlineEditSupport showOnEdit="saveButton,cancelButton" hideOnEdit="EditButton" />
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save1}" value="Save" id="saveButton"/>
                <apex:commandButton action="{!edit1}" value="Edit" id="EditButton"/>
                <apex:commandButton action="{!Delete}" value="Delete" id="deleteButton"/>
                <apex:commandButton action="{!cancel}" value="cancel" id="cancelButton"/>               
            </apex:pageBlockButtons>
			<!-- New button -->
            <apex:pageBlockSection title="My Content Section" columns="2" rendered = "{!NOT(editMode)}">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="{!$ObjectType.Account.Fields.name.label}" />
                    <apex:inputField value="{!Acc.name}"/>
                </apex:pageBlockSectionItem>               
                 <apex:pageBlockSectionItem >  
                     <apex:outputLabel value="{!$ObjectType.Account.Fields.Site.label}" />
                    <apex:inputField value="{!Acc.site}"/>
                </apex:pageBlockSectionItem>                    
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="{!$objectType.Account.Fields.Type.label}" />
                    <apex:inputField value="{!Acc.type}"/>
                </apex:pageBlockSectionItem>                  
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="{!$objectType.Account.Fields.website.label}" />
                    <apex:inputField value="{!Acc.accountNumber}"/>
                 </apex:pageBlockSectionItem>                     
            </apex:pageBlockSection>
			
			<!-- Edit Button -->
            <apex:pageBlockSection title="Edit section"   columns="2" rendered = "{!editMode}">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="{!$ObjectType.Account.Fields.name.label}" />
                    <apex:inputField value="{!accEdit.name}"/>
                </apex:pageBlockSectionItem>               
                 <apex:pageBlockSectionItem >  
                     <apex:outputLabel value="{!$ObjectType.Account.Fields.Site.label}" />
                    <apex:inputField value="{!accEdit.site}"/>
                </apex:pageBlockSectionItem>                    
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="{!$objectType.Account.Fields.Type.label}" />
                    <apex:inputField value="{!accEdit.type}"/>
                </apex:pageBlockSectionItem>                  
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="{!$objectType.Account.Fields.website.label}" />
                    <apex:inputField value="{!accEdit.accountNumber}"/>
                 </apex:pageBlockSectionItem>                     
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
Best Answer chosen by DJ 367
PawanKumarPawanKumar
public class AccountSaveCont{

    private ApexPages.StandardController stdCtrl{get;set;}
    public Account acc{get;set;}
    public account accs {get;set;}
    //public string Aid {get;set;}
    public boolean editMode {get;set;}
    public Account accEdit{get;set;}
    //Public Id Id = ApexPages.currentPage().getParameters().get('id');
    public AccountSaveCont(ApexPages.StandardController std) {
        stdCtrl = std;
        acc = (Account) stdCtrl.getRecord();  
        editMode = false;
        if(acc.id != null){
           //accs = [select Id,Name,site, type,accountNumber from Account whereid=:acc.id]; 
            acc = [select Id,Name,site, type,accountNumber from Account where id=:stdCtrl.getId()];         
        }     
    }

    public pageReference save1(){
      if(acc.site == null){
          acc.site.adderror('Please enter site');
      }else{
         upsert acc;    
           PageReference pg = new PageReference('/' + acc.Id);
            return pg;
      }
      
     return null;
    }
    
    public pageReference edit1(){
        pageReference pgEdit = new pageReference('/'+acc.Id+'/e?retURL=%2Fa'+acc.Id);
        editMode = true;
        if(acc.id != null){
            accEdit = [select Id,Name,site, type,accountNumber from Account where id=:acc.id];
        }     
        return null;
    }
    
}

Please let me know if it helps you.

Regards,
Pawan Kumar

All Answers

PawanKumarPawanKumar
public class AccountSaveCont{

    private ApexPages.StandardController stdCtrl{get;set;}
    public Account acc{get;set;}
    public account accs {get;set;}
    //public string Aid {get;set;}
    public boolean editMode {get;set;}
    public Account accEdit{get;set;}
    //Public Id Id = ApexPages.currentPage().getParameters().get('id');
    public AccountSaveCont(ApexPages.StandardController std) {
        stdCtrl = std;
        acc = (Account) stdCtrl.getRecord();  
        editMode = false;
        if(acc.id != null){
           //accs = [select Id,Name,site, type,accountNumber from Account whereid=:acc.id]; 
            acc = [select Id,Name,site, type,accountNumber from Account where id=:stdCtrl.getId()];         
        }     
    }

    public pageReference save1(){
      if(acc.site == null){
          acc.site.adderror('Please enter site');
      }else{
         upsert acc;    
           PageReference pg = new PageReference('/' + acc.Id);
            return pg;
      }
      
     return null;
    }
    
    public pageReference edit1(){
        pageReference pgEdit = new pageReference('/'+acc.Id+'/e?retURL=%2Fa'+acc.Id);
        editMode = true;
        if(acc.id != null){
            accEdit = [select Id,Name,site, type,accountNumber from Account where id=:acc.id];
        }     
        return null;
    }
    
}

Please let me know if it helps you.

Regards,
Pawan Kumar
This was selected as the best answer
PawanKumarPawanKumar
Hi DJ,

Please let me know if it helps you.

Regards,
Pawan Kumar
Saurabh Sood 12Saurabh Sood 12
Yeaah It comes when you are trying to access field which is not in query statement and use this inside controller statement
 accs = [select Id,Name,site, type,accountNumber from Account whereid=:stdCtrl .getId()] ;
DJ 367DJ 367
Hi ,

Thanks for your reply and it work. but I just want to know earlier I used acc.id and it should work, why it was not working?
Thanks.
PawanKumarPawanKumar
Good to know. it is working.
Because you were using 'accs' instead of 'acc'. please see the difference in my commented code. Minute error was there.

if(acc.id != null){
           //accs = [select Id,Name,site, type,accountNumber from Account whereid=:acc.id]; 
            acc = [select Id,Name,site, type,accountNumber from Account where id=:stdCtrl.getId()];         
        }     

Regards,
Pawan Kumar