MultiFile upload / Drag and drop Attachments

Attaching multiple files as attachments to records is a pain. Its takes lot of time plus lot of navigation. Today, I want to share a small piece of code that I have written as home page component which will inject a small section in the ‘Notes and Attachments’ related list which will allow the users to load multiple file either selecting it from the input file selection or the users can drag and drop the files.


So how do you implement this in your org.

1) Add the main script to a js file and add it to static resource.

2) Create a new Custom Link.

3) Reference the js file in the custom link you created in the previous step. We will also need jQuery, so we will add that as well.

4) Add that custom link a home page component of type Links.

5) Add the home page component to the home page layout and you’re done. For more details on this hacking method, you can check this link here

So here we go: ->

1) Create a javascript file with the below code and add it as a static resource with the name attachmentjs


    /*Checks when the notes and attachment section is loaded and then initiates the process.*/
    var timeInterval = setInterval(function(){
        if(jQuery("div.bRelatedList[id$=RelatedNoteList]").find("div.pbHeader").find("td.pbButton").find("input[name=attachFile]").length > 0){


/*Adds the drop zone div in the notes and attachment section. Event listener are added to listen when files are dropped in the zone.*/
function addAttachButton() {

    var attachmentDiv = jQuery("div.bRelatedList[id$=RelatedNoteList]");

        jQuery("<td>", {
            style   : "width:35%;cursor:pointer;"
                style : "height: 30px;  width: px;border-color: orange;border: 3px solid orange;border-style: dotted;border-radius: 4px;text-align: center;vertical-align: middle;line-height: 2;color: Red;font-family: monospace;font-size: 14px;",
                id : "dropDiv"
            }).append(jQuery("<span>",{id:"clickHere"}).text('Drop files here / click here!') , jQuery("<span>",{id:"uploadingMessage",style:"display:none;"}).text('Uploading your Files, please wait!'))

        if(jQuery("#uploadingMessage:hidden").length > 0) {
            if(jQuery("#multiUploadButton").length > 0) {
            }else {
        } else {
            alert('Your files are being uploaded. Please Wait!');

    function handleFileSelect(evt) {
        var files = evt.dataTransfer.files; // FileList object.
    function handleDragOver(evt) {
        evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.

    var dropZone = document.getElementById('dropDiv');
    dropZone.addEventListener('dragover', handleDragOver, false);
    dropZone.addEventListener('drop', handleFileSelect, false);

function processFiles(files) {
    var insertedFilesArray = [];
    var fileInsertionCounter = 0;

        for(i=0;i<files.length;i++) {
            var reader = new FileReader();
            reader.onload = (function(newFile,pId) {
                return function(e) {
                    var attachmentBody =',')+1,;
                        type: "POST",
                        url: "https://""/services/data/v33.0/sobjects/Attachment/",
                        contentType:"application/json; charset=utf-8",
                        headers: { 
                            "Authorization" : "Bearer "+getCookie('sid')
                        data    : JSON.stringify({'name','body':attachmentBody,'parentId':pId,'isprivate':false})
                    }).success(function(result) {
                        if(fileInsertionCounter == files.length){
                        alert('Unable to Insert file \n Contact your Adminstrator with this error message \n' + JSON.stringify(err));
                        //if all the files are uploaded then show the zone div.
                        if(fileInsertionCounter == files.length){

/*Inserts the input file button. This is hidden from the view.*/
function insertButton() {
    if(jQuery("#multiUploadButton").length == 0) {
        var attachmentDiv = jQuery("div.bRelatedList[id$=RelatedNoteList]");
                {id     : "multiUploadButton",
                value   : "Multiple Upload",
                type    : "file",
                multiple: "multiple",
                style   : "display:none;"

function hideDiv() {

function showDiv() {


function getCookie(c_name){
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++){
        if (x==c_name){
            return unescape(y);

2) Create a custom link called ‘Attacher Custom Link’ and the add the below code. Make sure you select Behaviour as ‘Execute Javascript’.


… follow all the steps mentioned above and you should be set. I have tested this on Latest Chrome and firefox browsers and this is working just fine.

Generate a PDF and Attach it to record from a trigger in Salesforce — SOAP webservice

The reason you stumbled upon this post is because you tried using the .getContentAsPdf() method in a trigger and you got an error message stating you cannot use it in a trigger. So you tried to search and figure out as to how you could do it.

I had faced a similar issue when a task was given to me attach a pdf(which would be automatically created) when a record is created. Inititally I thought its quite simple and accepted it. But when I actually started coding, I became aware of the issues with this requirement.

Well, I googled for a while and came across a post (unfortunately, I could not find it now) which had the solution to this problem, but the guy had not posted the code to demonstrate. So I thought I could share the code in a post as this could help someone.

Here you go:
Step 1: create a class and include a webservice method that will generate the pdf and attach it to a record
Step 2: Generate the WSDL from this class.
Step 3: Generate a class from the WSDL you just created. (step 2 and 3 are a little wierd, but believe me it works)
Step 4: Create a class which has future Method (with callout=true). This future method will call the stub which you just created from the WSDL.
Step 5: Finally create a trigger that will call this future method.

If you find any better way of doing this please do let me know.

In this scenario I will create a account and a pdf will be attached to it after the insert.

First lets create the VF page to generate the pdf, a simple one. Lets call it, pdfRenderPage

<apex:page standardController="Account" renderAs="pdf">

Hey, the Account name is {!account.Name}


The webservice class, After you save this class, you can see a generate WSDL button. click on it and save the WSDL file

global class AddPdfToRecord{

    webservice static void addPDF(list<id> accountIds){
		//Instantiate a list of attachment object
        list<attachment> insertAttachment = new list<attachment>();
        for(Id accId: accountIds){
			//create a pageReference instance of the VF page.
            pageReference pdf = Page.pdfRenderPage;
			//pass the Account Id parameter to the class.
            Attachment attach = new Attachment();
            Blob body;
                body = pdf.getContent();
            attach.Body = body;
            attach.Name = 'pdfName'+accId+'.pdf';
            attach.IsPrivate = false;
            attach.ParentId = accId;//This is the record to which the pdf will be attached
		 //insert the list
         insert insertAttachment;

Now, consume the WSDL that you just generate. Go to Setup->App Setup-> Develop-> Apex Classes and click on Generate from WSDL. Load the WSDL file and name the class as ‘addPDFStub’.

//Generated by wsdl2apex

public class addPDFStub {
    public class LogInfo {
        public String category;
        public String level;
        private String[] category_type_info = new String[]{'category','','LogCategory','1','1','false'};
        private String[] level_type_info = new String[]{'level','','LogCategoryLevel','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'','true','false'};
        private String[] field_order_type_info = new String[]{'category','level'};
    public class AllowFieldTruncationHeader_element {
        public Boolean allowFieldTruncation;
        private String[] allowFieldTruncation_type_info = new String[]{'allowFieldTruncation','','boolean','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'','true','false'};
        private String[] field_order_type_info = new String[]{'allowFieldTruncation'};
    public class DebuggingHeader_element {
        public addPDFStub.LogInfo[] categories;
        public String debugLevel;
        private String[] categories_type_info = new String[]{'categories','','LogInfo','0','-1','false'};
        private String[] debugLevel_type_info = new String[]{'debugLevel','','LogType','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'','true','false'};
        private String[] field_order_type_info = new String[]{'categories','debugLevel'};
    public class CallOptions_element {
        public String client;
        private String[] client_type_info = new String[]{'client','','string','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'','true','false'};
        private String[] field_order_type_info = new String[]{'client'};
    public class SessionHeader_element {
        public String sessionId;
        private String[] sessionId_type_info = new String[]{'sessionId','','string','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'','true','false'};
        private String[] field_order_type_info = new String[]{'sessionId'};
    public class addPDFResponse_element {
        private String[] apex_schema_type_info = new String[]{'','true','false'};
        private String[] field_order_type_info = new String[]{};
    public class DebuggingInfo_element {
        public String debugLog;
        private String[] debugLog_type_info = new String[]{'debugLog','','string','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'','true','false'};
        private String[] field_order_type_info = new String[]{'debugLog'};
    public class addPDF_element {
        public String[] accountIds;
        private String[] accountIds_type_info = new String[]{'accountIds','','ID','0','-1','true'};
        private String[] apex_schema_type_info = new String[]{'','true','false'};
        private String[] field_order_type_info = new String[]{'accountIds'};
    public class AddPdfToRecord {
        public String endpoint_x = 'https://'+URL.getSalesforceBaseUrl().getHost()+'/services/Soap/class/AddPdfToRecord';
        public Map<String,String> inputHttpHeaders_x;
        public Map<String,String> outputHttpHeaders_x;
        public String clientCertName_x;
        public String clientCert_x;
        public String clientCertPasswd_x;
        public Integer timeout_x;
        public addPDFStub.CallOptions_element CallOptions;
        public addPDFStub.AllowFieldTruncationHeader_element AllowFieldTruncationHeader;
        public addPDFStub.SessionHeader_element SessionHeader;
        public addPDFStub.DebuggingHeader_element DebuggingHeader;
        public addPDFStub.DebuggingInfo_element DebuggingInfo;
        private String CallOptions_hns = 'CallOptions=';
        private String AllowFieldTruncationHeader_hns = 'AllowFieldTruncationHeader=';
        private String SessionHeader_hns = 'SessionHeader=';
        private String DebuggingHeader_hns = 'DebuggingHeader=';
        private String DebuggingInfo_hns = 'DebuggingInfo=';
        private String[] ns_map_type_info = new String[]{'', 'addPDFStub'};
        public void addPDF(String[] accountIds) {
            addPDFStub.addPDF_element request_x = new addPDFStub.addPDF_element();
            addPDFStub.addPDFResponse_element response_x;
            request_x.accountIds = accountIds;
            Map<String, addPDFStub.addPDFResponse_element> response_map_x = new Map<String, addPDFStub.addPDFResponse_element>();
            response_map_x.put('response_x', response_x);
              new String[]{endpoint_x,
            response_x = response_map_x.get('response_x');

Next create a class with the future method:

global class AccountTriggerController{

    public static void addPDFAttach(string sessionId, list<id> accountId){
        addPDFStub.SessionHeader_element sessionIdElement= new addPDFStub.SessionHeader_element();
        sessionIdElement.sessionId = sessionId;

        addPDFStub.AddPdfToRecord stub= new addPDFStub.AddPdfToRecord();
        stub.SessionHeader= sessionIdElement;

FInally create the trigger:

trigger accountAfterInsert on Account (after insert) {

     list<id> accId = new list<id>();
     for(id acc: trigger.newMap.keySet()){
		//You would need to send the session id to the future method as the you cannot use the userInfo.getSessionID() method in the future method because it is asynchronous and doesn't run in the user context.
        AccountTriggerController.addPDFAttach(userInfo.getSessionId(), accId);


All of this would work only if you add the host in my case it’s, add it to the remote site setting. So go to Setup-> Admin Setup -> security Controls -> Remote Site Settings. Click on new, give it a name(webserviceTest), add the Remote Site URL(just copy paste the url from the address bar), it will only take the host and truncate the rest when you save it.

Hope it helps!!


I have updated the post with the test Classes. Now all the classes have more than 90% coverage!
ALso I have made some changes in the AddPdfToRecord class to accommodate the Test class changes
Below test classes needs to be added to get the coverage

First add the web service mock implementation and then the actual test class.

global class WebServiceMockImpl implements WebServiceMock {
   global void doInvoke(
           Object stub,
           Object request,
           Map<String, Object> response,
           String endpoint,
           String soapAction,
           String requestName,
           String responseNS,
           String responseName,
           String responseType) {
           String[] accountIdArray = new String[]{};
       addPDFStub.addPDFResponse_element respElement = new addPDFStub.addPDFResponse_element();
       addPDFStub.AllowFieldTruncationHeader_element class1= new addPDFStub.AllowFieldTruncationHeader_element();
       addPDFStub.DebuggingHeader_element class2= new addPDFStub.DebuggingHeader_element();
       addPDFStub.CallOptions_element class3= new addPDFStub.CallOptions_element();
       addPDFStub.DebuggingInfo_element  class4= new addPDFStub.DebuggingInfo_element ();
       addPDFStub.SessionHeader_element class5= new addPDFStub.SessionHeader_element();
       response.put('response_x', respElement ); 
public class AddPDFtoRecordTest{
    @isTest static void addPDFtoRecordTestMethod() { 
        Test.setMock(WebServiceMock.class, new WebServiceMockImpl());
        account a = new account(); 'test PDF';
        insert a;
        AddPdfToRecord.addPDF(new list<Id>{a.Id});    

YOu can achieve the same by using the APEX Rest Resource. Check out the post here

You can find the code on gitHub