0

I've fumbled my way through the visualforce page below and I'm wondering how I can get the user input to save.

<apex:page lightningStylesheets="true" standardController="Inventory_Transfer_Sheet__c">            
<!--<div style="width:700px">-->
<apex:form>
<table style = "width:100%; font-size:12px; ">
<tr>
<td style = "width:33%; text-align:left; font-family:SansSerif; font-weight:bold;">
    Technician: {!Inventory_Transfer_Sheet__c.Employee_Name__r.Name}
</td>
<td style = "width:33%; text-align:center; font-family:SansSerif; font-weight:bold;">
    Inventory Transfer Sheet: {!Inventory_Transfer_Sheet__c.Name}
</td>
<td style = "width:33%; text-align:right; font-family:SansSerif; font-weight:bold;">
    <apex:outputText value="Date: {0, date, MMM/dd/yyyy}" >
        <apex:param value="{!Inventory_Transfer_Sheet__c.Date__c}"/>
    </apex:outputText>
</td>
</tr>
<tr>
<td style = "width:33%; text-align:left; font-family:SansSerif; font-weight:bold;">
    Account: {!Inventory_Transfer_Sheet__c.Account__r.Name}
</td>
<td style = "width:33%; text-align:center; font-family:SansSerif; font-weight:bold;">
    <apex:outputText value="Work Order: {!Inventory_Transfer_Sheet__c.Work_Order__r.Banner_Number__c}" escape="false"/>
</td>
<td style = "width:33%; text-align:right; font-family:SansSerif; font-weight:bold;">
    Truck: {!Inventory_Transfer_Sheet__c.Truck__c}
</td>
</tr>
</table>
<br/>
<br/>
<table style = "width:100%; font-size:12px; ">
<tr>
<td style = "width:15%; text-align:center; font-family:SansSerif; font-weight:bold;">
    Item:
</td>
<td style = "width:40%; text-align:left; font-family:SansSerif; font-weight:bold;">
    Description:
</td>
<td style = "width:15%; text-align:center; font-family:SansSerif; font-weight:bold;">
    Unit:
</td>
<td style = "width:15%; text-align:center; font-family:SansSerif; font-weight:bold;">
    Qty Taken:
</td>
<td style = "width:15%; text-align:center; font-family:SansSerif; font-weight:bold;">
    Qty Used:
</td>
</tr>
<apex:repeat value="{!Inventory_Transfer_Sheet__c.Inventory_Transfer_Lines__r}" var="lineItems">
<tr>
<td style = "width:15%; text-align:center; font-family:SansSerif; font-weight:normal;">
    {!lineItems.Item__r.Name}
</td>
<td style = "width:40%; text-align:left; font-family:SansSerif; font-weight:normal;">
    {!lineItems.Description__c}
</td>
<td style = "width:15%; text-align:center; font-family:SansSerif; font-weight:normal;">
    {!lineItems.Unit_of_Measurement__c}
</td>
<td style = "width:15%; text-align:center; font-family:SansSerif; font-weight:normal;">
    {!lineItems.Qty_Taken__c}
</td>
<td style = "width:15%; text-align:center; font-family:SansSerif; font-weight:normal;">
    <apex:inputField value="{!lineItems.Qty_Used__c}"/>
</td>
</tr>
</apex:repeat>
</table>
<apex:commandButton  action="{!save}" value="Save"/>
</apex:form>
<!--</div>-->
</apex:page>
1
  • the standard controller won't save relatedlist items; you'll need a controller extension – cropredy Mar 25 at 22:12
1

To save parent/child objects, you need some Apex (or JavaScript). Here's one possible approach:

<apex:page extensions="InventoryTransferSheetController" lightningStylesheets="true" standardController="Inventory_Transfer_Sheet__c">            

...

<apex:commandButton  action="{!saveChildren}" value="Save"/>

Add an Apex class:

public inherited sharing InventoryTransferSheetController {
  Inventory_Transfer_Sheet__c record;
  public InventoryTransferSheetController(ApexPages.StandardController controller) {
    record = (Inventory_Transfer_Sheet__c)controller.getRecord();
  }
  public void saveChildren() {
    update record.Inventory_Transfer_Lines__r;
  }
}
4
  • Thank you for your reply! Will I need a test class to go alongside the new class? – Kylan Mar 25 at 22:44
  • @Kylan Yes, it'll need a unit test. Fortunately, should only be maybe 20-30 lines of code, though. – sfdcfox Mar 25 at 23:02
  • Just a quick question, would there be any way the {!saveChildren} action could direct the user back to the original record like the standard {!save} action does? – Kylan Mar 25 at 23:58
  • @Kylan Yes: public PageReference saveChildren() { update record.Inventory_Transfer_Lines__r; return new ApexPages.StandardController(record).view(); } – sfdcfox Mar 26 at 0:42

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.