The content you are about to read will show you how to upload files as attachments using Apex in Salesforce. You will learn about the different methods available for doing this, as well as the benefits and drawbacks of each.
By the end, you should have a good understanding of how to upload files as attachments using Apex in Salesforce.
- Salesforce provides various methods for uploading files as attachments. The most common method is to use the Attachment object. This object allows users to upload files to the record they are viewing. Another method is to use the ContentDocumentLink object. This object links a file to a record but does not actually upload the file to Salesforce. Finally, there is the FeedItem object, which can be used to upload files to a user’s feed.
- The Attachment object is the most common method for uploading files as attachments in Salesforce. This object allows users to upload files to the record they are viewing. The maximum file size that can be uploaded using this method is 25 MB. To use the Attachment object, create a new instance of it and set the ParentId property to the Id of the record you want to attach the file to. Then set the Body property to the contents of the file you wish to upload. Finally, call the insert () method to upload the file.
- The ContentDocumentLink object is another method for attaching files in Salesforce. This object links a file to a record but does not actually upload the file to Salesforce. The maximum file size that can be linked using this method is 2 GB. To use the ContentDocumentLink object, create a new instance of it and set the LinkedEntityId property to the Id of the record you want to link the file to. Then set the ContentDocumentId property to the Id of the file you wish to link. Finally, call the insert () method to save the changes.
- Nowadays, it is very common to use file attachments in our application. We might need to attach a file while creating a record or sending an email. Apex Salesforce provides the functionality to attach files to the records using the Attachment object. In this blog post, we will learn how we can upload files as attachment using Apex code in Salesforce.
- First of all, let’s understand how the Attachment object works in Salesforce. The Attachment object contains a field named Body which stores the Base64 encoded version of the attached file. So, when we try to view an attachment, Salesforce first decode the Base64 encoded string and then renders it as an image or PDF or any other type of file.
- To upload a file as attachment, we need to insert a new record into the Attachment object. For this, we need to specify the ParentId field which specifies the Id of the record to which we are going to attach the file. We also need to set the Body field with Base64 encoded version of the file and set the ContentType field with the MIME type of the file.
Let’s see how we can upload files as attachment using Apex code in Salesforce.
- First of all, let’s create a Visualforce page which renders a File Uploader and a Save button.
- Save the Visualforce page and navigate to it. It should look like this:
- Now, click on Choose File button and select any file from your machine. After that, click on Save button.
It should show the following page:
Now, let’s check our Attachment object to see if the file is uploaded or not. For this, navigate to Your Name | Setup | App Setup | Customize | Attachments | Attachments.
You should see the newly created attachment record like this:
As you can see in the above screenshot, the Body field of the attachment record contains Base64 encoded version of the selected file.
So, this is how we can upload files as attachment using Apex code in Salesforce. Hope you find this blog post helpful.
Conclusion:
The Attachment object is the most common method for uploading files as attachments in Salesforce. This object allows users to upload files to the record they are viewing. The maximum file size that can be uploaded using this method is 25 MB. To use the Attachment object, create a new instance of it and set the ParentId property to the Id of the record you want to attach the file to. Then set the Body property to the contents of the file you wish to upload. Finally, call the insert() method to upload the file.