Search code examples
salesforcevisualforceapex

Update quantity of multiple opportunity line items on single Visualforce page


I have a visualforce page that displays on the Opportunity page layout inline. The goal is to display each opportunity line item (OLI) that is associated with the opportunity, with a quantity inputfield. I need to be able to change the quantity of the OLI from the VF page. I have a controller that is extended off the standard opportunity controller and everything displays correctly, but when I try to save with my custom save method, the page refreshes and the changes to the fields are not updated. Any help is appreciated!

VF PAGE:

<apex:page standardController="Opportunity" extensions="OLIController">
<apex:form >
<apex:pageBlock title="Opportunity Products">



    <apex:pageBlockTable var="OLI" value="{!OLIs}" id="newProduct">
        <apex:column value="{!OLI.name}"/>
        <apex:column headerValue="Quantity">

            <apex:inputfield id="Quantity" value="{!OLI.Quantity}"/>

        </apex:column>

    </apex:pageBlockTable>

    <apex:pageBlockButtons >
        <apex:commandButton value="Save" action="{!saveIt}" immediate="false"/>
    </apex:pageBlockButtons>

</apex:pageBlock>
</apex:form>

CONTROLLER: public with sharing class OLIController {

public ApexPages.StandardController sc;
public Opportunity Opp {get;set;}
private Map<Id, OpportunityLineItem> oliItems;
public List<OpportunityLineItem> OLIlist2 {get ;set;}

public OLIController(ApexPages.StandardController sc) { 
this.Opp = (Opportunity)sc.getRecord();

}


public List<OpportunityLineItem> getOLIs() {

    List<OpportunityLineItem> OLIlist2 = [Select Name, ID, Quantity, OpportunityId FROM OpportunityLineItem WHERE OpportunityId =:Opp.Id];

    return OLIlist2;

}
public PageReference saveIt() {
    List<OpportunityLineItem> listOLI = getOLIs();

    update listOLI;

    return null;

}

}


Solution

  • Problem solved;

    Just needed to add the data from the "get" part of the OLIlist2 from the controller to the OLIController sc. See modified code below.

    public with sharing class OLIController {
    
    public ApexPages.StandardController sc;
    public Opportunity Opp {get;set;}
    public List<OpportunityLineItem> OLIlist2 {get ;set;}
    
    public OLIController(ApexPages.StandardController sc) { 
    this.Opp = (Opportunity)sc.getRecord();
    OLIlist2 = [Select Name, ID, Quantity, OpportunityId FROM OpportunityLineItem WHERE OpportunityId =:Opp.Id];
    }
    
    
    public List<OpportunityLineItem> getOLIs() {
    
        List<OpportunityLineItem> OLIlist2 = [Select Name, ID, Quantity, OpportunityId FROM OpportunityLineItem WHERE OpportunityId =:Opp.Id];
    
        return OLIlist2;
    
    }
    public PageReference saveIt() {
       // List<OpportunityLineItem> listOLI = getOLIs();
    
        update OLIlist2;
    
        return null;
    
    }
    

    }