Salesforce : Uploading Multiple Attachments
***********************
Visualforce Code :
***********************
<apex:page standardController="Account" extensions="MultipleAttchments">
<apex:form >
<apex:pageBlock title="Upload Multiple Attachments to Object">
<apex:pageBlockButtons >
<apex:commandButton value="upload" action="{!saveAttachments}"/>
</apex:pageBlockButtons>
<apex:pageMessages />
<apex:pageBlocksection >
<apex:pageBlockSectionItem >
<apex:outputLabel value="Select Attachments you want to upload"/>
<apex:selectList value="{!selectCount}" size="1" multiselect="false" onchange="changeCount()">
<apex:selectOption itemLabel="--None--" itemValue=""/>
<apex:selectOptions value="{!selectCountList}"/>
</apex:selectList>
</apex:pageBlockSectionItem>
</apex:pageBlocksection>
<!-- Section will appear when no. of attachments selected from drop down -->
<apex:pageBlockSection title="Select Files" rendered="{!IF(selectCount!=null && selectCount != '', true, false)}">
<apex:repeat value="{!allAttachments}" var="ATCH">
<Apex:inputFile value="{!ATCH.body}" filename="{!ATCH.Name}"/>
</apex:repeat>
</apex:pageBlockSection>
<apex:actionFunction name="changeCount" action="{!changeCount}"/>
</apex:pageBlock>
</apex:form>
</apex:page>
*********************************************************************************
***************************
Controller
***************************
public class MultipleAttchments {
public list<SelectOption> selectCountList {get;set;}
public String selectCount {get;set;}
public list<Attachment> allAttachments {get; set;}
public MultipleAttchments(ApexPages.StandardController controller) {
selectCount = '';
selectCountList = new list<SelectOption>();
allAttachments = new list<Attachment>();
for(integer i = 1; i < 11; i ++) {
selectCountList.add(new SelectOption(''+i, ''+i));
}
}
public PageReference saveAttachments() {
String accId = system.CurrentPagereference().getParameters().get('id');
if(accId == null || accId == '') {
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR,'No Record is associated, Please pass the record Id in Parameter'));
return null;
}
if(selectCount == null && selectCount == '') {
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR,'Please select how many files you want to attach'));
return null;
}
list<Attachment> insertList = new list<Attachment>();
for(Attachment attach : allAttachments) {
if(attach.name != '' && attach.body != null && attach.name != null) {
insertList.add(new Attachment(parentId = accId, name = attach.name, body = attach.body));
}
}
if(insertList.size() > 0 ) {
insert insertList;
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO,'Attachment inserted successfully'));
} else {
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.WARNING,'Please select atleast one file to attach'));
}
return null;
}
/* Method will be called when number of attachments change on page */
public PageReference changeCount() {
allAttachments.clear();
system.debug('==========='+ selectCount);
for(integer i = 0; i < integer.valueOf(selectCount); i++) {
allAttachments.add(new Attachment());
}
return null;
}
}
No comments:
Post a Comment