0

I have a simple VF page that displays 4 fields from a list of OpportunityLineItems. Two of the fields are input fields. Any changes I make do not get saved back to the original record. What changes should I make to my controller to get this to work as intended?

VF Page:

<apex:page standardController="Opportunity" extensions="OLI_SplitQty2" id="thePage" tabStyle="Product2" sidebar="false">
<!-- Style for Page Messages -->
<style>
    .Messages { color: red; font-weight: strong;}
    .StatusPicklist { }
</style>

<!-- Javascript -->
<script>
    // Script to provide pop-up on 'Cancel' button click
    function confirmCancel() {
        var isCancel = confirm("Are you sure you wish to cancel?");
        if (isCancel) return true;

        return false;
    }
</script>

<!-- Section Header -->
<apex:sectionHeader title="Split Products by Quantity" />
<!-- Steps for the end users -->
<!--
<h1>Notes:</h1>
<ol>
    <li>Select the Product(s) you want to split and</li>
    <li>enter a quantity to split off</li>
</ol>
-->

<!-- Form with Page Blocks and Sections -->
<apex:form >
    <!-- Page Block providing the user with Editable Table -->   
    <apex:pageBlock title="Select the Product(s) you want to split by entering a Quantity to split off and a new Line Description (optional)">

        <apex:pageBlockButtons location="top">
            <apex:commandButton action="{!split}" value="Split" immediate="false"/>
            <apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true"/>
        </apex:pageBlockButtons>

        <apex:messages styleClass="Messages"/>

        <apex:pageBlockSection id="editableView" title="Opportunity Product List View" columns="1" rendered="true" collapsible="false" >
            <apex:pageBlockTable value="{!OLIs}" var="val">

                <apex:column >
                    <apex:facet name="header">Product Name</apex:facet>
                        <apex:outputLink value="/{!val.ID}" >{!val.Product_Name__c}</apex:outputLink>
                </apex:column>

                <apex:column >
                    <apex:facet name="header">Quantity</apex:facet>
                    <apex:outputField value="{!val.Quantity}" />
                </apex:column>

                <apex:column >
                    <apex:facet name="header">Split Qty</apex:facet>
                    <apex:inputField value="{!val.Split_Qty__c}" />
                </apex:column>

                <apex:column >
                    <apex:facet name="header">Line Description</apex:facet>
                    <apex:inputField value="{!val.Description}" />
                </apex:column> 

            </apex:pageBlockTable>

        </apex:pageBlockSection>

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

Controller:

public with sharing class OLI_SplitQty2 {

public ApexPages.StandardController sc;
public Opportunity Opp {get; set;}
public List<OpportunityLineItem> OLIlist2 {get; set;}

public OLI_SplitQty2(ApexPages.StandardController sc) { 
    getOIDQueryParameter();
    OLIlist2 = [Select Product_Name__c, ID, Quantity, Split_Qty__c, Description, OpportunityId FROM OpportunityLineItem WHERE OpportunityId =:Opp.Id];

}

public Boolean getOIDQueryParameter(){
    Opp = new Opportunity(id=ApexPages.currentPage().getParameters().get('oid'));
    return null;
}

public List<OpportunityLineItem> getOLIs() {
    List<OpportunityLineItem> OLIlist2 = [Select Product_Name__c, ID, Quantity, Split_Qty__c, Description, OpportunityId FROM OpportunityLineItem WHERE OpportunityId =:Opp.Id];

    return OLIlist2;
}

public PageReference split() {
    PageReference returnPage;

    update(OLIlist2);
    returnPage = new ApexPages.StandardController(Opp).view();
    returnPage.setRedirect(true);
    return returnPage;
}

}

1 Answer 1

1

Ok, your issue was this being used as the iterator

public List<OpportunityLineItem> getOLIs() {
    List<OpportunityLineItem> OLIlist2 = [Select Product_Name__c, ID, Quantity, Split_Qty__c, Description, OpportunityId FROM OpportunityLineItem WHERE OpportunityId =:Opp.Id];

    return OLIlist2;
}

and then in the split() method you were saving the class property OLIlist2 which was not what was being edited on the page

Remove the above and change the VF to:

<apex:pageBlockTable value="{!OLIlist2}" var="val">

And it will work. I tested it in my org.

Description: In the getOLIs you are populating a local property OLIlist2 which is NOT available outside of that method.

There is no need to have that method there as you are already populating a list in the constructor. You could have also salvaged it by modifying the method to the following:

public List<OpportunityLineItem> getOLIs() {
    OLIlist2 = [Select Product_Name__c, ID, Quantity, Split_Qty__c, Description, OpportunityId FROM OpportunityLineItem WHERE OpportunityId =:Opp.Id];

    return OLIlist2;
}

Which would have assigned the query to the class property

4
  • That did not help. The records are still not updating. Are they somehow being requeried prior to the update?
    – Eric Smith
    Mar 18, 2017 at 14:16
  • @EricSmith - Updated my answer, thanks for confirming it was not the rerender issue
    – Eric
    Mar 18, 2017 at 14:29
  • Perfect - thank you. What's a good source for newbies to wrap their head around the whole object oriented Apex structure?
    – Eric Smith
    Mar 18, 2017 at 14:50
  • 1
    trailhead.salesforce.com
    – Eric
    Mar 18, 2017 at 14:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .