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
jordanmjordanm 

Create PDF apex plugin from cloud designer webinar & test case question

Hello,

 

I couldn't find it anywhere, so I reproduced the create and attach PDF plugin from the cloud designer webinar. Here it is for those that may want it.

global class CreateAndAttachPDF implements Process.Plugin { 

// member variables for status
    public static final String SUCCESS_CODE = 'SUCCESS';
    public static final String ERROR_CODE = 'ERROR';

    public String aStatus;
    public String anErrorDescription;

// The main method to be implemented. The Flow calls this at runtime.  
    
global Process.PluginResult invoke(Process.PluginRequest request) { 
        

    // Get record to which pdf needs to be attached
    
    String recordID = (String) request.inputParameters.get('recordID');
        
    // Get the attachment content
    String pdfAttachmentName = (String) request.inputParameters.get('pdfAttachmentName');
    String pdfAttachmentContent = (String) request.inputParameters.get('pdfAttachmentContent');  
        
    Map<String,Object> result = new Map<String,Object>(); 
    AttachPDF(recordID, pdfAttachmentName, pdfAttachmentContent);
    result.put('Status', aStatus);
    result.put('Error Message', anErrorDescription);
    
        return new Process.PluginResult(result); 
    } 

    // Returns the describe information for the interface  
    
    global Process.PluginDescribeResult describe() { 

    Process.PluginDescribeResult result = new Process.PluginDescribeResult(); 
    result.description = 'The CreateAndAttachPDF flow plug-in creates a PDF document and attaches it to specific record';
    result.tag = 'PDF Utils';

        result.inputParameters = new 
           List<Process.PluginDescribeResult.InputParameter>{ 
               new Process.PluginDescribeResult.InputParameter('recordID', 
               Process.PluginDescribeResult.ParameterType.STRING, true),
        new Process.PluginDescribeResult.InputParameter('pdfAttachmentName', 
               Process.PluginDescribeResult.ParameterType.STRING, true),
        new Process.PluginDescribeResult.InputParameter('pdfAttachmentContent', 
               Process.PluginDescribeResult.ParameterType.STRING, true)   
            }; 
        result.outputParameters = new 
           List<Process.PluginDescribeResult.OutputParameter>{
        new Process.PluginDescribeResult.OutputParameter('Status', 
               Process.PluginDescribeResult.ParameterType.STRING), 
        new Process.PluginDescribeResult.OutputParameter('Error Message', 
               Process.PluginDescribeResult.ParameterType.STRING)
        }; 
        return result; 
    }

    public void AttachPDF(String recordID, String pdfAttachmentName, String pdfAttachmentContent) {

    Blob pdfAttachment;
        try {
            pdfattachment = Blob.toPdf(pdfAttachmentContent);
            Attachment attach = new Attachment();
            attach.ParentId = recordId;
            attach.Name = pdfAttachmentName;
            attach.Body = pdfAttachment;
            insert(attach);

            aStatus = SUCCESS_CODE;
        } catch (Exception anException) {
            astatus = ERROR_CODE;
            anErrorDescription = anException.getMessage();
        }
    }
}

 Here is the test case I wrote for it (Doesn't appear in the webinar). I only got it up to 78% coverage, any idea how to get this up to 100? The red problem areas seem to all be in the DescribeResult section.

@isTest
public class TestCreateAndAttachPDF {

    public static testMethod void MyTest() {
              
        Profile pf = [Select Id from Profile where Name = 'Sys Admin'];

        User u = new User();
        u.FirstName = 'Test';
        u.LastName = 'User';
        u.Email = 'testuser@test123456789.com';
        u.CompanyName = 'test.com';
        u.Title = 'Test User';
        u.Username = 'testuser@test123456789.com';
        u.Alias = 'testuser';
        u.CommunityNickname = 'Test User';
        u.TimeZoneSidKey = 'America/Mexico_City';
        u.LocaleSidKey = 'en_US';
        u.EmailEncodingKey = 'ISO-8859-1';
        u.ProfileId = pf.Id;
        u.LanguageLocaleKey = 'en_US';
        insert u;

        system.runAs(u){
    
            Family__c o = new Family__c();
            o.Family_Type__c = 'Other';
            insert o;
            
            CreateAndAttachPDF plugin = new CreateAndAttachPDF();
            Map<String,Object> inputParams = new Map<String,Object>();

            string recordID = o.Id;
            string pdfAttachmentName = 'test intake form';
            string pdfAttachmentContent = 'test content';
            InputParams.put('recordID', recordID);
            InputParams.put('pdfAttachmentName', pdfAttachmentName);
            InputParams.put('pdfAttachmentContent', pdfAttachmentContent);

            Map<String,Object> outputParams = new Map<String,Object>();
            string Status = 'status test';
            string ErrorMsg = 'error message test';
            outputParams.put('Status',Status);
            outputParams.put('Error Message',ErrorMsg);


            Process.PluginRequest request = new Process.PluginRequest(inputParams);
            Process.PluginRequest request2 = new Process.PluginRequest(outputParams);           
            
            plugin.invoke(request);
            plugin.invoke(request2);
        }
     }
}