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 
                        Enrolment__c e 
                        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; 


<apex:page standardController="Program_Instance__c" extensions="BulkAcceptedChrist">
.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     {;}
<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:actionStatus id="ajaxStatus" startText="Updating enrolments...">
   <apex:facet name="stop">
   <apex:outputPanel id="main">
     <apex:repeat value="{!headers}" var="h">
      <td class="hdr">{!h}</td>

    <apex:repeat value="{!enrols}" var="e">
      <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>

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++)
  if(lstToUpdate.size() > 0)
    update lstToUpdate;
  return null;
  • Thanks Shailesh... As this.enrols is a list would I need to loop through the whole list to update each this.enrols[x] separately? Or is there a way to update the contact data as a set? – Kevin Wilson Jun 20 '16 at 9:00
  • @KevinWilson Something like updated answer OR you can create a set of contact IDs and fetch the contacts through SOQL. – Shailesh Patil Jun 20 '16 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.