1

I have created a SelectList inside of a PageBlock Table and when I add a new row to the table and use the select list it will hold the same value in all of the lists instead of just for that one row. Does Anyone have any thoughts on this? Below are pictures and code.

Output of Table Showing Same Values for Both Rows

As you can see, both rows are set to "Red Pepper Jelly" even though I only selected that within the first row.

Here is my VF Page Code:

<apex:page controller="CommunityQuoteNowController" showHeader="true" sidebar="false" lightningStylesheets="true">

<apex:outputPanel id="messagePanel">
    <apex:pageMessages />
</apex:outputPanel>

<!-- Quote Now Form -->
<apex:form id="quoteNowForm" >
    <apex:pageBlock title="Quote Now">
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton action="{!GetRates}" value="Get Rates"/>
            <apex:commandButton action="{!clearPage}" value="Clear" immediate="true" html-formnovalidate="formnovalidate" onclick="if(!confirm('Are you sure you would like to clear this page?')){return};" />
        </apex:pageBlockButtons>
        <apex:pageBlockSection id="CustomerProfile" columns="2" >
            <apex:inputField label="Customer Profile" value="{!load.rtms__Customer__c}" />
            <apex:selectList value="{!selectedMode}" title="Select" multiselect="false" size="1" id="selectModes" label="Mode">
                <apex:selectOptions value="{!ModesList}" />
            </apex:selectList>
        </apex:pageBlockSection>
        <apex:pageBlockSection title="Details" columns="2">
            <apex:inputField label="Ship Date" value="{!load.rtms__Order_Date__c}" />
            <apex:inputField label="Payment Terms" value="{!load.rtms__Payment_Terms__c}" />
            <apex:inputField label="Origin Zip Code" value="{!originStop.rtms__Postal_Code__c}" />
            <apex:inputField label="Destination Zip Code" value="{!destinationStop.rtms__Postal_Code__c}" />
        </apex:pageBlockSection>
        <hr />
        <apex:pageBlockSection id="FreightDetails" columns="1">
            <apex:outputLabel for="freightDetails" value="Freight Details"/>
            <apex:actionRegion immediate="true">
                <apex:selectList value="{!selectedFreightDetail}" title="freightDetails" multiselect="false" size="1" id="selectFreightDetails" label="Freight Details" >
                    <apex:selectOptions value="{!FreightDetails}" />
                    <apex:actionSupport event="onchange" reRender="FreightDetailsWrapper" />
                </apex:selectList>
            </apex:actionRegion>
        </apex:pageBlockSection>
        <apex:outputPanel layout="none" id="FreightDetailsWrapper" >
            <apex:pageBlockSection columns="2" id="ClassAndWeight" rendered="{!IF(selectedFreightDetail = 'Class_and_Weight',True,False)}">
                <apex:variable value="{!0}" var="index" />
                <apex:pageBlockTable value="{!lineItems}" var="items" >
                    <apex:column headerValue="Row #">
                        <apex:outputText>
                            <apex:variable value="{!index + 1}" var="index" />
                            {!index}
                        </apex:outputText>
                    </apex:column>
                    <apex:column headerValue="Select Commodity">
                        <apex:actionRegion>
                            <apex:selectList value="{!selectedCommodity}" title="Select" multiselect="false" size="1" id="selectCommodities" label="Commodity">
                                <apex:selectOptions value="{!Commodities}" />
                                <apex:actionSupport event="onchange" reRender="FreightDetailsWrapper" action="{!commoditySelected}" />
                            </apex:selectList>
                        </apex:actionRegion>
                    </apex:column>
                    <apex:column headerValue="NMFC Class" >
                        <apex:inputField label="NMFC Class" value="{!items.rtms__NMFC_Class__c}"/>
                    </apex:column>
                    <apex:column headerValue="Weight" >
                        <apex:inputField label="Weight" value="{!items.rtms__Weight__c}"/>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            <apex:pageBlockSection columns="4" id="Density"  rendered="{!IF(selectedFreightDetail = 'Density',True,False)}">
                <apex:repeat value="{!lineItems}" var="items">
                    <apex:inputField label="Length (in)" value="{!items.rtms__Length__c}"/>
                    <apex:inputField label="Width (in)" value="{!items.rtms__Width__c}"/>
                    <apex:inputField label="Height (in)" value="{!items.rtms__Height__c}"/>
                    <apex:inputField label="Weight (lbs)" value="{!items.rtms__Weight__c}"/>
                </apex:repeat>
            </apex:pageBlockSection>
            <apex:pageBlockSection columns="3" id="Pallet" rendered="{!IF(selectedFreightDetail = 'Pallet',True,False)}">
                <apex:repeat value="{!lineItems}" var="items">
                    <apex:inputField label="Handling Units" value="{!items.rtms__Handling_Units__c}"/>
                    <apex:inputField label="Handling Unit Count" value="{!items.rtms__Handling_Unit_Count__c}"/>
                    <apex:inputField label="Weight (lbs)" value="{!items.rtms__Weight__c}"/>
                </apex:repeat>
            </apex:pageBlockSection>
            <apex:commandButton value="Add Line Item" Action="{!AddRow}" immediate="true" id="addRowButton"/>
        </apex:outputPanel>
    </apex:pageBlock>
</apex:form></apex:page>

And here is my Controller:

public class CommunityQuoteNowController {

// Variables
public rtms__Load__c load { get; set; }
public rtms__Stop__c originStop { get; set; }
public rtms__Stop__c destinationStop { get; set; }
public List<rtms__Load__c> loadList { get; set; }
public List<rtms__LineItem__c> lineItems { get; set; }
public List<rtms__LineItem__c> deleteLineItems { get; set; }
public String selectedMode { get; set; }
public List<SelectOption> availableModes { get; set; }
public String selectedFreightDetail { get; set; }
public List<SelectOption> options { get; set; }
public String selectedCommodity { get; set; }
public List<SelectOption> availableCommodities { get; set; }


public rtms__Load__c getLoad() {
    return load;
}

public CommunityQuoteNowController() {
    load = new rtms__Load__c();
    originStop = new rtms__Stop__c();
    destinationStop = new rtms__Stop__c();
    lineItems = new List<rtms__LineItem__c>();        
    AddRow();
    selectedFreightDetail = 'Class_and_Weight';
}

public PageReference AddRow() {
    lineItems.add(new rtms__LineItem__c());
    return null;
}

public PageReference commoditySelected() {
    lineItems[0].rtms__NMFC_Class__c = selectedCommodity;
    return null;
}

public List<selectOption> getModesList() {
    List<rtms__Mode__c> modes = new List<rtms__Mode__c>();

    modes = [Select Id, Name from rtms__Mode__c];

    List<SelectOption> availableModes = new List<SelectOption>();

    availableModes.add(new SelectOption('','--None--'));

    for(rtms__Mode__c m : modes) {
        availableModes.add(new SelectOption(m.Id,m.Name));
    }
    return availableModes;
}

public List<selectOption> getCommodities() {
    List<rtms__Commodity__c> commodities = new List<rtms__Commodity__c>();

    commodities = [Select Id, Name, rtms__NMFC_Class__c, rtms__Description__c from rtms__Commodity__c WHERE Id = 'a1B02000000egveEAA'];

    List<SelectOption> availableCommodities = new List<SelectOption>();

    availableCommodities.add(new SelectOption('','--None--'));

    for(rtms__Commodity__c c : commodities) {
        availableCommodities.add(new SelectOption(c.rtms__NMFC_Class__c,c.rtms__Description__c));
        system.debug(availableCommodities);
    }
    return availableCommodities;
}

public List<selectOption> getFreightDetails() {
    List<SelectOption> options = new List<SelectOption>();

    options.add(new SelectOption('Class_and_Weight','Class and Weight'));
    options.add(new SelectOption('Density','Density'));
    options.add(new SelectOption('Pallet','Pallet'));

    return options;
}

/* Attempt at displaying a total weight for line items
public Decimal getTotalWeight(){
    Decimal totalWeight = 0;
    for(rtms__LineItem__c li : lineItems) {
        totalWeight += (li.rtms__Weight__c);
    }
    return totalWeight;
} */

public PageReference clearPage() {
    PageReference newpage = new PageReference(System.currentPageReference().getURL());
    newPage.getParameters().clear();
    newpage.setRedirect(true);
    return newpage;
}

public PageReference GetRates() {
    load.rtms__Mode__c = selectedMode;
    load.rtms__Total_Weight__c = 1;
    upsert load;
    
    if(load != null) {
        originStop.rtms__Load__c = load.Id;
        originStop.rtms__Number__c = 1;
        originStop.rtms__Is_Pickup__c = True;
        upsert originStop;
    }
    
    if(load != null) {
        destinationStop.rtms__Load__c = load.Id;
        destinationStop.rtms__Number__c = 2;
        destinationStop.rtms__Is_Dropoff__c = True;
        upsert destinationStop;
    }
    
    List<rtms__LineItem__c> items = new List<rtms__LineItem__c>();
    if(load != null) {
        for(rtms__LineItem__c i : lineItems) {
            i.rtms__Load__c = load.Id;
            i.Name = 'Line Item From Quote';
            i.rtms__Item_Description__c = 'From Quote';
            i.rtms__Pickup_Stop__c = originStop.Id;
            i.rtms__Delivery_Stop__c = destinationStop.Id;
            items.add(i);
        }
    }
    if(items != null) { 
        upsert lineItems;
   }
   return null;
}}

Any help would be greatly appreciated.

Best, Chad

1 Answer 1

4

The issue you are having is here:

<apex:pageBlockTable value="{!lineItems}" var="items" >
      ...
      <apex:column headerValue="Select Commodity">
         <apex:actionRegion>
           <apex:selectList value="{!selectedCommodity}" title="Select" multiselect="false" size="1" id="selectCommodities" label="Commodity">
             <apex:selectOptions value="{!Commodities}" />
                <apex:actionSupport event="onchange" reRender="FreightDetailsWrapper" action="{!commoditySelected}" />
           </apex:selectList>
         </apex:actionRegion>
      </apex:column>
  ...
</apex:pageBlockTable>

You are iterating over {!lineItems} yet binding the selected Commodity to a top-level property called selectedCommodity defined as:

public String selectedCommodity { get; set; }

Thus, when you refresh the page, that property is picked up by every row.

What you want is for each row's selectedCommodity to come from either:

Option 1:

A field on the iterated collection {!lineItems} - it looks like you could use rtms__NMFC_Class__c for this.

Or ...

Option2

Use a wrapper class that wraps each lineItem and the selected Commodity. Then your pageBlockTable iterates over a collection of Wrapper class objects. You can find many examples of Wrapper classes on Stackexchange or via a search engine

You must log in to answer this question.

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