Difference between revisions of "Code:Generate an Attachment"

From AgileApps Support Wiki
imported>Aeric
imported>Aeric
Line 5: Line 5:
# Use the [[generateDocument]] API to create a PDF (or HTML) document from an existing template.
# Use the [[generateDocument]] API to create a PDF (or HTML) document from an existing template.
# Use the [[getDocument]] API to retrieve it, in the form of a {{^PlatformFileBean}}.
# Use the [[getDocument]] API to retrieve it, in the form of a {{^PlatformFileBean}}.
# Use the [[addRecord]] API to attach the PlaformFileBean to the case.
# Use the [[addRecord]] API to attach the document to the case.


:<syntaxhighlight lang="java" enclose="div">
:<syntaxhighlight lang="java" enclose="div">
Line 16: Line 16:
public class UtilityFunctions
public class UtilityFunctions
{
{
   // This signature allows the method to be invoked from a rule
   // This signature allows the method to be invoked from a rule.
  // We assume it is invoked on a Case record.
   public void generateAttachment(com.platform.api.Parameters inParams)
   public void generateAttachment(com.platform.api.Parameters inParams)
   {
   {
Line 27: Line 28:
     int resultCode = result.getCode();
     int resultCode = result.getCode();
     if (resultCode < 0) {
     if (resultCode < 0) {
         String msg = "Some Message";
         String msg = "Document generation failed";
         Logger.info(msg + ":\n" + result.getMessage(), "Doc"); // Log details
         Logger.info(msg + ":\n" + result.getMessage(), "genAttachment");
         Functions.throwError(msg + ".");                       // Error message
         Functions.throwError(msg + ".");
         exit;
         return;
     }
     }


     // Retrieve the document
     // Retrieve the document
     String docID = result.get("id");
     String docID = result.get("id");
     PlatformFileBean fileBean = Functions.getDocument(docID);
     result = Functions.getDocument(docID);
    resultCode = result.getCode();
    if (resultCode < 0) {
        String msg = "Failed to retrieve the document";
        Logger.info(msg + ":\n" + result.getMessage(), "genAttachment");
        Functions.throwError(msg + ".");
        return;
    }
    Parameters docParams = result.getParameters();
    PlatformFileBean file = docParams.getPlatformFileBean(documentId);


     // Add the document as an attachment
     // Add the document as an attachment
Line 43: Line 53:
     params.add("related_to", "cases:"+recordID);
     params.add("related_to", "cases:"+recordID);
     result = Functions.addRecord("attachments", params);
     result = Functions.addRecord("attachments", params);
    resultCode = result.getCode();
    if (resultCode < 0) {
        String msg = "Failed to attach document to case";
        Logger.info(msg + ":\n" + result.getMessage(), "genAttachment");
        Functions.throwError(msg + ".");
        return;
    }
   }
   }
}
}
</syntaxhighlight>
</syntaxhighlight>

Revision as of 00:43, 17 January 2014

This example uses a Document Template to generate a PDF, and then attaches the PDF to the current case.

In outline, the process is:

  1. Get the record ID from the incoming method parameters.
  2. Use the generateDocument API to create a PDF (or HTML) document from an existing template.
  3. Use the getDocument API to retrieve it, in the form of a PlatformFileBean.
  4. Use the addRecord API to attach the document to the case.
package com.platform.yourCompany.yourPackage;

import com.platform.api.*;
import com.platform.beans.*;
//import java.util.*;

public class UtilityFunctions
{
  // This signature allows the method to be invoked from a rule.
  // We assume it is invoked on a Case record.
  public void generateAttachment(com.platform.api.Parameters inParams)
  {
     // Get the record ID from the incoming parameters
     String recordID = inParams.get("id");

     // Generate the document
     Result result = Functions.generateDocument("cases", recordID, templateID, CONSTANTS.DOCUMENT.PDF);
                                                                         // or CONSTANTS.DOCUMENT.HTML 
     int resultCode = result.getCode();
     if (resultCode < 0) {
        String msg = "Document generation failed";
        Logger.info(msg + ":\n" + result.getMessage(), "genAttachment");
        Functions.throwError(msg + ".");
        return;
     }

     // Retrieve the document
     String docID = result.get("id");
     result = Functions.getDocument(docID);
     resultCode = result.getCode();
     if (resultCode < 0) {
        String msg = "Failed to retrieve the document";
        Logger.info(msg + ":\n" + result.getMessage(), "genAttachment");
        Functions.throwError(msg + ".");
        return;
     }
     Parameters docParams = result.getParameters();
     PlatformFileBean file = docParams.getPlatformFileBean(documentId);

     // Add the document as an attachment
     Parameters params = Functions.getParametersInstance();
     params.add("title", "…name of generated file…");
     params.add("file_field", fileBean );
     params.add("related_to", "cases:"+recordID);
     result = Functions.addRecord("attachments", params);
     resultCode = result.getCode();
     if (resultCode < 0) {
        String msg = "Failed to attach document to case";
        Logger.info(msg + ":\n" + result.getMessage(), "genAttachment");
        Functions.throwError(msg + ".");
        return;
     }
  }
}