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
Richa_LearningRicha_Learning 

SObject row was retrieved via SOQL without querying

Hi, 
I am working on Cloning cases. I am creating a custom button because we only want few fields to get cloned. I get this error when i click on the button clone

SObject row was retrieved via SOQL without querying the requested field: Case.Status​
 
Class:

public class VFController {
 
   
    public case o {get;set;} 
   
    public VFController(ApexPages.StandardController stdController) {
        this.o = (case)stdController.getRecord();
    }
     
    // Code we will invoke on page load.
    public PageReference autoRun() {
 
        String theId = ApexPages.currentPage().getParameters().get('id');
 
        if (theId == null) {
            // Display the Visualforce page's content if no Id is passed over
            return null;
        }
 
        List<Case> cases = [Select id,lot__c,status From Case ];
            // Do all the dirty work we need the code to do
        
 
        // Redirect the user back to the original page
        PageReference pageRef = new PageReference('/' + theId);
        pageRef.setRedirect(true);
        return pageRef;
 
    }
 
}
 
VF page:
<apex:page standardController="Case" extensions="VFController">
 

    <apex:form id="frmId">
      
       <apex:pageBlock id="pb">
      
         <apex:messages ></apex:messages>
             <apex:pageBlockSection columns="1" title="Clones Case" id="pbs_Clone">
                 <apex:InputField value="{!o.status}"/>
                 
            
                 <apex:InputField value="{!o.LOT__c}" required="true"/>
                 
                 

                 
            </apex:pageBlockSection>
         <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}" />
            <apex:commandButton value="Cancel" action="{!Cancel}" />
         </apex:pageBlockButtons> 
         
 </apex:pageblock>
 </apex:form>
 
 </apex:page>

 
Best Answer chosen by Richa_Learning
Amit Chaudhary 8Amit Chaudhary 8
Please change your code like below
public class VFController 
{
    public case o {get;set;} 

    public VFController(ApexPages.StandardController stdController) 
	{
        this.o = (case)stdController.getRecord();
		if(o.id != null)
		{
			o = [Select id,lot__c,status From Case where id=:o.id];
		}	
    }
     
    // Code we will invoke on page load.
    public PageReference autoRun() 
	{
        String theId = ApexPages.currentPage().getParameters().get('id');
        if (theId == null) 
		{
            // Display the Visualforce page's content if no Id is passed over
            return null;
        }
 
        List<Case> cases = [Select id,lot__c,status From Case ];
            // Do all the dirty work we need the code to do
        
 
        // Redirect the user back to the original page
        PageReference pageRef = new PageReference('/' + theId);
        pageRef.setRedirect(true);
        return pageRef;
 
    }
 
}

 

All Answers

Gupta.VishalGupta.Vishal
Hi Richa , 

You need to query the field  for the bind variable you are using in the VF page which in this case is "o".

after this line 
this.o = (case)stdController.getRecord();

do this 
Case myCase=[Select id,lot__c,status From Case where id=:o.id];

and use myCase as the bind Variable.
go through below link . 
https://developer.salesforce.com/docs/atlas.en-us.198.0.pages.meta/pages/apex_ApexPages_StandardController_getRecord.htm


 
Amit Chaudhary 8Amit Chaudhary 8
Please change your code like below
public class VFController 
{
    public case o {get;set;} 

    public VFController(ApexPages.StandardController stdController) 
	{
        this.o = (case)stdController.getRecord();
		if(o.id != null)
		{
			o = [Select id,lot__c,status From Case where id=:o.id];
		}	
    }
     
    // Code we will invoke on page load.
    public PageReference autoRun() 
	{
        String theId = ApexPages.currentPage().getParameters().get('id');
        if (theId == null) 
		{
            // Display the Visualforce page's content if no Id is passed over
            return null;
        }
 
        List<Case> cases = [Select id,lot__c,status From Case ];
            // Do all the dirty work we need the code to do
        
 
        // Redirect the user back to the original page
        PageReference pageRef = new PageReference('/' + theId);
        pageRef.setRedirect(true);
        return pageRef;
 
    }
 
}

 
This was selected as the best answer
RatanRatan
Or in your controller Just add fields like this..
 
public VFController(ApexPages.StandardController stdController) {
       
       //Skip blow line for test class else it will give error
       if(!Test.isRunningTest)
              stdController.addFields(new List<String>{'Status'});
        this.o = (case)stdController.getRecord();
    }

http://bobbuzzard.blogspot.in/2011/04/dynamic-visualforce-bindings-and.html
Richa_LearningRicha_Learning
thanks this worked,.. but when i use this clone button on cases...after i save ,the record is not getting created and it redirects me to the original case.