Wednesday, 29 October 2014

Uploading Multiple Attachments into Salesforce

Okay, so here is the simple code to upload multiple attachments into Salesforce. Please note that I've used standard controller ("Account" in the code) so you need to pass the record Id in parameter. You can also change this according to your need as code is dynamic.

So first option allow you to select how many files you want to upload. Say, you have selected "5", then it will give you 5 options to select files and once you click on "Upload" it will upload the files as attachments to the associated record.



Visualforce Page Code:


  1. <apex:page standardController="Account" extensions="MultipleUploadController">  
  2.     <apex:form>  
  3.         <apex:pageBlock title="Upload Multiple Attachment to Object">  
  4.               
  5.             <apex:pageBlockButtons>  
  6.                 <apex:commandButton value="Upload"  action="{!SaveAttachments}"/>  
  7.             </apex:pageBlockButtons>  
  8.               
  9.             <apex:pageMessages id="MSG"/>  
  10.             <apex:actionFunction name="ChangeCount" action="{!ChangeCount}"/>  
  11.               
  12.             <apex:pageblocksection>  
  13.                               
  14.                 <apex:pageBlockSectionItem>  
  15.                     <apex:outputLabel value="How many files you want to upload?"/>  
  16.                     <apex:selectList onchange="ChangeCount() ;" multiselect="false" size="1" value="{!FileCount}">  
  17.                         <apex:selectOption itemLabel="--None--" itemValue=""/>  
  18.                         <apex:selectOptions value="{!filesCountList}"/>  
  19.                     </apex:selectList>  
  20.                 </apex:pageBlockSectionItem>  
  21.               
  22.             </apex:pageblocksection>  
  23.               
  24.             <apex:pageBlockSection title="Select Files" rendered="{!IF(FileCount != null && FileCount != '', true , false)}">  
  25.                 <apex:repeat value="{!allFileList}" var="AFL">  
  26.                     <apex:inputfile value="{!AFL.body}" filename="{!AFL.Name}" />  
  27.                       
  28.   
  29.                 </apex:repeat>  
  30.             </apex:pageBlockSection>  
  31.               
  32.         </apex:pageBlock>  
  33.     </apex:form>  
  34. </apex:page>  


Apex Code:


  1. public class MultipleUploadController  
  2. {     
  3.     //Picklist of tnteger values to hold file count  
  4.     public List<SelectOption> filesCountList {get; set;}  
  5.     //Selected count  
  6.     public String FileCount {get; set;}  
  7.       
  8.     public List<Attachment> allFileList {get; set;}  
  9.       
  10.     public MultipleUploadController(ApexPages.StandardController controller)  
  11.     {  
  12.         //Initialize    
  13.         filesCountList = new List<SelectOption>() ;  
  14.         FileCount = '' ;  
  15.         allFileList = new List<Attachment>() ;  
  16.           
  17.         //Adding values count list - you can change this according to your need  
  18.         for(Integer i = 1 ; i < 11 ; i++)  
  19.             filesCountList.add(new SelectOption(''+i , ''+i)) ;  
  20.     }  
  21.       
  22.     public Pagereference SaveAttachments()  
  23.     {  
  24.         String accId = System.currentPagereference().getParameters().get('id');  
  25.         if(accId == null || accId == '')  
  26.             ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'No record is associated. Please pass record Id in parameter.'));  
  27.         if(FileCount == null || FileCount == '')  
  28.             ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'Please select how many files you want to upload.'));  
  29.   
  30.         List<Attachment> listToInsert = new List<Attachment>() ;  
  31.           
  32.         //Attachment a = new Attachment(parentId = accid, name=myfile.name, body = myfile.body);  
  33.         for(Attachment a: allFileList)  
  34.         {  
  35.             if(a.name != '' && a.name != '' && a.body != null)  
  36.                 listToInsert.add(new Attachment(parentId = accId, name = a.name, body = a.body)) ;  
  37.         }  
  38.           
  39.         //Inserting attachments  
  40.         if(listToInsert.size() > 0)  
  41.         {  
  42.             insert listToInsert ;  
  43.             ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.INFO, listToInsert.size() + ' file(s) are uploaded successfully'));  
  44.             FileCount = '' ;  
  45.         }  
  46.         else  
  47.             ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'Please select at-least one file'));  
  48.               
  49.         return null;  
  50.     }  
  51.       
  52.     public PageReference ChangeCount()  
  53.     {  
  54.         allFileList.clear() ;  
  55.         //Adding multiple attachments instance  
  56.         for(Integer i = 1 ; i <= Integer.valueOf(FileCount) ; i++)  
  57.             allFileList.add(new Attachment()) ;  
  58.         return null ;  
  59.     }  
  60. }  


You can enhance the code if you like, as I've provided the basic code which is re-usable. Also you can change the object passed in standard controller and code will handle the rest. URL should have ID else you will get error on UI :-)

Say, I've saved my VFP code as "MultipleUpload", so my URL will be https://...../apex/MultipleUpload?id=XXXXXXXXXXXXXXX (account id in this code).

No comments:

Post a Comment