Code:Generate an Attachment
From AgileApps Support Wiki
Revision as of 00:34, 17 January 2014 by imported>Aeric
This example uses a Document Template to generate a PDF, and then attaches the PDF to the current case.
In outline, the process is:
- Get the record ID from the incoming method parameters.
- 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 addRecord API to attach the PlaformFileBean to the case.
- <syntaxhighlight lang="java" enclose="div">
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 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 = "Some Message"; Logger.info(msg + ":\n" + result.getMessage(), "Doc"); // Log details Functions.throwError(msg + "."); // Error message exit; }
// Retrieve the document String docID = result.get("id"); PlatformFileBean fileBean = Functions.getDocument(docID);
// 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); }
} </syntaxhighlight>