Find your content:

Search form

You are here

Generating and sending PDF to be signed

 
Share

I have a custom object named Certificate__c, once a user asks for a certificate, a PDF doc generated and send to the manager to be signed. Here's my code behind. I'm calling it from a trigger after insert.

public static List<Certificate__c> lstCertif = new List<Certificate__c>();

public static Map<Id, Certificate__c> MapCertificate = new Map<Id, Certificate__c>();
    public static List<Attachment> lstPdfAttachement = new List<Attachment>();

    //Variable of the Parsing HTML
    public static List<Document> lstDocuments = new List<Document>();
    public static String docContent;
    public static List<String> lstContentPdf = new List<String>();
    public static Map<Id, String> contentPdfMap = new Map<Id, String>();

    private static string replaceSTR(string str,Map<string,string> replaceMap)
        {
        for(String toReplace : replaceMap.keySet()){
            string replace = replaceMap.get(toReplace);

            str = str.replace(toReplace, replace);
        }
        return str;
    }
    public static void MyKerensenCertificates(List<User> lstUser){
                    for(User user : lstUser){
                    Certificate__c certif = new Certificate__c();
                    //thirdDoc.ThirdId__c = acc.Id;
                    //thirdDoc.Document_Type__c = 'Historical MIFID';
                    lstCertif.add(certif);
                    }
    //Insert the List of Third Document
    insert lstCertif;
    //Construct HTML Content for PDF : 
    lstDocuments = [Select  e.Body,e.developerName,e.Keywords From Document e where e.Name = 'CertifModel'];
                    if(lstDocuments.size()>0){
                    docContent = '';
                    docContent = lstDocuments[0].body.toString();
                    fusionCorps1(lstUser);
                    }
    for(User user : lstUser){
        Attachment myAttach = new Attachment();
        myAttach.ParentId = MapCertificate.get(user.Id).Id;//Id of the object to which the page is attached
        myAttach.name = 'Certificate.pdf';
        myAttach.body = Blob.toPdf(contentPdfMap.get(user.Id));
        lstPdfAttachement.add(myAttach);
    }
    insert lstPdfAttachement;
    }

    private static void fusionCorps1(List<User> lstUser) 
        {
            Map<String,String> replaceMap = new Map<String,String>();

            for(User user : lstUser){

            if(user.LastName!=null)
                replaceMap.put('{Employee LName}' ,user.LastName);
                else 
                replaceMap.put('{Employee LName}' ,'');
             if(user.FirstName!=null)
                replaceMap.put('{Employee FName}' ,user.FirstName);
                else 
                replaceMap.put('{Employee FName}' ,'');
             if(user.LastName!=null)
                replaceMap.put('{Employee Street}' ,user.Street);
                else 
                replaceMap.put('{Employee Street}' ,'');
             if(user.LastName!=null)
                replaceMap.put('{Employee City}' ,user.City);
                else 
                replaceMap.put('{Employee City}' ,'');
             if(user.LastName!=null)
                replaceMap.put('{Employee State}' ,user.State);
                else 
                replaceMap.put('{Employee State}' ,'');
             if(user.LastName!=null)
                replaceMap.put('{Employee PC}' ,user.PostalCode);
                else 
                replaceMap.put('{Employee PC}' ,'');

             if(user.LastName!=null)
                replaceMap.put('{Employee Country}' ,user.Country);
                else 
                replaceMap.put('{Employee Country}' ,'');
            if(user.LastName!=null)
                replaceMap.put('{Employee Title}' ,user.Title);
                else 
                replaceMap.put('{Employee Title}' ,'');

            docContent = replaceSTR(docContent,replaceMap);  
            if(!contentPdfMap.containsKey(user.Id)){
                contentPdfMap.put(user.Id, docContent);
            }

            }
        }

}

Attribution to: LoveLace
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/32669

My Block Status

My Block Content