Difference between revisions of "Code:Generate an Attachment"
From AgileApps Support Wiki
imported>Aeric |
imported>Aeric |
||
(13 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
This example uses a [[Document Template]] to generate a PDF, and then attaches the | This example uses a [[Document Template]] to generate a PDF or HTML document, and then attaches the document to the current case. It is expected that the method will be invoked from a Rule. | ||
In outline, the process is: | In outline, the process is: | ||
Line 19: | Line 19: | ||
// We assume it is invoked on a Case record. | // 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) | ||
throws Exception | |||
{ | { | ||
String documentTitle = "PUT TITLE OF GENERATED DOCUMENT HERE"; | String documentTitle = "PUT TITLE OF GENERATED DOCUMENT HERE"; | ||
Line 24: | Line 25: | ||
// Get the record ID from the incoming parameters | // Get the record ID from the incoming parameters | ||
String recordID = inParams. | String objectID = inParams.get("object_id"); | ||
String recordID = inParams.get("id"); | |||
// Generate the document | // Generate the document | ||
Result result = Functions.generateDocument( | Result result = Functions.generateDocument(objectID, recordID, templateID, | ||
CONSTANTS.DOCUMENT. | CONSTANTS.DOCUMENT.HTML); | ||
// or CONSTANTS.DOCUMENT. | // or CONSTANTS.DOCUMENT.PDF | ||
int resultCode = result.getCode(); | int resultCode = result.getCode(); | ||
if (resultCode < 0) { | if (resultCode < 0) { | ||
Line 38: | Line 40: | ||
} | } | ||
// Retrieve the document | // Retrieve the document as a PlatformFileBean | ||
String docID = result.getId(); | String docID = result.getId(); | ||
result = Functions.getDocument(docID); | result = Functions.getDocument(docID); | ||
Line 49: | Line 51: | ||
} | } | ||
Parameters docParams = result.getParameters(); | Parameters docParams = result.getParameters(); | ||
PlatformFileBean | PlatformFileBean fileBean = docParams.getPlatformFileBean(docID); | ||
// Add the document as an attachment | // Add the document as an attachment | ||
Line 55: | Line 57: | ||
params.add("title", documentTitle); | params.add("title", documentTitle); | ||
params.add("file_field", fileBean ); | params.add("file_field", fileBean ); | ||
params.add("related_to", " | params.add("related_to", objectID+":"+recordID); | ||
result = Functions.addRecord("attachments", params); | result = Functions.addRecord("attachments", params); | ||
resultCode = result.getCode(); | resultCode = result.getCode(); | ||
Line 67: | Line 69: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{Tip|<br>For a more general solution, add function parameters to specify the document title and the template to use. Then write a wrapper method to supply a specific template, something like this: | |||
:<syntaxhighlight lang="java" enclose="div"> | |||
public void generate_A1_Attachment(com.platform.api.Parameters inParams) | |||
throws Exception | |||
{ | |||
String docTitle = "A1 Attachment"; | |||
String templateID = "9ewr8aasd923234ased0897234d"; | |||
generateAttachment(inParams, docTitle, templateID); | |||
} | |||
</syntaxhighlight> | |||
Then, when you create the invocation Rule, you'll choose which wrapper method to use.}} |
Latest revision as of 02:42, 21 January 2014
This example uses a Document Template to generate a PDF or HTML document, and then attaches the document to the current case. It is expected that the method will be invoked from a Rule.
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 document 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. // We assume it is invoked on a Case record. public void generateAttachment(com.platform.api.Parameters inParams) throws Exception { String documentTitle = "PUT TITLE OF GENERATED DOCUMENT HERE"; String templateID = "PUT ID OF DOCUMENT TEMPLATE HERE";
// Get the record ID from the incoming parameters String objectID = inParams.get("object_id"); String recordID = inParams.get("id");
// Generate the document Result result = Functions.generateDocument(objectID, recordID, templateID, CONSTANTS.DOCUMENT.HTML); // or CONSTANTS.DOCUMENT.PDF 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 as a PlatformFileBean String docID = result.getId(); 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 fileBean = docParams.getPlatformFileBean(docID);
// Add the document as an attachment Parameters params = Functions.getParametersInstance(); params.add("title", documentTitle); params.add("file_field", fileBean ); params.add("related_to", objectID+":"+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; } }
} </syntaxhighlight>