1

I have a custom controller with a list of parent records (opportunities) and a list of their child records (opportunitylineitems). I want to save both the parent fields and child fields with one button. I know my update method needs to add the child list but I don't know how to get the child list to update.

Here is my code.

Controller

    public PageReference getopps()
{

    Set<String> oppids = new Set<String>();
        for (OpportunityLineItem oline : opps) oppids.add(oline.opportunityId);

    oppparents = [Select Id, StageName,Opportunity_Notes__c,Manager_Note__c,AccountId, Account.Name
                  ,name,Resellers_and_Type__c,(SELECT Quantity,Product2Id,product2.Name,ServiceDate
                                               ,Ship_Out_Date__c FROM OpportunityLineItems)                         
                  from opportunity where id in: oppids order by name];


    system.debug(opps);

    return null;

} 
public PageReference updateopps(){  
   //save the opportunities after update or send error message to admin


    update oppparents;
}

Visualforce

<apex:pageBlockButtons location="both">
   <apex:commandButton value="Search" action="{!getopps}" rerender="block" status="status" oncomplete="pos();"/>
   <apex:commandButton value="Save & Update" action="{!updateopps}" rerender="block" status="status"/>

    <apex:pageBlockSection id="sect2" >
      <apex:outputLabel for="oenduser" style="float:right;margin-right:150px;">End User&nbsp; &nbsp; 
      <apex:inputText id="oenduser" value="{!oppenduser}" onkeypress="return onKeyup_TxtFieldcom(event);"/></apex:outputLabel>
      <apex:outputLabel for="oreseller" style="float:right;margin-right:150px;">Reseller&nbsp; &nbsp; 
      <apex:inputText id="oreseller" value="{!oppreseller}" onkeypress="return onKeyup_TxtFieldcom(event);"/></apex:outputLabel>
      <apex:outputLabel for="oproduct" style="float:right;margin-right:150px;">Product Sku&nbsp; &nbsp; 
      <apex:inputText id="oproduct" value="{!oppprod}" onkeypress="return 
onKeyup_TxtFieldcom(event);"/></apex:outputLabel> 
      <apex:pageBlockSectionItem />
      <apex:pageBlockSectionItem />
    </apex:pageBlockSection><br/>

    <apex:actionFunction name="search" action="{!getopps}" rerender="block" 
status="status"/>

  <apex:actionStatus id="status" startText="Searching... please wait..."/>
 <apex:pageBlockSection title="Results" id="resultsBlock" columns="1">
   <apex:pageBlockTable value="{!oppparents}" var="opp" rendered="
{!NOT(ISNULL(oppparents))}">
       <apex:column headerValue="End User" width="100">
      <apex:outputLink value="/{!opp.Id}">{!opp.Account.Name}
</apex:outputLink>

      </apex:column>
      <apex:column headerValue="Resellers" width="100">
        <apex:outputfield value="{!opp.Resellers_and_Type__c}" />
          <br/>
          <br/>
            <apex:dataTable value="{!opp.OpportunityLineItems}" var="item" rendered="{!NOT(ISNULL(opps))}" id="lineitems">
                <apex:column headerValue="Product" width="200">
                <apex:outputfield value="{!item.Product2.Name}"/>
            </apex:column>
            <apex:column headerValue="Quantity" width="200">
                <apex:inputfield value="{!item.Quantity}"/>
            </apex:column>
            <apex:column headerValue="Ship Out Date" width="200">
                <apex:inputfield value="{!item.Ship_Out_Date__c}"/>
            </apex:column>    
           </apex:dataTable>
      </apex:column>
       <apex:column headerValue="Stage" width="100">
        <apex:inputfield value="{!opp.StageName}" />
      </apex:column>
       <apex:column headerValue="Pipeline Notes" width="100">
        <apex:inputfield value="{!opp.Opportunity_Notes__c}" />
      </apex:column>           



        </apex:pageBlockTable>
2
  • put only relevant code with the context of your question. and first give a try at your end – Santanu Boral Aug 16 '17 at 21:24
  • Sorry. I edited the code. – disasterlooms Aug 16 '17 at 22:09
2

In your updateOpps method, you need to loop through the opps, build a list of OppLineItems, then update that list. Something like this:

List<OpportunityLineItem> olis = new List<OpportunityLineItem>();
for (Opportunity opp : opps) {
  olis.addAll(opp.OpportunityLineItems);
}
update olis;
1
  • This is at the most a comment as specified in How to Answer, please include details as to how/what the user should do alongside any code with explanations. Thanks! – glls Aug 17 '17 at 1:05

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.