[SalesForce] Trying to update related object fields from visualforce list

I have a visualforce page with a grid displaying an editable list of records from our Enrolment object. This list also includes a related field from the Contact, which is related to the Enrolment via a lookup field. It is important that this field be able to be updated next to the other enrolment columns.

The page itself is called from the Program_Instance__c object (which collects a set of enrolments in a specific instance of one of our training programs), and lists these Enrolments.

I can get the page to work fine, saving changes back to the Enrolment__c object. However any changes to the Contact__r.Is_Christian__c field are not saved. I have tried all sorts of approaches but cannot get the Contact record updated. Is there anyone that can help show me what I am missing so I can get this form working? Thanks so much.

Extension Class

public class BulkAcceptedChrist {

  // Constructor
 public BulkAcceptedChrist(ApexPages.StandardController controller) {
  this.pi = (Program_Instance__c)controller.getSubject();

     this.enrols = [ SELECT 
                        e.id,
                        e.name,
                        e.contact__c,
                        e.enrolment_status__c,
                        e.contact__r.IsChristian__c,
                        e.Accepted_Christ__c,
                        e.Accepted_Christ_Module__c,
                        e.Accepted_Christ_Date__c,
                        e.program_instance__c
                      FROM 
                        Enrolment__c e 
                      WHERE 
                        e.Program_Instance__c = :pi.id 
                        and (e.enrolment_status__c IN ('Approved Awaiting Deposit','Accepted','Graduate'))];

 }

 // Action Method called from page button
 public pagereference saveChanges() { 
  update this.enrols;
  return null;
 }

 // public Getter to provide table headers 
 public string[] getheaders() { return new string [] 
  {'Enrol No.','Contact','Enrolment Status','Is Christian','Accepted Christ', 'In Module',
   'On Date' } ; }

 // public Getter to list program instance enrolments
 public enrolment__c[] getenrols() { 
  return this.enrols; 
 } 

 // class variables
 Program_Instance__c pi;
 Enrolment__c[] enrols; 
}

Visualforce

<apex:page standardController="Program_Instance__c" extensions="BulkAcceptedChrist">
 <style>
.pbBody td {
 border-color: #E3DEB8;
 border-style: none none solid;
 border-width: medium medium 2px;
 padding-bottom: 4px;
 padding-top: 4px;
 width: 85px;
}
.pbBody input   { width: 105px;}
.pbBody .nameColumn { width: 125px;}
.hdr     {;}
</style>
<apex:form >
 <apex:messages />

 <apex:sectionHeader title="Enrolments for" subtitle="{!Program_Instance__c.name}" />
 <apex:pageBlock title="Edit Accepted Christ Details:">
  <apex:pageBlockButtons >
   <apex:commandButton action="{!saveChanges}" value="Save"
    rerender="main" status="ajaxStatus" />
   <apex:commandButton action="{!cancel}" value="Cancel" />
  </apex:pageBlockButtons>
  <apex:actionStatus id="ajaxStatus" startText="Updating enrolments...">
   <apex:facet name="stop">
   <apex:outputPanel id="main">
    <table>
    <tr>
     <apex:repeat value="{!headers}" var="h">
      <td class="hdr">{!h}</td>
     </apex:repeat>
    </tr>

    <apex:repeat value="{!enrols}" var="e">
     <tr>
      <td ><apex:outputField value="{!e.name}" /></td>
      <td><apex:outputField value="{!e.Contact__c}" /></td>
      <td><apex:outputField value="{!e.Enrolment_Status__c}" /></td>
      <td><apex:inputField value="{!e.Contact__r.IsChristian__c}" /></td>
      <td><apex:inputField value="{!e.Accepted_Christ__c}" /></td>
      <td><apex:inputField value="{!e.Accepted_Christ_Module__c}" /></td>
      <td><apex:inputField value="{!e.Accepted_Christ_Date__c}" /></td>
     </tr>
    </apex:repeat>
    </table>
   </apex:outputPanel>
   </apex:facet>
  </apex:actionStatus>
 </apex:pageBlock>
</apex:form>
</apex:page>

Best Answer

You are only updating the Enrollment records and not the Contact records. You have to explicitly call an update on the related contact record. May be like this:

    // Action Method called from page button
 public pagereference saveChanges() { 
  update this.enrols;
  list<Contact> lstToUpdate= new list<contact>();
  for(Integer i=0; i<this.enrols.size();i++)
  {
    lstToUpdate.add(this.enrols[i].Contact__r);
  }
  if(lstToUpdate.size() > 0)
    update lstToUpdate;
  return null;
 }
Related Topic