function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LA_193LA_193 

how to upload files for multiple fields.... it's very urgent

Hi

 

Generally   we upload  file  for one record in salesforce .But our requirement is we have to upload  files

to  multiple fields for one record  in a Visualpage

 

for example 

 

Field1   Browse button1 

 

Field2   Browse button2

 

Field3   Browse button3

 

Please suggent ....

 

Thanks

Lakshmi

 

Best Answer chosen by Admin (Salesforce Developers) 
LA_193LA_193

 

Thanks alot  sharing this code

 

 

All Answers

APathakAPathak

These files go into the attachment salesforce object?

Laxman RaoLaxman Rao

try this 

 

Vf 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="1" id="block1">
<apex:repeat value="{!attachements}" var="a">
<apex:pageBlockSectionItem >
<apex:outputLabel value="File" for="file"/>
<apex:inputFile value="{!a.body}" filename="{!a.name}" id="file" required="true"/>
</apex:pageBlockSectionItem>
</apex:repeat>
</apex:pageBlockSection>

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

 

Apex Class:

public with sharing class AttachmentUploadController {

public list<Attachment> attachements {get;set;}

public AttachmentUploadController() {
attachements = new list<Attachment>();
Attachment obj1 = new Attachment(Name = 'Attachement 1', ParentId = 'a0190000004J47Y');
attachements.add(obj1);

Attachment obj2 = new Attachment(Name = 'Attachement 2', ParentId = 'a0190000004J47Y');
attachements.add(obj2);

Attachment obj3 = new Attachment(Name = 'Attachement 3', ParentId = 'a0190000004J47Y');
attachements.add(obj3);
}

public PageReference upload() {
try {
insert attachements;
} catch (DMLException e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
return null;
}

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

 

Replace the ParentId with your record id

 

 

LA_193LA_193

 

Thanks alot  sharing this code

 

 

This was selected as the best answer