The WesternTech » Visualforce Pages » Upload a file as an attachment to a record using Visualforce Page
Scenario - Provide the file upload Page to the users. Once user uploads the file, attach it to the Account Record.
Step 1: Create an Apex class named “attachmentsample”
saveDoc method of attachmentSample class getting the recordId from Page Reference and using it to attach the file.
public class attachmentSample {
public attachmentSample(ApexPages.StandardController controller) {
}
Public Attachment myfile;
Public Attachment getmyfile()
{
myfile = new Attachment();
return myfile;
}
Public Pagereference saveDoc()
{
String accid = System.currentPagereference().getParameters().get('id');
Attachment a = new Attachment(parentId = accid, name=myfile.name, body = myfile.body);
//insert the attachment
insert a;
return NULL;
}
}
Step 2: Create a Visualforce page named “attachment”
Step 3: Custom Button
Create a custom button (Detail Page button) on Account and select the new Visualforce page that you created earlier
Step 4: Update Account Layout
Add the new button to the Account Page layout.
Step 5: Quick Test:
If you do not wish to do Step3 and Step4 then follow this.
Paste the following URL in your browser
http://yoursalesforceinstance.com/apex/attachment?id={accountId}
References
All Categories
Recent Posts
Elementor #6437Lightning Out to integrate the Salesforce real time data into external websiteCreating a Lookup Field in Screen FlowDisplay Static Resource into Salesforce FlowConnect Salesforce with Postman Application and Test REST APIDownload and upload Salesforce Metadata using Visual Studio CodeFlow - Create Rollup summary to calculate the Contacts associated with AccountsUpload a file as an attachment to a record using Visualforce PageCreate a Lighting Web Component using Visual Studio CodeHide or Display Text using Picklist Selection by Conditional Logic in Salesforce Flow