function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LoveLaceLoveLace 

Adding attachment files to the bulk inserting records

I have a wrapper class that add rows dynamically to my visualforce page. I'm trying add row that Contain also an Attachment Input File to insert mutiple files for each row (record) apparently it's not working!
My controller class:
public class AP005ManageListController
{
public List<ExpenseWrapper> wrappers {get; set;}
public List<AttachmentWrapper> attach{get; set;}
public static Integer toDelIdent {get; set;}
public static Integer addCount {get; set;}
private Integer nextIdent=0;
public AP005ManageListController()
{
  wrappers=new List<ExpenseWrapper>();
  attach= new List<AttachmentWrapper>();
  attach.add(new AttachmentWrapper());
   wrappers.add(new ExpenseWrapper(nextIdent++));
}
public void delWrapper()
{
  Integer toDelPos=-1;
  for (Integer idx=0; idx<wrappers.size(); idx++)
  {
   if (wrappers[idx].ident==toDelIdent)
   {
    toDelPos=idx;
   }
  }
  if (-1!=toDelPos)
  {
   wrappers.remove(toDelPos);
  }
}
public void addRows()
{
  for (Integer idx=0; idx<addCount; idx++)
  {
   wrappers.add(new ExpenseWrapper(nextIdent++));
  }
}
public PageReference save()
{
  List<Expense__c> accs=new List<Expense__c>();
  List<Attachment> att=new List<>(Attachment);
  for (ExpenseWrapper wrap : wrappers)
  {
  for(AttachmentWrapper attWrap :attach ){
  att.add(attWrap.exp);
  }
   accs.add(wrap.acc);
  }
  insert accs;
  return new PageReference('/' + Schema.getGlobalDescribe().get('Expense__c').getDescribe().getKeyPrefix() + '/o');
}
public class ExpenseWrapper
{
  public Expense__c exp{get; private set;}
  public Integer ident {get; private set;}
  
  public ExpenseWrapper(Integer inIdent)
  {
       ident=inIdent;
       exp=new Expense__c();
  }
}
public class AttachmentWrapper {
    public Expense__c exp {get;set;}
    public Attachment att {get;set;}
    public AttachmentWrapper(Expense__c exp, Attachment att){
        this.exp = exp;
        if(att!= null){
            this.att = att;
        } else {
            this.att = new Attachment(parentId = this.att.Id);
        }
    }
}
}
My VF page:

<apex:page controller="AP005ManageListController" tabstyle="Expenses__c">
<apex:stylesheet value="{!URLFOR($Resource.styles, 'RHstyles.css')}"/>
<apex:form >
   <apex:pageBlock title="Bulk Account Create">
    <apex:pageblockSection title="Traveling Information" >
      <apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
          
         <apex:column headerValue="Client">
            <apex:inputField value="{!wrapper.acc.Client__c}"/>
         </apex:column>
         <apex:column headerValue="Project">
            <apex:inputField value="{!wrapper.acc.Project__c}"/>
         </apex:column>
         <apex:column headerValue="Date">
            <apex:inputField value="{!wrapper.acc.Date__c}"/>
         </apex:column>
         <apex:column headerValue="Food/Meal Expenses">
            <apex:inputText value="{!wrapper.acc.Food_Expenses__c}"/>
         </apex:column>
        
         <apex:column headerValue="Phone Expenses">
            <apex:inputField value="{!wrapper.acc.Phone_Expenses__c}"/>
         </apex:column>
        <!-- <apex:column headerValue="File">
        <apex:inputFile value="{!wrapper.a.body}" filename="{!wrapper.a.name}"/>
        </apex:column>  -->
        </apex:pageblockSection>
         <apex:column headerValue="Action">
            <apex:commandButton value="Delete" action="{!delWrapper}" rerender="wtable">
               <apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/>
            </apex:commandButton>
         </apex:column>
         
      </apex:pageBlockTable>
      <apex:commandButton value="Add Row" action="{!addRows}" rerender="wtable">
         <apex:param name="addCount" value="1" assignTo="{!addCount}"/>
      </apex:commandButton>
     
      <apex:commandButton value="Save" action="{!save}"/>
     
   
   </apex:pageBlock>
</apex:form>
</apex:page>