+ Start a Discussion
Carter85Carter85 

Need help with dynamic list size not displaying correctly.

I'm working on creating a list to allow attachments to be assigned to different or multiple object records at once.  the list to add the attachements is supposed to populate spaces for files dynamicall based on user selection from a list.  I have the attachements assigning properly to their inteded records in my tests, however, the list size is not responding how I'd like.  It currently increments only one space at a time, rather than how many spaces the user selects and I'm having trouble getting it correct, so I'd appreciate any assistance.
My apex:
public with sharing class CM_MultipleAttach{

//Picklist of integer values to hold file count
    public List<SelectOption> filesCountList {get;set;}
    //Selected count
    public integer FileCount {get;set;}
    public string claimNum {get;set;}
    transient String contentType {get;set;}
transient String fileName {get;set;}
    transient Blob file {get;set;}
    public List<uploadWrapper> uploadList    {get;set;}
    public List<Attachment> allFileList {get;set;}


    public CM_MultipleAttach(ApexPages.StandardController controller){
        //Initialize
        filesCountList = new List<SelectOption>();
        FileCount = 0;
        allFileList = new List<Attachment>();
        uploadList = new List<uploadWrapper>();
        //Adding values count list - you can change this according to your need
        for(Integer i = 1; i < 3; i++){
            filesCountList.add(new SelectOption(''+i , ''+i));
         }
      }

    public class uploadWrapper{
        //the claim record(s)
        transient Attachment rClaim{get;set;}
        //Identify the claim and what necessary fields are filled in with for claim attach
        public string claimNum {get;set;}
        public Blob file {get;set;}
        public string fileName {get;set;}
        public String contentType {get; set;}
  /*
        *   Constructor initializes the reference
        */

    public uploadWrapper(List<Attachment> allFileList){
     }

     }

    public Pagereference SaveAttachments(){
        for(uploadWrapper cw : uploadList){
         if(cw.file == null){
             ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'Some claim numbers do not have associated files. Please select files for those claims.'));
          return null;
          }
            if(cw.file != null){
                claimNum = cw.claimNum;
                file = cw.file;
                MG_MAG_Claim__c attInv = [SELECT ID FROM MG_MAG_Claim__c WHERE Name =:claimNum];
                Attachment attach = new Attachment();
         attach.Body = cw.file;
         attach.Name = cw.filename;
         attach.ContentType = cw.contentType;
         attach.ParentID = attInv.ID;
      try{
          insert(attach);
         }
         catch(System.DMLException e){
          ApexPages.addMessages(e);
          return null;
         }
                }
                }
                return page.CM_BulkUpload3;
       }

    public PageReference ChangeCount(){
        allFileList.clear();
        //Adding multiple attachments
        for(Integer i = 0 ; i <= FileCount ; i++)
            allFileList.add(new Attachment());
         uploadList.add(new uploadWrapper(allFileList));
        if(FileCount > 0){
          return page.CM_BulkUpload2;
       }
      else{
       return null;
       }
       }

public PageReference pgBack(){
        allFileList.clear();
        FileCount = 0;
        return page.CM_BulkUpload;
     }
}

The Visualforce form meant to increase/decrease in size based on selection:
<apex:pageBlockSection title="Select Files" rendered="{!IF(FileCount != null && FileCount != 0, true , false)}">
                <apex:pageBlockTable value="{!uploadList}" style="border:1px solid black;" var="a" rendered="{!IF(FileCount != null && FileCount != 0, true , false)}">
             <apex:column headervalue="Claim Number (ex: CM-XXXXXXX)" style="border:1px solid black; width:115px; align:center">
             <apex:inputText value="{!a.claimNum}" style="border:1px solid black; width:115px; align:center"/>
             </apex:column>
             <apex:column headervalue="Invoice" style="border:1px solid black; width:50px; align:center">
             <apex:inputfile value="{!a.file}" filename="{!a.fileName}" onchange="check(this)" filesize="1000" size="50"/>
             </apex:column>
             </apex:pageBlockTable>
             </apex:pageBlockSection>
Eli Flores, SFDC DevEli Flores, SFDC Dev
without seeing the rest of the page, i think what you need  is to a the rerender attribute on the section and then make sure your submit action triggers the rerender. 

There's  a simple example to this approach in the docs:
http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#CSHID=pages_compref_actionFunction.htm|StartTopic=Content%2Fpages_compref_actionFunction.htm|SkinName=webhelp
Carter85Carter85
I made a mistake and didn't explain properly.  The form i listed is actually meant to be the result of a user selection on a previous page which, once selected, loads the new page which should then render the table as intended.  So, for clarification, this is the first form which the user selects how many attachments they want to upload:

<apex:form >
        <apex:pageBlock title="Upload Multiple Claim Invoices">

            <apex:actionFunction name="ChangeCount" action="{!ChangeCount}"/>

            <apex:pageblocksection >

                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="How many files do you want to upload?"/>
                    <apex:selectList onChange="ChangeCount();" multiselect="false" size="1" value="{!FileCount}">
                        <apex:selectOption itemLabel="--None--" itemValue="0"/>
                        <apex:selectOptions value="{!filesCountList}"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>

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

And the apex is supposed to take the input and generate the proper sized list on this page:

<apex:form >
         <apex:pageBlock >  
   <script type = "text/javascript">
   function check(obj) {
   var path = obj.value;
   var ext = path.substring(path.lastIndexOf('.') + 1);
   if(obj.size > 10240){
    obj.value = null;
    window.alert("File size can not exceed 10MB.");
    return false;
    }
   var res_field = obj.value;  
     var extension = res_field.substr(res_field.lastIndexOf('.') + 1).toLowerCase();
     var allowedExtensions = ['jpg', 'jpeg', 'tif', 'pdf', 'png', 'gif'];
     if (res_field.length > 0){
           if (allowedExtensions.indexOf(extension) === -1){
               alert('Invalid file Format. Only ' + allowedExtensions.join(', ') + ' are allowed.');
               obj.value = null;
               return false;
              }
       }
    }
    </script>
            <apex:pageBlockSection title="Select Files" rendered="{!IF(FileCount != null && FileCount != 0, true , false)}">
                <apex:pageBlockTable value="{!uploadList}" style="border:1px solid black;" var="a" rendered="{!IF(FileCount != null && FileCount != 0, true , false)}">
             <apex:column headervalue="Claim Number (ex: CM-XXXXXXX)" style="border:1px solid black; width:115px; align:center">
             <apex:inputText value="{!a.claimNum}" style="border:1px solid black; width:115px; align:center"/>
             </apex:column>
             <apex:column headervalue="Invoice" style="border:1px solid black; width:50px; align:center">
             <apex:inputfile value="{!a.file}" filename="{!a.fileName}" onchange="check(this)" filesize="1000" size="50"/>
             </apex:column>
             </apex:pageBlockTable>
             </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Back" action="{!pgBack}"/>
                <apex:commandButton value="Upload" action="{!SaveAttachments}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>

However, I have to keep switching back and forth between the two pages just to get the list to increment one by one rather than having it render the way it is meant to upon simple user selection.  So if this highlights the problem better and makes it easier for anyone to see the solution I would still appreciate any help.