Passing Parameters to the Report

Many of today's reports have parameters defined in them. This allows reports to be dynamic and adaptable. When reports are scheduled, any parameters defined in the report need to have a value unless a default value is already defined. Like database credentials, parameter values can be manually typed in from the Crystal Management Console on the Process tab. However, I describe how to accomplish this programmatically here.

From the IReport interface, call the getReportParameters method. This returns a collection of IReportParameter objects. There is one object in the collection per parameter values required for the report. After the IReportParameter interface is obtained, there is a getCurrentValues method that needs to be called. This returns an IReportParameterValues interface. There are two types of parameter values that can be added via this interface: a single value or a range value. As the name implies, a single value is an individual parameter value.

A range value, on the other hand, is two values that form upper and lower bounds. Range parameters are common in date ranges. For this example, assume a single value is being added. This is done by calling the addSingleValue method. This returns yet another interface called IReportParameterSingleValue. To set that actual value, call the setValue method.

NOTE

Even though the argument to the setValue method is of type String, this does not mean that other types of parameter values can't be set. When setting other parameter types, simply send in a string version of their value. For example, the string “100” can be passed in for a numeric parameter value or the string “True” could be passed in for a Boolean value.


The following code snippet schedules a report passing in the value "Canada" for the first parameter defined in the report:

IReport report = (IReport) infoObject;
List params = report.getReportParameters();
IReportParameter param = (IReportParameter) params.get(0);
IReportParameterValues vals = param.getCurrentValues();
IReportParameterSingleValue val = vals.addSingleValue();
val.setValue("Canada");
ISchedulingInfo sched = infoObject.getSchedulingInfo();
iStore.schedule(results);

NOTE

Parameters are stored in the report parameters collection in the same order that they are shown in the Crystal Reports field explorer.


The code example above hard-coded the value but a more generic approach might be to prompt users to enter a value when they create the scheduled job. Sometimes users cannot remember the exact spelling or value they should use for a parameter. Along these lines, the IReportParameter interface exposes a collection of pick list values that were created at report design time. This makes it easy for a developer to provide a list of values for the end user. Listing 35.1 provides a JSP page that fills a drop-down box with pick list values for a parameter.

Listing 35.1. Prompting for a Parameter
<%@ page import="java.util.*,
                 com.crystaldecisions.sdk.framework.*,
                 com.crystaldecisions.sdk.occa.infostore.*,
                 com.crystaldecisions.sdk.plugin.desktop.common.*,
                 com.crystaldecisions.sdk.plugin.desktop.report.*" %>

<html>
<body>
<form method=POST action=schedule.jsp>
Select a country to use for the scheduled report:
<select name=country>
<%

ISessionMgr sessMgr = CrystalEnterprise.getSessionMgr();
IEnterpriseSession ceSession = sessMgr.logon("Ryan", "123", "CMS_NAME",
                             "secEnterprise");
IInfoStore iStore = (IInfoStore) ceSession.getService("InfoStore");
IInfoObjects results = iStore.query("SELECT * FROM CI_INFOOBJECTS WHERE " +
    "SI_NAME='Parameter Report' AND SI_INSTANCE=0");
IInfoObject infoObject = (IInfoObject) results.get(0);

IReport report = (IReport) infoObject;
List params = report.getReportParameters();
IReportParameter param = (IReportParameter) params.get(0);
IReportParameterValues vals = param.getDefaultValues();
for (int i=0; i<vals.size(); i++) {
    IReportParameterSingleValue val = (IReportParameterSingleValue) vals.get(i);
    out.println("<option value=" + val.getValue() + ">" +
             val.getValue() +"</option>");
}
ceSession.logoff();
%>
</select>
<input type=submit value=Select>
</form>
</body>
</html>

Figure 35.5 shows the output of this page.

Figure 35.5. Prompting for a parameter before scheduling.


Selection Formula

Most reports have some kind of record selection formula used in the report processing to reduce the number of records returned to the report. This is a key factor in maximizing report performance. The selection formula often needs to be dynamic. It can be set programmatically via the IReport interface. There are two methods for doing this: setRecordFormula and setGroupFormula. These set the record selection formula and group selection formula, respectively. These methods accept a string argument, which is the formula expressed in the Crystal Reports formula language syntax. The following example schedules a report with a modified record selection formula:

IReport report = (IReport) infoObject;
report.setRecordFormula("{Products.Category} = 'Widgets' AND + {Product.Status}='In Stock'");
ISchedulingInfo sched = infoObject.getSchedulingInfo();
iStore.schedule(results);

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.73.175