Uploading Multiple Attachments into Salesforce | Techila
2109
single,single-post,postid-2109,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive
 

Uploading Multiple Attachments into Salesforce

UPLOADING MULTIPLE ATTACHMENTS INTO SALESFORCE

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.

Upload multiple attachments into Salesforc

Visualforce Page Code:

<apex:page standardController=”Account” extensions=”MultipleUploadController”>

<apex:form>

<apex:pageBlock title=”Upload Multiple Attachment to Object”>

<apex:pageBlockButtons>

<apex:commandButton value=”Upload”  action=”{!SaveAttachments}”/>

</apex:pageBlockButtons>

<apex:pageMessages id=”MSG”/>

<apex:actionFunction name=”ChangeCount” action=”{!ChangeCount}”/>

<apex:pageblocksection>

<apex:pageBlockSectionItem>

<apex:outputLabel value=”How many files you want to upload?”/>

<apex:selectList onchange=”ChangeCount() ;” multiselect=”false” size=”1″ value=”{!FileCount}”>

<apex:selectOption itemLabel=”–None–” itemValue=””/>

<apex:selectOptions value=”{!filesCountList}”/>

</apex:selectList>

</apex:pageBlockSectionItem>

</apex:pageblocksection>

<apex:pageBlockSection title=”Select Files” rendered=”{!IF(FileCount != null && FileCount != ”, true , false)}”>

<apex:repeat value=”{!allFileList}” var=”AFL”>

<apex:inputfile value=”{!AFL.body}” filename=”{!AFL.Name}” />

<br/>

</apex:repeat>

</apex:pageBlockSection>

</apex:pageBlock>

</apex:form>

</apex:page>

Apex Code:

public class MultipleUploadController

{

//Picklist of tnteger values to hold file count

public List<SelectOption> filesCountList {get; set;}

//Selected count

public String FileCount {get; set;}

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

public MultipleUploadController(ApexPages.StandardController controller)

{

//Initialize

filesCountList = new List<SelectOption>() ;

FileCount = ” ;

allFileList = new List<Attachment>() ;

//Adding values count list – you can change this according to your need

for(Integer i = 1 ; i < 11 ; i++)

filesCountList.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 record Id in parameter.’));

if(FileCount == null || FileCount == ”)

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 = accid, name=myfile.name, body = myfile.body);

for(Attachment a: allFileList)

{

if(a.name != ” && a.name != ” && a.body != null)

listToInsert.add(new Attachment(parentId = accId, name = a.name, body = a.body)) ;

}

//Inserting attachments

if(listToInsert.size() > 0)

{

insert listToInsert ;

ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.INFO, listToInsert.size() + ‘ file(s) are uploaded successfully’));

FileCount = ” ;

}

else

ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,’Please select at-least one file’));

return null;

}

public PageReference ChangeCount()

{

allFileList.clear() ;

//Adding multiple attachments instance

for(Integer i = 1 ; i <= Integer.valueOf(FileCount) ; i++)

allFileList.add(new Attachment()) ;

return null ;

}

}

You can enhance the code as you like, 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).

Courtesy: http://forceguru.blogspot.in/2012/12/uploading-multiple-attachments-into.html
http://uploads.webflow.com/54f0e01674244f7d696ff311/56e9f385de3aa8347038118d_QuestionClips.png

“Explore – Techila Global Services, A Salesforce development company”

Author: techila

No Comments

Post A Comment