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
KRayKRay 

Update Child Records using custom controller

Hey Forum,

I've created a visualforce page that displays a Parent(Parent__c) record with its only child record, Child__c.  My goal is to allow the user to update the child record if needed.  I'm able to display the child record but I've hit a road block when it comes to updating the child record. I've scaled the code back drastically but I'm unable to pass the values back to the controller.  I've read on several sites , it says to pass the data to a class variable then update it but the NEW values aren't being passed.  Can you help?
public class UpdateTheChild{

public Parent__c Parent;
public Child__c Child {get; set;}
public Integer childAge {get; set;}
public Id childId {get; set;}
public String childDOB {get; set;}

public void UpdateTheChild(ApexPages.standardController sc){
 this.Parent = (Parent__c)sc.getRecord();
}

public Parent__c getParent(){
 return Parent;
}

public Child__c getChild(){
Child__c onlyChild = [Select ID, Name, Age, DOB from Child__c WHERE ParentId = :Opportunity.Id limit 1];

return onlyChild;
}

public PageReference UpdateChild(){

System.debug(Child);
System.debug(childAge);
System.debug(childDOB);
//update Child;
}
 
<apex:form>

/** 
Parent will have page block here
**/
<apex:pageBlock >

<apex:pageBlockSection title="Child" columns="2" collapsible="false" showHeader="true">

<apex:outputField value="{!Child.Name}"/>
<apex:InputField value="{!Child.Age}"/>
<apex:InputField value="{!Child.DOB}"/>
<apex:commandbutton action="{!updateChildRecord}" value="Update" rerender="">

<apex:param name="childAge" value=" {!Child.Age}"  assignTo="{!childAge}"/>
<apex:param name="childId" value=" {!Child.Id}" assignTo="{!childId}"/>
<apex:param name="childDOB" value=" {!Child.DOB}" assignTo="{!childDOB}"/>

</apex:commandbutton>

</apex:pageBlockSection>
</apex:PageBlock>

<apex:form>

 
Best Answer chosen by KRay
Nayana KNayana K
<apex:form>

/** 
Parent will have page block here
**/
<apex:pageBlock id="childPB">

<apex:pageBlockSection title="Child" columns="2" collapsible="false" showHeader="true">

<apex:outputField value="{!Child.Name}"/>
<apex:InputField value="{!Child.Age}"/>
<apex:InputField value="{!Child.DOB}"/>
<apex:commandbutton action="{!updateChildRecord}" value="Update" rerender="childPB">

</apex:commandbutton>

</apex:pageBlockSection>
</apex:PageBlock>

<apex:form>

I have removed params. When you use inputField data will be binded. No need to use apex:param to pass values.

Can you please check once whether System.debug(Child); displays the values entered in page?

All Answers

Nayana KNayana K
<apex:form>

/** 
Parent will have page block here
**/
<apex:pageBlock id="childPB">

<apex:pageBlockSection title="Child" columns="2" collapsible="false" showHeader="true">

<apex:outputField value="{!Child.Name}"/>
<apex:InputField value="{!Child.Age}"/>
<apex:InputField value="{!Child.DOB}"/>
<apex:commandbutton action="{!updateChildRecord}" value="Update" rerender="childPB">

</apex:commandbutton>

</apex:pageBlockSection>
</apex:PageBlock>

<apex:form>

I have removed params. When you use inputField data will be binded. No need to use apex:param to pass values.

Can you please check once whether System.debug(Child); displays the values entered in page?
This was selected as the best answer
KRayKRay
Nayana, the value(s) are null. I noticed in my previous code snippet, the action reference within the commandbutton didn't match the PageReference's name. After updating the button, the values are still null. 
KRayKRay
Nayana, I got it!!! Instead of returning the getChild()'s "onlyChild" variable, I assigned the record to the class's Child variable. Now, when I update the child record, the value is being passed back.
public class UpdateTheChild{

public Parent__c Parent;
public Child__c Child {get; set;}
public Integer childAge {get; set;}
public Id childId {get; set;}
public String childDOB {get; set;}

public void UpdateTheChild(ApexPages.standardController sc){
 this.Parent = (Parent__c)sc.getRecord();
}

public Parent__c getParent(){
 return Parent;
}

public Child__c getChild(){
Child = [Select ID, Name, Age, DOB from Child__c WHERE ParentId = :Opportunity.Id limit 1];

return Child;
}

public PageReference UpdateChild(){

System.debug(Child);
System.debug(childAge);
System.debug(childDOB);
//update Child;
}