1

Im having trouble getting a list from my wrapper class to show up in a page block table after clicking a command button. Can someone tell why my wrapper class is not showing up in the table? Is the rerender even required? Am I doing something else wrong?

FYI I tried to use "rerender" in the action button but it is not playing well with the inputfile component. When i added an action region, the variables required for the action button return null.

Controller

public class mcsReturnProcessorController {
 public Blob csvFileBody{get;set;}
 public string csvAsString{get;set;}
 public String[] csvFileLines{get;set;}
 public List<Asset> assetlist{get;set;}
 public List<wrapAsset> wrapAssets{get; set;}

  public mcsReturnProcessorController(){
    csvFileLines = new String[]{};
    assetlist = New List<Asset>();
  }

public void importCSVFile(){
  List<wrapAsset> wrapAssets = new List<wrapAsset>();
   try{
       system.debug(csvFileBody);
       system.debug(csvAsString);
       csvAsString = csvFileBody.toString();
       csvFileLines = csvAsString.split('\n');
       for(Integer i=1;i<csvFileLines.size();i++){
           string[] csvRecordData = csvFileLines[i].split(',');
           wrapAssets.add(new wrapAsset(csvRecordData[0],csvRecordData[1],csvRecordData[2]));
       }
    system.debug('wrapAssets.size= ' + wrapAssets.size());
    system.debug(wrapAssets[0].IMEI);
    }
    catch (Exception e)
    {
        System.debug(e.getMessage());
        ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importing data Please make sure input csv file is correct');
        ApexPages.addMessage(errorMessage);
    }  

}

public class wrapAsset {
    public String IMEI {get; set;}
    public String returnTracking {get; set;}
    public boolean detachSims {get; set;}

    public wrapAsset(String imeip, String returnTrackingp, String detachSimsp){
        this.IMEI = imeip;
        this.returnTracking = returnTrackingp;
        If (detachSimsp == '0'){
            this.detachSims = false;
        } else {
           this.detachSims = true; 
        }
    }
}   

}

VF Page

<apex:page controller="mcsReturnProcessorController" sidebar="false">
<script type="text/javascript">
/* function selectAllCheckboxes(obj,receivedInputID){
        var inputCheckBox = document.getElementsByTagName("input");                  
        for(var i=0; i<inputCheckBox.length; i++){          
            if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){                                     
                inputCheckBox[i].checked = obj.checked;
            }
        }
    } */
</script>
<apex:form>
    <apex:pagemessages />
    <apex:pageBlock>
        <apex:pageBlockSection title="Returns Processor" columns="3">
            <apex:inputFile value="{!csvFileBody}"  filename="{!csvAsString}"/>
            <apex:commandButton value="Import Returns List" action="{!importCSVFile}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection>
            <apex:pageBlockTable value="{!wrapAssets}" var="w" id="table">
                <apex:column>
                    <apex:facet name="header">IMEI</apex:facet>
                    <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                    <apex:inputCheckbox value="{!w.detachSims}"/>
                </apex:column>
                <apex:column value="{!w.IMEI}"><apex:facet name="header">Return Tracking</apex:facet></apex:column>
                <apex:column value="{!w.returnTracking}"><apex:facet name="header">Detach Sims?</apex:facet></apex:column>
            </apex:pageBlockTable>
            <apex:outputText value="{!wrapAssets[0].IMEI}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

1 Answer 1

1

This is a very common mistake (as I've done it myself)

Your controller declares a variable

public List<wrapAsset> wrapAssets{get; set;}

But your action method redeclares it!

public void importCSVFile(){
  List<wrapAsset> wrapAssets = new List<wrapAsset>();
  ...

So, you are adding to a locally scoped collection and not the collection bound to the VF page

Remove the second declaration and all should be fine.

Aside

While not all devs do this, I tend to refer to controller object variables with a prefix of this. to make sure I'm using the one I want for the VF binding

2
  • That worked! Thank you! I'm new to the site so if I did not give you credit appropriately please let me know.
    – Matt P
    Commented Oct 14, 2019 at 14:11
  • @MattP - happy! and yes, you gave me the green check mark which is the way things are done.
    – cropredy
    Commented Oct 14, 2019 at 17:50

You must log in to answer this question.

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