You need to sign in to do that
Don't have an account?

Attachment Upload through Visualforce Page
Hello All,
I have a requirement to upload an attachment through Visual Force Page.
Attachment should reside in Custom Object.
On creation of record, user will fill some details and upload attachment from local machine.
Details and Attachment should be captured as record in custom object.
Please let me know for any sample code.
Thanks !
I have a requirement to upload an attachment through Visual Force Page.
Attachment should reside in Custom Object.
On creation of record, user will fill some details and upload attachment from local machine.
Details and Attachment should be captured as record in custom object.
Please let me know for any sample code.
Thanks !
public AttachmentUploadController(ApexPages.StandardController controller) {
Associate__c assoc = (Associate__c)controller.getRecord();
recordId = assoc.id;
}
Let me know if you have any issue.
Abhishek
All Answers
You can find complete help regarding this on link given below :
http://blog.jeffdouglas.com/2010/04/28/uploading-an-attachment-using-visualforce-and-a-custom-controller/
Regards,
Abhishek.
Thanks for your response.
Code works fine if the Parent Id is hardcoded.
Here, I need to capture the attachment.ParentId dynamically on click of the custom button at Objects Detail Page.
Please let me know, how to achieve it.
Thanks !
Please use below code and modify your controller code accordingly : If you are not able to do the chages than please share your class code so that i can help you.
Regards,
Abhishek.
Please find the code below --
VF Page
<apex:page standardController="Associate__c" extensions="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="2" id="block1">
<apex:pageBlockSectionItem >
<apex:outputLabel value="File Name" for="fileName"/>
<apex:inputText value="{!attachment.name}" id="fileName"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="File" for="file"/>
<apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Description" for="description"/>
<apex:inputTextarea value="{!attachment.description}" id="description"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
-------------------------------------------------------------------------
Apex Class
public with sharing class AttachmentUploadController {
public Id recordId;
public AttachmentUploadController() {
}
public AttachmentUploadController(ApexPages.StandardController controller) {
this.recordId = (Associate__c)StandardController.getRecord().Id;
}
public Attachment attachment {
get {
if (attachment == null)
attachment = new Attachment();
return attachment;
}
set;
}
public PageReference upload() {
attachment.OwnerId = UserInfo.getUserId();
attachment.ParentId = recordId; // the record the file is attached to
// attachment.ParentId = ApexPages.currentPage().getParameters().get('id');
attachment.IsPrivate = true;
try {
insert attachment;
} catch (DMLException e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
return null;
} finally {
attachment = new Attachment();
}
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
return null;
}
}
Thanks !!
Yes, this.recordId = (Associate__c)StandardController.getRecord().Id;
is not recognized by Salesforce
public AttachmentUploadController(ApexPages.StandardController controller) {
Associate__c assoc = (Associate__c)controller.getRecord();
recordId = assoc.id;
}
Let me know if you have any issue.
Abhishek