Difference between revisions of "Create a JSP Page Print Template"

From LongJump Support Wiki
imported>Aeric
(Created page with " ===Inserting an Image Stored in an Object Record=== To insert an image into a Print Template, use the platform <tt>IMG</tt> tag in an HTML <tt>img</tt> element. ;Format: :<tt>…")
 
imported>Aeric
 
(26 intermediate revisions by the same user not shown)
Line 1: Line 1:
===Creating the Template Page===
# Start by creating a JSP/Html [[Page]] in the platform.
#: Turn off the ''Include Headers'' option for that page.
#: (Those headers contain platform-specific Javascript that interfere with template processing.)
# Edit the page either in the platform, or using the [[Eclipse Plug-In]].
# Add boilerplate text and fixed graphics
# Use JSP code and the platform APIs to control page content, as explained below.


===Inserting an Image Stored in an Object Record===
===Accessing Record Information===
To insert an image into a Print Template, use the platform <tt>IMG</tt> tag in an HTML <tt>img</tt> element.
{{:Get Object and Record ID from the Request Object}}


;Format:
===Processing Related Records===
:<tt><nowiki><img src="IMG</nowiki></tt><u><b>{$</b></u>''objectName''<u>.</u>''imageFieldName''<u><b>}</b></u><tt>"></tt>
Use the Java [[searchRecords]] API on the [[Related Object]], selecting all records whose lookup-field value matches the current record ID.
where:
:* '''objectName -''' The name of the current object. (This variable is in {{Velocity}} format. The object name effectively creates a namespace, ensuring that there is no conflict with another variable that might have the same name.)
:* '''imageFieldName -''' The name of the field in the current record that contains the image to display, joined to the object name by a "dot" (.).


{{Important|The braces <u>{</u>...<u>}</u> and other underlined characters in this syntax are ''literals''. Type them in exactly as shown.}}
For example, when the ClaimItems object has a lookup to Claims. When processing a Claim with record ID 1234, search for ClaimItems records who have claim lookup field with that value.


;Result:
===Using APIs===
:The URL for the image is inserted into the generated page. When viewed, the image is displayed.
{{:Common:Using APIs}}


;Example:
===Learn More===
:<tt><nowiki><img src=”IMG{$Customer.logo_image}”></nowiki></tt>
:* [[Print Templates]]
:* [[Pages#Print Templates Using Pages|Print Templates Using Pages]]
<noinclude>


==Inserting a Chart==
[[Category:Presentation | 5]]
To insert a chart into a Print Template, use the platform <tt>CHART</tt> tag in an HTML <tt>img</tt> element.
[[Category:Tutorials]]
 
</noinclude>
;Format:
:<tt><nowiki><img src="CHART</nowiki></tt><u><b>{$</b></u>''report_id''<u><b>}</b></u><tt>"></tt>
:<tt><nowiki><img src="CHART</nowiki></tt><u><b>{$</b></u>''report_id'', <u>chart-title=</u>''Your Title'', ''field1''<u>=</u>''value'', ...<u><b>}</b></u><tt>"></tt>
where:
:* '''report_id -''' A required argument that gives the ID of the report that generates the chart. 
:* '''chart-title -''' An optional argument containing text for a chart title.
:* '''field1 -''' An optional record field. Only records with a matching value will be included in the generated chart. Up to three fields and values can be specified in the comma-separated list. (More can be specified, but only the first three are used.)
 
{{Important|The braces <u>{</u>...<u>}</u> and other underlined characters in this syntax are ''literals''. Type them in exactly as shown.}}
 
;Result:
:A URL for the generated chart is inserted into the generated page. When viewed, the chart image is displayed.
 
'''Example #1 -''' Inserting a chart with no optional arguments:
:<tt><nowiki><img src="CHART{c5cc43653b1b49db8142bc844735c209}"></nowiki></tt>
 
'''Example #2 -''' Inserting a chart of Orders taken by the owner of the current record:
:<tt><nowiki><img src="CHART{c5cc43653b1b49db8142bc844735c209, chart-title=Orders by $Order.owner.full_name, owner_id=$Order.owner.id}"></nowiki></tt>

Latest revision as of 02:27, 11 September 2013

Creating the Template Page

  1. Start by creating a JSP/Html Page in the platform.
    Turn off the Include Headers option for that page.
    (Those headers contain platform-specific Javascript that interfere with template processing.)
  2. Edit the page either in the platform, or using the Eclipse Plug-In.
  3. Add boilerplate text and fixed graphics
  4. Use JSP code and the platform APIs to control page content, as explained below.

Accessing Record Information

When a JSP Page is launched from a context that is associated with a particular object record, the request object available in the JSP page contains the identifiers needed to obtain additional information from the record, using either the REST record Resource or the Java record handling APIs.

To get all of the parameters available in the request object, and their values:

<%
  String[] params = request.getParameterValues();
  for (int i=0; i<params.length; i++)
  {
    String paramName = params[i];
    String paramValue = request.getParameter( paramName );
  }
%>

To obtain a record identifier from a request object sent by the platform:

With the object ID and record ID, use the getRecord API to retrieve the record.
<%
  String object_id = request.getParameter("object_id");
  String record_id = request.getParameter("record_id");
%>

Notepad.png

Note: Although the object_id is alphanumeric, it can be used in any API that requires an object name.

To obtain a record identifier from a request object sent by a Custom Action button:

A Custom Action button can be applied to one record, or it can be applied to several records at one time if the user took advantage of the More Actions feature in the record list view.
This code gets the record IDs and uses the searchRecords API to retrieve the records:
<%
  // Get the object ID and the comma separated list of record IDs
  String object_id = request.getParameter("object_id");
  String selectedRecords = request.getParameter("selectedRecords"); 

  // Break the comma-separated list into record IDs. 
  // Join them with "OR" operands for use when searching for the records
  String filterCriteria = "";
  if (selectedRecords != null)
  {
    StringTokenizer st = new StringTokenizer(selectedRecords,",");
    while (st.hasMoreTokens())
    {
      if ( !"".equals(filterCriteria.trim()))
      {
         // Criteria string isn't empty, and we're adding another expression
         // Prefix the new expression with a boolean OR operator
         filterCriteria += " OR "
      }
      filterCriteria += "record_id = "+ st.nextToken();
    }
  }

  // Use the filter criteria to fetch the selected records
  // Here, we ask for the record_id and name fields
  Result results;
  results = Functions.searchRecords(object_id , "record_id,name", filterCriteria);
  int resultCode = results.getCode();
  if (resultCode < 0)
  {
     // Error occurred
  }
  else if (resultCode == 0)
  {
    // No records found. (This situation should never occur.)
  }
  else
  {
    // Records retrieved successfully
    // Process them here
    ParametersIterator iterator = results.getIterator();
    while(iterator.hasNext())
    {
      Parameters params = iterator.next();
      String recordID = params.get("record_id");
      String recordName = params.get("name");
      // Take additional action according to your business logic
    }
  }
%>

Processing Related Records

Use the Java searchRecords API on the Related Object, selecting all records whose lookup-field value matches the current record ID.

For example, when the ClaimItems object has a lookup to Claims. When processing a Claim with record ID 1234, search for ClaimItems records who have claim lookup field with that value.

Using APIs

In general, the Java APIs and REST APIs provide equivalent functionality. But there are some differences, as well. So while the Java APIs are generally more convenient to use in a JSP page, you may also want to review the functionality listed in the REST API CheatSheet.

The following Java API classes are implicitly imported into JSP Pages:

Other considerations:

  • You cannot make a database connection. (Use the Java Record Handling APIs, instead.)
  • The maximum number of loop iterations, along with other restrictions, is determined by the configuration of the platform Governors.)

Learn More