Customize Email Attachment Selection

Introduction

By default, the S-Docs Email page includes an Attach or Remove Files button that allows the user to attach S-Docs, Attachments, or Files related to the base record to the outbound email. This button also allows users to attach Documents from the Documents folder, or upload their own files.

You can also add your own Add Files button that queries Salesforce File IDs using your own custom Apex class and attaches them to the email. This article provides step-by-step instructions for setting up a custom Add Files button to call your custom Apex class, as well as a sample Apex class.

Create Apex Class

The first step is to create an Apex class that returns a list of ContentDocumentIDs (the IDs of the files that your custom button will attach to the email).

You can use whatever logic you'd like for your Apex class. The following sample class returns related Product file IDs.

global class SDEmailFilesRetriever implements Callable {

    public Object call(String action, Map<String,Object> args) {
        switch on action {
            when 'getProductFileIDs' {
                Id recordID = (Id)args.get('recordID');
                List<OpportunityLineItem> oliList = [SELECT Product2.Id FROM OpportunityLineItem WHERE OpportunityId=:recordID];
                List<Id> productIDs = new List<Id>();
                for (OpportunityLineItem oli : oliList) {
                    productIDs.add(oli.Product2.Id);
                }
                List<Id> contentDocumentIDs = new List<Id>();
                if (!productIDs.isEmpty()) {
                    List<ContentDocumentLink> cdlList = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId IN :productIDs];
                    for (ContentDocumentLink cdl : cdlList) {
                        contentDocumentIDs.add(cdl.ContentDocumentId);
                    }
                }
                return contentDocumentIDs;
            }
        }
        return null;
    }
    
}

The following test class provides test coverage for the class above.

@isTest
private class SDEmailFilesRetrieverTest {
    @isTest(SeeAllData=true)
    private static void testSDEmailFilesRetriever() {
        List<OpportunityLineItem> oliList = [SELECT Product2.Id FROM OpportunityLineItem];
        List<Id> productIDs = new List<Id>();
        for (OpportunityLineItem oli : oliList) {
            productIDs.add(oli.Product2.Id);
        }
        List<Id> contentDocumentIDs = new List<Id>();
        List<ContentDocumentLink> cdlList = [SELECT LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId IN :productIDs];
        Id productId = cdlList[0].LinkedEntityId;
        
        oliList = [SELECT OpportunityId FROM OpportunityLineItem WHERE Product2Id=:productId];
        Id recordId = oliList[0].OpportunityId; 

        Type classType = Type.forName('SDEmailFilesRetriever');
        Callable extension = (Callable)classType.newInstance();
        extension.call('getProductFileIDs', new Map<String,Object>{ 'recordID' => recordId });
        extension.call(null, null);
    }
    
}

To add these classes to your org, navigate to the Setup menu and type "Apex Classes" into the Quick Find bar. Click Apex Classes (under Custom Code) and then click New.

Be sure to create separate classes for your main and test class.

Configure The customAddFilesButton Parameter

The customAddFilesButton parameter must be added to any S-Docs button that should display your custom Add Files button on the S-Docs Email page when users generate and email documents using that button. To set this parameter up, you must add required attributes and encode it using a URL encoder. We'll walk through these steps now.

Add Parameter Attributes

This parameter has 2 required attributes and 2 optional attributes. Let's take a look at sample syntax and walk through what each attribute means.

Note: This parameter cannot be added to your S-Docs button URL as shown below; it must be encoded using a URL encoder first. We'll walk through that process in the next section.

customAddFilesButton={
  "buttonLabel" : "Attach All Related Product Files",
  "action" : "SDEmailFilesRetriever.getProductFileIDs",
  "renderConditions" : "IS_SSIGN",
  "autoRun" : "true"
}

buttonLabel: The text that appears on the button. The syntax above would create the button shown in the image below. This attribute is required.

action: The custom Apex class name and function that will be called when users click this button. Since our sample Apex class name is SDEmailFilesRetriever, and our function is getProductFileIDs, our action is SDEmailFilesRetriever.getProductFileIDs. This attribute is required.

renderConditions: A comma-delimited list of conditions necessary for the button to display. If this attribute is not included, the button will always display. This attribute is optional.

Note: IS_SSIGN is a custom condition that will cause the button to display only when the generated document is S-Sign enabled.

autoRun: When set to "true," the function specified in the action attribute will automatically run upon loading the S-Docs email page. This attribute is optional.

Encode Your Parameter

After setting up the customAddFilesButton with required attributes, you must encode it using a URL encoder before adding it to your S-Docs button URL.

To do so, copy all parameter syntax after the equals sign, including brackets (shown highlighted below).

Then, navigate to a URL encoding tool like the one linked here. [1] Paste your syntax, [2] encode it, and [3] copy the encoded URL.

You will add the encoded URL to your S-Docs button in the next section.

Add The customAddFilesButton Parameter To S-Docs Button

Finally, you must add the customAddFilesbutton parameter to any S-Docs button(s) that should display your custom Add Files button on the S-Docs Email page.

To do so, navigate to the Object Manager in the Setup menu, then find and click on your object.

Navigate to the Buttons, Links, and Actions tab in the sidebar. Find your S-Docs button, then click the dropdown arrow on the right and click Edit.

Add the customAddFilesButton parameter to your button URL using the following syntax, replacing PASTE_ENCODED_URL_HERE with your encoded URL.

{!URLFOR('/apex/SDOC__SDCreate1', null,[id=Opportunity.Id, Object='Opportunity', customAddFilesButton='PASTE_ENCODED_URL_HERE'])}

Then, click Save.

See It In Action

When users generate documents from your newly modified S-Docs button and navigate to the email page, your custom Add Files button will appear. When clicked, the files specified by your Apex class will be attached to the email.

Note: If you set the autoRun attribute to true, the files will be automatically added when the user opens the email page.

Tags: ,

Was this helpful?