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
ckahlckahl 

Table to Edit and Create Records

I used Ron's example from the this post to make an editable table that displays related records and it works perfectly! Now, I'm trying to find a way that I can use this same page, but have it add rows to create new records in addition to editing existing records.  

 

I have an object called time_off_request__c that is the parent of time_off_day__c.  Basically, I want the user to be able to create a new time off request and add as many days as they'd like all on one screen.  

 

Here is my code-

 

VF Page

 

<apex:page standardController="Time_Off_Request__c" extensions="timeoffdayController">
 <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="Days for" subtitle="{!Time_Off_Request__c.name}" />
 <apex:pageBlock title="Edit Days">
  <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 time off request...">
   <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="{!Days}" var="a">
     <tr>
      <td><apex:inputField value="{!a.Date__c}" /></td>
      <td><apex:inputField value="{!a.Hours__c}" /></td>
      <td><apex:inputField value="{!a.Note__c}" /></td>
     </tr>
    </apex:repeat>
    </table>
   </apex:outputPanel>
   </apex:facet>
  </apex:actionStatus>
 </apex:pageBlock>
</apex:form>
</apex:page>

 

Class

 

public class timeoffdayController {
  
  // Constructor
 public timeoffdayController(ApexPages.StandardController controller) {
  this.request = (Time_off_request__c)controller.getSubject();
  
     this.day = [ SELECT 
      d.SystemModstamp, d.Name, 
      d.LastModifiedDate, d.LastModifiedById, d.LastActivityDate, 
      d.IsDeleted, d.Id, d.Date__c, d.CreatedDate, d.CreatedById, 
      d.Time_off_request__c, d.Hours__c, d.note__c  
   
      FROM 
      Time_off_day__c d 
      WHERE 
      d.time_off_request__c = :request.id ];
 }
 
 // Action Method called from page button
 public pagereference saveChanges() { 
  upsert this.day;
  return null;
 }
 
 // Action Method called from page link
 public pagereference newDeliverable() { 
  time_off_day__c d = new time_off_day__c();
  d.time_off_request__c =this.request.id; 
  day.add( d );
  return null;
 }
 
 // public Getter to provide table headers 
 public string[] getheaders() { return new string [] 
  {'Date','Hours','Note' } ; }
 
 // public Getter to list request days
 public time_off_day__c[] getDays() { 
  return this.day; 
 } 
 
 // class variables
 Time_off_request__c request;
 time_off_day__c[] day; 
}

 

 

 

ckahlckahl
Can anyone help?
Jeff MayJeff May

In your controller, add the methods that create the additional records, and make sure to update the List<> you are displaying on the VF page.  Have the pageBlock on the VF page rerender and the List<>< contents will be displayed as rows.

ckahlckahl

Thanks! I don't have a whole of experience creating controllers.  Can you point in the direction of how to add the methods that create additional records?

Jeff MayJeff May

You will have to create your own controller.  The Visual Force Dev Guide is my right-hand when I'm learning new things.  As you start to write your code, please lean on this group to help you understand parts that are confusing.  Here is a link to the VF Dev guide in case you haven't found that yet: 

 

http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#CSHID=pages_compref.htm|StartTopic=Content%2Fpages_compref.htm|SkinName=webhelp

 

Happy Programming!