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.
attachmentMap
from? Is its size zero too?addRow
, you're not setting a name or body on the Attachment, so the test inSaveAttachments
will always fail.