function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bathybathy 

Save a record as PDF attachment to the same record

Hi Guys,

My requirement is to save a record as a PDF and attach it to the same record. I got this link in previous post( with same requirement) as a response.http://corycowgill.blogspot.com.au/2012/02/generating-pdf-in-apex-trigger.html?showComment=1432515085965#c7833857141499711147. 

I am sharing my modified apex class and  apex trigger. Can any one suggest a way to change the order of the fields on the pdf file? And also if the field is a check box how can we display Yes/No instead of True/False on the PDF file?
public with sharing class AandcPDFGenerator
{
   
    public static final String FORM_HTML_START = '<HTML><BODY>';
    public static final String FORM_HTML_END = '</BODY></HTML>';

    public static void generateandcPDF(Administration_and_Compliance__c andc)
    {
        String pdfContent = '' + FORM_HTML_START;
        try
        {
            pdfContent = '' + FORM_HTML_START;
            pdfContent = pdfContent + '<H2>A and C Information in PDF</H2> <p/>';
             pdfContent = pdfContent + '<H1>Tax Returns</H1>';
           
            //Dynamically grab all the fields to store in the PDF
            Map<String, Schema.SObjectType> sobjectSchemaMap = Schema.getGlobalDescribe();
            Schema.DescribeSObjectResult objDescribe = sobjectSchemaMap.get('Administration_and_Compliance__c').getDescribe();
            Map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap();
           
            //Append each Field to the PDF
            for(Schema.SObjectField fieldDef : fieldMap.values())
            {
                Schema.Describefieldresult fieldDescResult = fieldDef.getDescribe();
                String label = fieldDescResult.getLabel();
                String name = fieldDescResult.getName();
              //This if Statement is used to add only few required fields instead adding all the fields to the PDF File. 
               if(name == 'Financial_Year__c' || name == 'Due_Date__c' || name == 'Priority_Fund__c' || name == 'Queries_on_the_portal_completed__c' || name == 'FS_Sent_To_Client__c' || name == 'FS_Received_From_Client__c' || name == 'Return_Paid__c' || name == 'Auditor_Requirements_outstanding__c' || name ==  'Auditor_Requirements_submitted__c')
               {
                 
                pdfContent = pdfContent + '<P>' + label + ': ' + andc.get(name) + '</P>';
                
                }
                    
                
            }
            pdfContent = pdfContent + FORM_HTML_END;
        }catch(Exception e)
        {
            pdfContent = '' + FORM_HTML_START;
            pdfContent = pdfContent + '<P>THERE WAS AN ERROR GENERATING PDF: ' + e.getMessage() + '</P>';
            pdfContent = pdfContent + FORM_HTML_END;
        }
        attachPDF(andc,pdfContent);
    }
   
    public static void attachPDF(Administration_and_Compliance__c andc, String pdfContent)
    {
        try
        {
            Attachment attachmentPDF = new Attachment();
            attachmentPDF.parentId = andc.Id;
            attachmentPDF.Name = andc.Name + andc.Return_Paid__c + '.pdf';
            attachmentPDF.body = Blob.toPDF(pdfContent); //This creates the PDF content
            insert attachmentPDF;
        }catch(Exception e)
        {
            andc.addError(e.getMessage());
        }
    }
   
}


Trigger:

trigger AandcTrigger on Administration_and_Compliance__c (after insert,after update)
{
  Administration_and_Compliance__c aandc =new Administration_and_Compliance__c();
  
    if(trigger.isAfter)
    {
        if(trigger.isInsert)
        {
            if(trigger.new.size() == 1) // Lets only do this if trigger size is 1
            {
              if(aandc.Return_Paid__c !=null)
               {
                AandcPDFGenerator.generateandcPDF(trigger.new[0]);
                }
            }
            
        }else if(trigger.isUpdate)
        {
            if(trigger.new.size() == 1) // Lets only do this if trigger size is 1
            {
              if(trigger.old[0].Return_Paid__c != trigger.new[0].Return_Paid__c)
               {
                AandcPDFGenerator.generateandcPDF(trigger.new[0]);
                }
            }
        }
    }
}


Any help is greatly appreciated.

Thanks in advance.