Crystal Reports 10: The Complete Reference

A custom function is very similar to a regular function you will already find in the function tree of the Crystal Reports Formula Editor (learn more about formulas in general and the function tree in Chapter 5). For example, the built-in ToText function in the Crystal Reports formula language will convert from, for instance, a numerical data type to a text data type. The built-in Left function will return a certain number of characters from the left side of a string value. And the built-in Round function will round a numeric value to a specified number of decimal places.

But what if you have a special need in your company to use a function that s not included as part of the built-in Crystal Reports functions? You may, for example, need to calculate the number of business days between two dates, excluding weekends and company holidays. Or you may want to calculate a discount for customers in a large number of formulas. However, you want to base this discount on several factors, including the size of the order amount, the number of orders the customer placed last year, and the length of time they have been a customer. While you can most probably make use of Crystal Reports extensive formula language to create these special formulas, making use of the logic over and over again in more than one formula, or sharing the logic with other report designers, is where a custom function becomes really useful.

Available custom functions (either that you built after creating the report, or added from the repository) will appear in the Formula Editor just like built-in functions. However, custom functions appear in their own Custom Functions area of the function tree, as shown in Figure 6-1.

Figure 6-1: A custom function used in a formula

A custom function can accept parameters or arguments, just like a regular function (arguments are discussed in more detail later in the chapter). And as with regular functions, you can use the custom function over and over in as many formulas as you desire . If the core logic of your custom function changes (perhaps business holidays or a discount percentage tier change with the new year), changing the custom function in its one location will be reflected in all formulas that make use of that function ”you won t have to edit each and every formula to change the formula logic.

Категории