0

I have a visual force page using an extension and the standard controller. I added a pagereference save() action to the page but it is only updating the child and grandchild records. How can I get this save button to update the standard controller as well as the child and grandchild records? Also, how can the page go to the standard controller's record after saving? I am new to apex so if you could add an explanation as to what is wrong and why your answer will work that would be perfect. I am trying to understand the system. Thank you in advance!

Extension

public with sharing class myClass {
private final Parent__c SiteP;
public List<Child__c> childList {get; set;}
public List<GrandChild__c> grandchildList {get; set;}
public myClass (ApexPages.StandardController stdController)
{ SiteP = [SELECT Id, Name,Status__c FROM Parent__c
               WHERE Id = 
:ApexPages.currentPage().getParameters().get('id')];
}

//Public lists here for Child and Grandchild records//

public PageReference saveAll() {
 update childlist;   
 List<GrandChild__c> children = new List<GrandChildren__c>();
     for (Child__c c : childList) {
         grandchildlist.addAll(c.GrandChild__r);
     }
 update grandchildlist; 
 update SiteP;
 return apexpages.currentpage();

} }

Visualforce page

<apex:page standardController="Parent__c" extensions="myClass">
 <apex:form >
    <apex:commandButton value="Save" action="{!saveAll}"/>

//Parent__c section here used from standard controller//

//Child__c code section here used from extension//

//GrandChild__c code section here used from extension//

 </apex:form>
</apex:page>

1 Answer 1

1

Don't query the parent back from the database; it's already in the Standard Controller:

public myClass (ApexPages.StandardController stdController)
{ SiteP = (Parent__c)stdController.getRecord();
}

Make sure any fields you're using for the Parent__c are bound on your page:

<apex:inputField value="{!Parent__c.Status__c}" />

Second, save a copy of the Standard Controller, and you can use its methods:

ApexPages.StandardController controller;
public myClass(ApexPages.StandardController stdController) {
  SiteP = (Parent__c)stdController.getRecord();
  controller = stdController;
}

To go back to the parent record's page when you're done, you can return the value from save():

public PageReference saveAll() {
  // ... omitted ...
  return controller.save();
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .