This action allows you to attach a file to an individual record from within a grid. Please note that it does not allow you to view the record from the grid after it's been attached; you would need to open that record in Salesforce to view the attachment.
Instructions:
1. Go to Setup > Visualforce pages and create a new Visualforce page
2. Call the page "MassAttachmentsController"
3. Delete the default content and paste this code into your new page:
<apex:page controller="MassAttachmentsController" lightningStylesheets="true">
<style type="text/css">
.container, .buttonsArea {
margin: 30px;
}
.buttonsArea.pbButton {
text-align: left;
}
</style>
<apex:form >
<apex:pageMessages />
<section id="container" class="container" >
<div class="fileInputArea">
<apex:inputFile value="{!file}" fileName="{!fileName}" />
</div>
</section>
<section class="buttonsArea pbButton">
<apex:commandButton action="{!upload}" value="Upload" />
</section>
</apex:form>
</apex:page>
4. Click "save" and when it prompts you to create related Apex classes, click on them to auto-create them. Do this until all the prompts disappear and you are able to save your new Visualforce page.
5. In the quick find box, search for "Apex class"
6. Find the apex class called "MassAttachmentsController" and edit it.
7. Delete the code within it and paste in this code:
public class MassAttachmentsController {
public blob file { get; set; }
public string fileName { get; set; }
public PageReference upload() {
ApexPages.getMessages().clear();
try {
List<String> parentIds = ApexPages.currentPage().getParameters().get('id').split(',');
List<ContentDocumentLink> filesToInsert = new List<ContentDocumentLink>();
ContentVersion v = new ContentVersion();
v.versionData = this.file;
v.title = this.fileName;
v.pathOnClient = '/' + this.fileName;
insert v;
Id contentVersionId = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id =: v.Id].ContentDocumentId;
for (String parentId : parentIds) {
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.ContentDocumentId = contentVersionId;
cdl.LinkedEntityId = parentId;
cdl.ShareType = 'V';
filesToInsert.add(cdl);
}
insert filesToInsert;
this.file = null;
Apexpages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.CONFIRM, 'Upload successful.'));
} catch(Exception ex) {
Apexpages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR, 'There was an error while uploading a file.'));
}
return null;
}
}
8. Click save
9. Go to the Grid Wizard > Manage Actions
10. Create a new action.
* Call it "add attachment" or something similar.
* Object: Select the object you want this action to function on. Example: Opportunity, if you want to add attachments to opportunity records.
* Type: "single record"
* Display behavior: "window overlay"
* Content source: visualforce page
* Content: MassAttachmentsController
* ID parameter: id (lowercase)
11. Go to the grid to which you'd like to add this action, and open the Grid Wizard > Select fields & actions. Scroll to the bottom and check the box beside this new action. Click 'save' and close/refresh your grid.
12. Click the three vertical dots beside any record on this grid. You should now see the "add attachments" option there. Click it to open the action and add an attachment to that record from within your grid.