Skip to main content

Output single record to PDF file

This section shows the sample that outputs the record among records of Opportunities object, that is specified by the Apex class, to a PDF file and uploads it to the record.

Reference

The procedure cannot attach the output PDF file to the record. Therefore, Apex class performs the process of uploading the downloaded PDF file to the record.

Points for setting buttons and procedures

  • Set as follows to output a PDF file.

    • When creating a button

      Click the icon_menu_Button.png SVF Button Settings screen - SVF Button Information - icon_setting.png - Action tab, and enable icon_pdf.png.

    • When creating a procedure

      Click the icon_menu_procedure.png Procedure screen - Action, and select "PDF".

  • Set parameters.

    • When setting parameters

      Name

      Default value

      Quotations

      Description

      id

      (Not specify)

      None *1

      ID list of records whose data to be output.

      *1: Because quotation marks are added when creating the ID list with Apex class, quotation marks are not added here.

  • Set the record extraction condition in the main query condition.

    • When setting query conditions

      Filter

      Sort

      Limit

      Offset

      Main query condition

      id = ${id}

      (Not specify)

      ${limit}

      ${offset}

Apex class sample

illust_procedure_PDFAttachemnt.png
  • enqueueJob

    Adds calling of the Apex class "SvfCloudPDFAttachmentQueueable" to the Apex job queue. It also specifies the record to be output.

  • SvfCloudPDFAttachmentQueueable

    Calls the procedure "OpportunityProcedure_PDF" that was created in SVF Cloud Manager. It also attaches the downloaded PDF file to the record.

  • SvfCloudProcedure (Common Apex Class)

    Common Apex classes that are used by all samples.

enqueueJob

// Add the procedure call process to Apex job queue and execute asynchronously.
SvfCloudProcedure proc = new SvfCloudProcedure(
    'a0xxxXXX',                // Tenant ID
    'svfCloudCertName',        // Name of the certificate registered in "Certificate and Key Management"
    UserInfo.getUserName(),    // Salesforce username who runs Salesforce REST from SVF Cloud
    'OpportunityProcedure_PDF' // Procedure name registered in SVF Cloud Manager
);
System.enqueueJob( new SvfCloudPDFAttachmentQueueable(
    proc,
    '006xxxxxxxxxxxx'          // Record ID
));

SvfCloudPDFAttachmentQueueable

global with sharing class SvfCloudPDFAttachmentQueueable implements Queueable, Database.AllowsCallouts{
    private SvfCloudProcedure proc;
    private String recordId;
    global SvfCloudPDFAttachmentQueueable(SvfCloudProcedure proc, String recordId){
        this.proc = proc;
        this.recordId = recordId;
    }
    public void execute(QueueableContext context) {
        
        List<String> recordIds = new List<String>{ recordId };
        Blob pdf = null;
        
        try {
            // Execute the procedure.
            String actionId = proc.executeProcedure(recordIds, null);
            System.debug('actionID: '+actionId);
            
            // Check activities.
            proc.waitStatus(60); 
            
            // Retrieve PDF file.
            pdf = proc.downloadArtifact();
            
            // Attach to the record. 
            Attachment attachment = new Attachment();
            attachment.Name = proc.action.artifact.path; // File name based on SVF button setting of SVF Cloud
            attachment.ParentId = recordId;
            attachment.Body = pdf;
            
            attachment.ContentType = proc.artifactContentType;
            Database.SaveResult saveResult = Database.insert(attachment, false);
            if (!saveResult.isSuccess()) {
                // ERROR
                System.debug('error attachement.');
            }      
        }
        catch (SvfCloudProcedure.SvfCloudException e){
            System.debug(e);
            throw e;
        }
        catch (CalloutException e){
            System.debug(e);
            throw e;
        }
    }
}