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
Mathew Andresen 5Mathew Andresen 5 

edit page for visualforce page of object

Hi,

I have created a visualforce page to replace the standard page of my detail object (in this case Client_Plan__c as master, and Client_Plan_Product__c as detail).  This seems to be working fine for creating a new record, or save and new for a record.  However, when I try to go into edit mode from the parent object I get the error

"SObject row was retrieved via SOQL without querying the requested field: Client_Plan_Product__c.Type__c "
Here is the URL that is getting passed in on the failed edit
"https://c.cs13.visual.force.com/apex/ClientProductPage?id=a0eW0000001t54N&retURL=%2Fa0QW0000002ILIY&sfdc.override=1"


I'm not quite sure what I'm doing wrong

thanks,
public class ClientProductClass {
    
    public Client_Plan_Product__C prod {get; set;}
    string prodId;
    string clientId;
    String retUrl;
    
    
    public ClientProductClass(ApexPages.StandardController stdcon) {
        prodId = ApexPages.currentPage().getParameters().get('id');  // access the product id from the URL
        clientId = ApexPages.currentPage().getParameters().get('CF00N50000003LQm5_lkid');  // access the product id from the URL
		
        
        if (prodId != null && prodId !='') {
            System.debug('id = ' +prodId);
            prod=(Client_Plan_Product__c)stdcon.getRecord();
        } else { 
            prod = new Client_Plan_Product__c(); 
            prod.Client_Plan__C = clientId;
               
               }
          
    }
    
    public PageReference save()
     {

       upsert prod; 
       // returns the page to the parent object after savings  
       Client_Plan__c plan = new Client_Plan__c(id=prod.Client_Plan__c);
       PageReference clientPage = new ApexPages.StandardController(plan).view();
       clientPage.setRedirect(true);
       return clientPage;   
     }
     
     public PageReference Cancel()
     {
      return null;
     }

    public PageReference saveNew() {

       upsert prod; 
            prod = new Client_Plan_Product__c(); 
            prod.Client_Plan__C = clientId;
       PageReference clientPage = new ApexPages.StandardController(prod).view();
       clientPage.setRedirect(true);
       return clientPage;  
    }
    
    
}
 
public class ClientProductClass {
    
    public Client_Plan_Product__C prod {get; set;}
    string prodId;
    string clientId;
    String retUrl;
    
    
    public ClientProductClass(ApexPages.StandardController stdcon) {
        prodId = ApexPages.currentPage().getParameters().get('id');  // access the product id from the URL
        clientId = ApexPages.currentPage().getParameters().get('CF00N50000003LQm5_lkid');  // access the product id from the URL
		
        
        if (prodId != null && prodId !='') {
            System.debug('id = ' +prodId);
            prod=(Client_Plan_Product__c)stdcon.getRecord();
        } else { 
            prod = new Client_Plan_Product__c(); 
            prod.Client_Plan__C = clientId;
               
               }
          
    }
    
    public PageReference save()
     {

       upsert prod; 
       // returns the page to the parent object after savings  
       Client_Plan__c plan = new Client_Plan__c(id=prod.Client_Plan__c);
       PageReference clientPage = new ApexPages.StandardController(plan).view();
       clientPage.setRedirect(true);
       return clientPage;   
     }
     
     public PageReference Cancel()
     {
      return null;
     }

    public PageReference saveNew() {

       upsert prod; 
            prod = new Client_Plan_Product__c(); 
            prod.Client_Plan__C = clientId;
       PageReference clientPage = new ApexPages.StandardController(prod).view();
       clientPage.setRedirect(true);
       return clientPage;  
    }
    
    
}

 
Best Answer chosen by Mathew Andresen 5
AMIT KAMBOJAMIT KAMBOJ
Ideally error should not come. Not sure if this is due to class API version being old or not. Anyways below code has some other workaround.
Please check if this helps.
public class ClientProductClass {
    
    public Client_Plan_Product__C prod {get; set;}
//    ApexPages.Standardcontroller cont;// Amit 
    string prodId;
    string clientId;
    String retUrl;
    
    
    public ClientProductClass(ApexPages.StandardController stdcon) {
//        this.cont = stdcon; // Amit 
//	cont.addFields('Type__c'); // Amit
        prodId = ApexPages.currentPage().getParameters().get('id');  // access the product id from the URL
        clientId = ApexPages.currentPage().getParameters().get('CF00N50000003LQm5_lkid');  // access the product id from the URL
		
        
        if (prodId != null && prodId !='') {
            
            System.debug('id = ' +prodId);
//            this.prod=(Client_Plan_Product__c)stdcon.getRecord(); // Amit added this
	      this.prod = [select Id,Type__c,Tier__c,Variety__c,Appelation__c,Brand__c,Quantity__c,FOB__c from Client_Plan_Product__C Where Id=:prodId]; // Amit alternate way	
            system.Debug(prod);

        } else { 
            this.prod = new Client_Plan_Product__c(); // Amit added this
            prod.Client_Plan__C = clientId;
               
               }
      //prod.addFields('Type__c');    
    }
    
    public PageReference save()
     {

       upsert prod; 
       // returns the page to the parent object after savings  
       Client_Plan__c plan = new Client_Plan__c(id=prod.Client_Plan__c);
       PageReference clientPage = new ApexPages.StandardController(plan).view();
       clientPage.setRedirect(true);
       return clientPage;   
     }
     
     public PageReference Cancel()
     {
      return null;
     }

    public PageReference saveNew() {

       upsert prod; 
            prod = new Client_Plan_Product__c(); 
            prod.Client_Plan__C = clientId;
       PageReference clientPage = new ApexPages.StandardController(prod).view();
       clientPage.setRedirect(true);
       return clientPage;  
    }
    
    
}


 

All Answers

AMIT KAMBOJAMIT KAMBOJ
Please share the page also.
AMIT KAMBOJAMIT KAMBOJ

Hi Mathew,

Please add this line in your constructor first line. e.g. after line 9 and see if this resolves the issue.

stdcon.addFields('Type__c');

if it works please mark this answer as solution for sure.

Thanks

Amit

AMIT KAMBOJAMIT KAMBOJ
Also try to expose this Type field i VF page in view mode
Mathew Andresen 5Mathew Andresen 5
Sorry
 
<apex:page standardcontroller="Client_Plan_Product__c" extensions="ClientProductClass">
  <apex:form >
        <apex:pageBlock title="Enter Client Plan Products">
 
              <apex:commandButton value="Save" action="{!save}"/>
              <apex:commandButton value="Cancel" action="{!cancel}"/>
          	  <apex:commandButton value="Save & New" action="{!savenew}"/>
	
        
        
            <apex:pageBlockSection >
			<apex:pageBlockTable value="{!prod}" var="pd" columns="7">

			<apex:column >
                <apex:facet name="header">Type</apex:facet>  
      	        <apex:inputField value="{!pd.Type__c}" label="Type" />	
			</apex:column>	
			<apex:column >
                <apex:facet name="header">Tier</apex:facet>  
      	        <apex:inputField value="{!pd.Tier__c}" label="Type" />	
			</apex:column>	
			<apex:column >
                <apex:facet name="header">Variety</apex:facet>  
      	        <apex:inputField value="{!pd.Variety__c}" label="Type" />	
			</apex:column>	
			<apex:column >
                <apex:facet name="header">Appelation</apex:facet>  
      	        <apex:inputField value="{!pd.Appelation__c}" label="Type" />	
			</apex:column>	
			<apex:column >
                <apex:facet name="header">Brand</apex:facet>  
      	        <apex:inputField value="{!pd.Brand__c}" label="Type" />	
			</apex:column>	
			<apex:column >
                <apex:facet name="header">Quantity</apex:facet>  
      	        <apex:inputField value="{!pd.Quantity__c}" label="Type" />	
			</apex:column>	
			<apex:column >
                <apex:facet name="header">FOB</apex:facet>  
      	        <apex:inputField value="{!pd.FOB__c}" label="Type" />	
			</apex:column>	

                
			</apex:pageBlockTable> 	
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
    
</apex:page>

Also, I get the following error when I try "stdcon.addFields('Type__c');"

Method does not exist or incorrect signature: [ApexPages.StandardController].addFields(String)
AMIT KAMBOJAMIT KAMBOJ
Try to add below just before the constructor ends.

prod.addFields('Type__c');
Mathew Andresen 5Mathew Andresen 5
Sorry I still get  "Method does not exist or incorrect signature: [Client_Plan_Product__c].addFields(String)"
 
public class ClientProductClass {
    
    public Client_Plan_Product__C prod {get; set;}
    string prodId;
    string clientId;
    String retUrl;
    
    
    public ClientProductClass(ApexPages.StandardController stdcon) {
        
        prodId = ApexPages.currentPage().getParameters().get('id');  // access the product id from the URL
        clientId = ApexPages.currentPage().getParameters().get('CF00N50000003LQm5_lkid');  // access the product id from the URL
		
        
        if (prodId != null && prodId !='') {
            
            System.debug('id = ' +prodId);
            prod=(Client_Plan_Product__c)stdcon.getRecord();
            system.Debug(prod);

        } else { 
            prod = new Client_Plan_Product__c(); 
            prod.Client_Plan__C = clientId;
               
               }
      prod.addFields('Type__c');    
    }
    
    public PageReference save()
     {

       upsert prod; 
       // returns the page to the parent object after savings  
       Client_Plan__c plan = new Client_Plan__c(id=prod.Client_Plan__c);
       PageReference clientPage = new ApexPages.StandardController(plan).view();
       clientPage.setRedirect(true);
       return clientPage;   
     }
     
     public PageReference Cancel()
     {
      return null;
     }

    public PageReference saveNew() {

       upsert prod; 
            prod = new Client_Plan_Product__c(); 
            prod.Client_Plan__C = clientId;
       PageReference clientPage = new ApexPages.StandardController(prod).view();
       clientPage.setRedirect(true);
       return clientPage;  
    }
    
    
}



 
AMIT KAMBOJAMIT KAMBOJ
Hi Mathew,

Please try this code.

FYI : I noticed that your VF pageblock table needs '!prod' as List but it is not a list in controller.
public class ClientProductClass {
    
    public Client_Plan_Product__C prod {get; set;}
    ApexPages.Standardcontroller cont;// Amit 
    string prodId;
    string clientId;
    String retUrl;
    
    
    public ClientProductClass(ApexPages.StandardController stdcon) {
        this.cont = stdcon; // Amit 
	cont.addFields('Type__c'); // Amit
        prodId = ApexPages.currentPage().getParameters().get('id');  // access the product id from the URL
        clientId = ApexPages.currentPage().getParameters().get('CF00N50000003LQm5_lkid');  // access the product id from the URL
		
        
        if (prodId != null && prodId !='') {
            
            System.debug('id = ' +prodId);
            this.prod=(Client_Plan_Product__c)stdcon.getRecord(); // Amit added this
            system.Debug(prod);

        } else { 
            this.prod = new Client_Plan_Product__c(); // Amit added this
            prod.Client_Plan__C = clientId;
               
               }
      //prod.addFields('Type__c');    
    }
    
    public PageReference save()
     {

       upsert prod; 
       // returns the page to the parent object after savings  
       Client_Plan__c plan = new Client_Plan__c(id=prod.Client_Plan__c);
       PageReference clientPage = new ApexPages.StandardController(plan).view();
       clientPage.setRedirect(true);
       return clientPage;   
     }
     
     public PageReference Cancel()
     {
      return null;
     }

    public PageReference saveNew() {

       upsert prod; 
            prod = new Client_Plan_Product__c(); 
            prod.Client_Plan__C = clientId;
       PageReference clientPage = new ApexPages.StandardController(prod).view();
       clientPage.setRedirect(true);
       return clientPage;  
    }
    
    
}

 
Mathew Andresen 5Mathew Andresen 5
I tried copy and paste the whole thing, but still got
"Method does not exist or incorrect signature: [ApexPages.StandardController].addFields(String)"
AMIT KAMBOJAMIT KAMBOJ
Ideally error should not come. Not sure if this is due to class API version being old or not. Anyways below code has some other workaround.
Please check if this helps.
public class ClientProductClass {
    
    public Client_Plan_Product__C prod {get; set;}
//    ApexPages.Standardcontroller cont;// Amit 
    string prodId;
    string clientId;
    String retUrl;
    
    
    public ClientProductClass(ApexPages.StandardController stdcon) {
//        this.cont = stdcon; // Amit 
//	cont.addFields('Type__c'); // Amit
        prodId = ApexPages.currentPage().getParameters().get('id');  // access the product id from the URL
        clientId = ApexPages.currentPage().getParameters().get('CF00N50000003LQm5_lkid');  // access the product id from the URL
		
        
        if (prodId != null && prodId !='') {
            
            System.debug('id = ' +prodId);
//            this.prod=(Client_Plan_Product__c)stdcon.getRecord(); // Amit added this
	      this.prod = [select Id,Type__c,Tier__c,Variety__c,Appelation__c,Brand__c,Quantity__c,FOB__c from Client_Plan_Product__C Where Id=:prodId]; // Amit alternate way	
            system.Debug(prod);

        } else { 
            this.prod = new Client_Plan_Product__c(); // Amit added this
            prod.Client_Plan__C = clientId;
               
               }
      //prod.addFields('Type__c');    
    }
    
    public PageReference save()
     {

       upsert prod; 
       // returns the page to the parent object after savings  
       Client_Plan__c plan = new Client_Plan__c(id=prod.Client_Plan__c);
       PageReference clientPage = new ApexPages.StandardController(plan).view();
       clientPage.setRedirect(true);
       return clientPage;   
     }
     
     public PageReference Cancel()
     {
      return null;
     }

    public PageReference saveNew() {

       upsert prod; 
            prod = new Client_Plan_Product__c(); 
            prod.Client_Plan__C = clientId;
       PageReference clientPage = new ApexPages.StandardController(prod).view();
       clientPage.setRedirect(true);
       return clientPage;  
    }
    
    
}


 
This was selected as the best answer
Mathew Andresen 5Mathew Andresen 5
This worked!   However, when I go into the record through edit, and hit save, the page doesn't return to the parent object page.  When I create a new child object and hit save it does.
Mathew Andresen 5Mathew Andresen 5
hmm, I think I see why, the clientID isn't making it's way down there.
Mathew Andresen 5Mathew Andresen 5
So I need to get the retURL and covert that to an ID I think?
Mathew Andresen 5Mathew Andresen 5
Ok, I got it working, added some code as below.  Thanks so much for your help, I really appreciate it.
 
public PageReference save()
     {

       upsert prod; 
       PageReference clientPage;
       // returns the page to the parent object after savings  
       
         if(returl==NULL) { // if the object was just created, get the return id from parent
       Client_Plan__c plan = new Client_Plan__c(id=this.clientId);
        clientPage = new ApexPages.StandardController(plan).view();
         } else {  // if the record came from the edit screen
          ClientPage = new PageReference(retUrl); 
         }
       clientPage.setRedirect(true);
       return clientPage;   
     }

 
AMIT KAMBOJAMIT KAMBOJ
Cool.