0

I have a map with String value and list of attachments. I am getting those by inputfile but not being able to insert those. Please find my snippet as below. Can someone please help?

Apex page:

.bPageBlock .detailList .dataCol { width: 0%; } .pbtitle {background-color: #e3f3ff ; color:Black; background-image:none;width: 100%;font-weight: bold; font-size: 11px; border: 1px solid #999; }

<apex:form >
    <apex:actionFunction name="addRow" action="{!addRow}" rerender="container">
        <!--<apex:actionSupport rerender="container"/>-->

        <apex:param id="key" name="keyValue" assignTo="{!keyValue}" value=""/>

    </apex:actionFunction>
</apex:form>
<apex:form >  
  <apex:outputPanel styleClass="pbtitle" layout="block" id="container">
    <apex:pageBlock title="Call Attachments" >  

        <apex:pageBlockButtons location="bottom" >  
            <apex:commandButton value="Upload"  action="{!SaveAttachments}"/>  
        </apex:pageBlockButtons>  

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

        <apex:pageBlockSection title="Written Inquiry/Referral" collapsible="1" columns="1">

          <apex:actionRegion >
            <apex:outputPanel styleClass="btn" onclick="addRow('Written Inquiry/Referral')">
                <apex:outputLabel >Add</apex:outputLabel>
            </apex:outputPanel>
          </apex:actionRegion>
            <apex:repeat value="{!attachmentMap['Written Inquiry/Referral']}" var="uploadFile1">
                <apex:inputFile value="{!uploadFile1.body}" fileName="{!uploadFile1.name}" />
            </apex:repeat>
        </apex:pageBlockSection>

        <apex:pageBlockSection title="Written Response"  collapsible="1" columns="1">

          <apex:actionRegion >
            <apex:outputPanel styleClass="btn" onclick="addRow('Written Response')">
                <apex:outputLabel >Add</apex:outputLabel>
            </apex:outputPanel>
          </apex:actionRegion>
            <apex:repeat value="{!attachmentMap['Written Response']}" var="uploadFile2">
                <apex:inputFile value="{!uploadFile2.body}" fileName="{!uploadFile2.name}" />
            </apex:repeat>
        </apex:pageBlockSection>

        <apex:pageBlockSection title="Written Response Approval"  collapsible="1" columns="1">

          <apex:actionRegion >
            <apex:outputPanel styleClass="btn" onclick="addRow('Written Response Approval')">
                <apex:outputLabel >Add</apex:outputLabel>
            </apex:outputPanel>
          </apex:actionRegion>
            <apex:repeat value="{!attachmentMap['Written Response Approval']}" var="myfile1">
                <apex:inputFile value="{!myfile1.body}" fileName="{!myfile1.name}" />
            </apex:repeat>
        </apex:pageBlockSection>

        <apex:pageBlockSection title="1:1 Speaker Training Approval"  collapsible="1" columns="1">

          <apex:actionRegion >
            <apex:outputPanel styleClass="btn" onclick="addRow('1:1 Speaker Training Approval')">
                <apex:outputLabel >Add</apex:outputLabel>
            </apex:outputPanel>
          </apex:actionRegion>
            <apex:repeat value="{!attachmentMap['1:1 Speaker Training Approval']}" var="uploadFile4">
                <apex:inputFile value="{!uploadFile4.body}" fileName="{!uploadFile4.name}" />
            </apex:repeat>
        </apex:pageBlockSection>



    </apex:pageBlock>  
    <apex:pageblock >
    <apex:pageblockSection id="pbs7" title="Call Attachments Uploaded" columns="1" collapsible="false" >

        <apex:outputPanel id="uploadSection">
        <table border="0">
        <tr><th> Category </th>
        <th> File Name </th>

        </tr>
        <apex:repeat value="{!All}" var="att">
            <tr>
            <td>{!att.Description}</td>
            <td>
            <a href ="/servlet/servlet.FileDownload?file={!att.id}" >{!att.name}</a >
            </td>
            </tr>
        </apex:repeat>
        </table>
    </apex:outputPanel>

        <div class="FilebuttonGroup">


           </div>   

        </apex:pageblockSection>
        </apex:pageblock>
    </apex:outputPanel>
</apex:form>  

class:

public with sharing class MultipleUploadController  

{
public Map> attachmentMap {get; set;} public String keyValue {get; set;} public String val {get; set;} public Attachment myfile; Public Attachment getmyfile() { myfile = new Attachment(); return myfile; } Public String callId='a04L0000003SSYw'; //Picklist of tnteger values to hold file count
public List filesCountList {get; set;}
//Selected count
public String FileCount {get; set;} // public String Inquiry{get; set;} public Integer Counting =0; //public Attachment attachment {get;set;} public Integer getCounting(){ return Counting; }

List<Attachment> WrittenInqList = new List<Attachment>();
List<Attachment> WrittenResponse = new List<Attachment>();
List<Attachment> WrittenResponseAppr =new List<Attachment>();
List<Attachment> SpeakerTrainingAppr =new List<Attachment>();

public List<Attachment> allFileList {get; set;}  


public MultipleUploadController(ApexPages.StandardController controller)  
{  
//Map Attachment

 attachmentMap = new Map<String, List<Attachment>>();
    attachmentMap.put('Written Inquiry/Referral', WrittenInqList );
    attachmentMap.put('Written Response', WrittenResponse );
    attachmentMap.put('Written Response Approval', WrittenResponseAppr );
    attachmentMap.put('1:1 Speaker Training Approval', SpeakerTrainingAppr );
    //Initialize    
    filesCountList = new List<SelectOption>() ;  
    Counting=0;//FileCount = '' ;  
    allFileList = new List<Attachment>() ;  

    //Adding values count list - you can change this according to your need  
    for(Integer i = 1 ; i < 6 ; i++)  
        filesCountList.add(new SelectOption(''+i , ''+i)) ;  
}  

public list<Attachment> getAll()
  {
  list<Attachment> alist= [select id,Name,Description from attachment where parentid=:callId];

  //All attach for the call
  return alist;

  }

public Pagereference SaveAttachments()  
{  
    //remove later
    //String callId = System.currentPagereference().getParameters().get('id');  
    if(callId == null || callId == '')  
        ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'No record is associated. Please pass record Id in parameter.'));  
    if(Counting== null )  
        ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'Please select how many files you want to upload.'));  

    List<Attachment> listToInsert = new List<Attachment>() ;  

    //Attachment a = new Attachment(parentId = callId, name=myfile.name, body = myfile.body);  
    system.debug('MAP:  '+attachmentMap.keyset());

    for(String a1: attachmentMap.keyset())  
    {  
        system.debug('List:  '+attachmentmap.get(a1));
      for(Attachment a: attachmentmap.get(a1))
        {listToInsert.add(new Attachment(parentId = callId, Description = a1,name = a.name, body = a.body)) ;  
            system.debug('listToInsert:  '+listToInsert);
        }
    }  

    //Inserting attachments  
    if(attachmentMap.get(keyvalue).size() > 0)  
    {  
       try
       {
        insert listToInsert ;  

        ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.INFO, listToInsert.size() + ' file(s) are uploaded successfully'));  
        Counting= 0 ;  
        }
        catch(Exception e1)
        {
         ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,e1.getMessage()));

         }
    }  
    else  
        ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'Please select at-least one file'));  
    //allFileList=null;

    return null;  
}  



public PageReference ChangeCount()  
{  
    allFileList = new List<Attachment>();
    //InquiryallFileList = new List<Attachment>();  
    Counting=Counting+1;
    //Inquiry='Yes';  //
    //Adding multiple attachments instance  
    for(Integer i = 1 ; i <= Counting; i++)  //Integer.valueOf(FileCount) 
        allFileList.add(new Attachment()) ;  
    return null ;  
}  

public Pagereference addRow()
{
    List<Attachment> TempList;
    Attachment a = new Attachment();
    a.Description= keyValue;
    System.debug('----> '+keyValue);

    TempList = attachmentMap.get(keyValue);
    TempList .add(a);
    system.debug('listToInsert:  '+TempList );
    return null;
}

} The list name listtoinsert size is always zero here.

6
  • Where are you getting attachmentMap from? Is its size zero too?
    – metadaddy
    Commented Apr 2, 2015 at 5:15
  • Please find my whole updated class. system.debug('MAP: '+attachmentMap.keyset()); prints this: MAP: {1, 2, 3, 4} Commented Apr 2, 2015 at 5:17
  • Call on AddRow from the apex page sets the attachmentmap Commented Apr 2, 2015 at 5:18
  • 1
    In addRow, you're not setting a name or body on the Attachment, so the test in SaveAttachments will always fail.
    – metadaddy
    Commented Apr 2, 2015 at 5:29
  • Can you please let me know how can I capture the input file from page? Commented Apr 2, 2015 at 5:32

1 Answer 1

0

Jeff Douglas has a blog entry on this. Here are the basics:

Visualforce Page

<apex:page controller="AttachmentUploadController">  
  <apex:sectionHeader title="Visualforce Example" subtitle="Attachment Upload Example"/>

  <apex:form enctype="multipart/form-data">
    <apex:pageMessages />
    <apex:pageBlock title="Upload a Attachment">

      <apex:pageBlockButtons >
        <apex:commandButton action="{!upload}" value="Save"/>
      </apex:pageBlockButtons>

      <apex:pageBlockSection showHeader="false" columns="2" id="block1">

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File Name" for="fileName"/>
          <apex:inputText value="{!attachment.name}" id="fileName"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File" for="file"/>
          <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="Description" for="description"/>
          <apex:inputTextarea value="{!attachment.description}" id="description"/>
        </apex:pageBlockSectionItem>

      </apex:pageBlockSection>

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

Controller

public with sharing class AttachmentUploadController {

  public Attachment attachment {
  get {
      if (attachment == null)
        attachment = new Attachment();
      return attachment;
    }
  set;
  }

  public PageReference upload() {

    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = '0037000000lFxcw'; // the record the file is attached to
    attachment.IsPrivate = true;

    try {
      insert attachment;
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    } finally {
      attachment = new Attachment(); 
    }

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;
  }

}
1
  • Yes I have seen that . The issue with my code is that it is not able to put the List values in map in a separate list from where insert can be done. The map is already having exact data but I am not able to do insert Attachment from there. Please help me here! Commented Apr 3, 2015 at 5:40

You must log in to answer this question.

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