Actions can be added to your grids to incorporate additional Salesforce functions, such as ‘Convert’ or ‘Send email’. All GridBuddy Connect Actions must point to a URL. In this example, we will add a button to add attachments to an account
- Create a new Apex Class with the following 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;
}
}
- Create a new Visualforce page using the following code.
- Name it “MassAttachments”
- Save it
- Click ‘preview’ and copy the preview URL
<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>
- Go to GridBuddy Connect Wizard, click “Manage Actions” and click “Create a new action”
- Action Name: Attach files
- Type: Batch OR Single Record
- Batch will put a button at the top of the grid. You will have to select grid records, then click the button, and you can add the same file to multiple grid records
- Single Record will put the action into the three vertical dots. Click the action to add files to that single grid record.
- Location: If you select ‘batch’, location should be ‘button’
- Connection: This will make this action available only to grids built off this connection.
- Object: Pick the object that the grid, to which you want to add this action, is built on. This will make this action available to all grids built off this object within the selected connection.
- Select the display behavior: window overlay
- Content Source: URL
- Content: Paste in the Visualforce ‘preview’ URL
- ID Parameter: “id”
- Click ‘save’
- Add this action to a grid
- Go to ‘Manage grids’ and select the grid you want to add this action to. Make sure the object and connection match whatever you selected in the Action
- In step 2 of the grid, scroll to the bottom and check the box beside this action
- Click ‘save’
- Launch the grid
- If you selected ‘batch’, the ‘Attach files’ button will appear at the top of the grid.
- Check the boxes beside the grid records to which you want to attach files
- Click the action button. A window will open with the option to select and upload the files of your choice.
- If you selected ‘batch’, the ‘Attach files’ button will appear at the top of the grid.
-
- If you selected ‘single record’, the ‘Attach files’ action will appear in the three vertical dots to the left of each grid record
- Click the action button for the record to which you want to attach files. A window will open with the option to select and upload the files of your choice to this record.
- If you selected ‘single record’, the ‘Attach files’ action will appear in the three vertical dots to the left of each grid record