+ Start a Discussion
bentonbenton 

Save child join fields in datatable

I have a VF page that shows a datatable with a master-child relationship join. I have an input field on the child objects but can not figure out how to save. Everything displays properly but will not save my changes to child objects.

 

VF Page:

   <apex:pageBlockButtons >
           <apex:commandButton action="{!save4}" id="saveButton" value="Save"/>
           <apex:commandButton onclick="resetInlineEdit()" id="cancelButton" value="Cancel"/>
    </apex:pageBlockButtons>
    <apex:pageblockTable value="{!acclist}" var="o">
        <apex:column width="10%" style="text-align:center;" headerValue="Name"><apex:outputField value="{!o.name}"/>  </apex:column>
          <apex:column headerValue="Line_Items">
                <apex:pageBlockTable value="{!o.Line_Items__r}" var="a">
                <apex:column width="4%" style="text-align:center;" headerValue="Pay Vendor"><apex:inputField value="{!a.Vendor_Paid__c}"/> </apex:column>
                <apex:column width="4%" style="text-align:center;" headerValue="InvRcd"><apex:outputField value="{!a.Invoiced_Received__c}"/> </apex:column>
                </apex:pageBlockTable>    
          </apex:column>
        </apex:column>
    </apex:pageblockTable>

 

Controller:

        acclist = [Select name, id, (SELECT Name,Invoiced_Received__c, Vendor_Paid__c, Service_Order__c from Line_Items__r) From Account where id IN (Select Vendor__c from Line_Item__c) ORDER BY name];\

     public PageReference save4() {
     update acclist;
return null;
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney
Is should read LineItems.addAll(acc.line_items__r) AddAll instead of add Sent from my iPhone

All Answers

Ritesh AswaneyRitesh Aswaney

You will need to explicitly update the rows you've retrieved with the inner query.

A save on the parent will not commit the changes on the child.

 

 public PageReference save4() {
List<Line_Item__c> lineItems = new List<Line_Item__c>{};

for(Account acc : accList)
if(acc.Line_Items__r != null)
lineItems.add(acc.Line_Items__r);

udpate lineItems;
     update acclist;
return null;
}

 

bentonbenton
Thanks for the help. Now I'm getting an error Duplicate variable: lineItems (attempt to re-create the variable with type: udpate) any ideas??
bentonbenton

Nevermind to that. It was just a typo. But when I save on my VF Page I get a VisualFOrce Error

 

System.QueryException: List has more than 1 row for assignment to SObject

Error is in expression '{!save4}' in component <apex:page> in page payvendorlist2
Ritesh AswaneyRitesh Aswaney
Is should read LineItems.addAll(acc.line_items__r) AddAll instead of add Sent from my iPhone
This was selected as the best answer
bentonbenton

Perfect. your awesome. I appreciate the help.