The Crystal Reports offers a very user-friendly and handy feature in the form of Custom Function. The custom function fosters reusability of computation that would otherwise require multiple formula to handle this situation within the Report. Sometimes there is a scenario that requires one to display dynamic values for which the use of Report formula would lead to complexity. Under these circumstances, the right approach would be to utilize the custom function. Custom functions are procedures that can be created in Crystal Reports to evaluate, process and generate results. When a custom function is used in a formula, all the operations within its definition are performed without the explicit need to specify logic in the formula itself. Thus the custom functions provides a way for you to reuse and share the logic within multiple formula that saves the time and effort on repetitive formulation and better logic integrity can be ensured as everything is properly formulated in a single custom function instead of multiple formula.
Custom function can be added to the Report from Crystal Repository, an exising formula or Report Custom Function. A custom function can be included in a Report formula directly as a part of formula logic in the Formula Editor or it can be chosen to supply formula logic through the Extractor in the Formula Expert.
In order to create custom function, the following action needs to be performed:
In order to create formula that use the custom function, the following action needs to be performed:
The definition of custom function and two formula created thereafter has been illustrated in the screen capture below:
Custom Function (Process Interval)
// Process_Interval
Function (datetimevar in_time,datetimevar out_time)
(
NumberVar interval;
NumberVar hrs;
NumberVar min;
NumberVar sec;
StringVar hhmmss;interval := datediff("s",in_time,out_time); //get the seconds between in_time and out_time
hrs := Truncate(Truncate(interval/60)/60); //get the interval in hours between in_time and out_time
min := Remainder(Truncate(interval/60),60); //get the minutes in hours between in_time and out_time
sec := Remainder(interval,60); //get the interval in seconds between in_time and out_timehhmmss := totext(hrs,"00") + ":" + totext(min,"00") + ":" + totext(sec,"00"); //concatenate into total time
);
Formula [ (Interval 1), (Interval 2) ]
// Interval 1
Process_Interval ({EMPLOYEE_WORK.IN_TIME_1},{EMPLOYEE_WORK.OUT_TIME_1});
----------------------------------------------------------------------------------------------------------------------------------
// Interval 2
Process_Interval ({EMPLOYEE_WORK.IN_TIME_2},{EMPLOYEE_WORK.OUT_TIME_2});
In the below illustration that consists of the Preview tab of the Report achieved on the Refresh, the values under Column Header 'Work Time (Mon)' represents the outcome of the first formula containing the custom function created in the beginning. The values under Column Header 'Work Time (Tue)' represents the outcome of the second formula containing the custom function created in the beginning.
Thus, it can be realised that a calculation from a Custom Function can be reused in multiple formula. In the process, it is possible to save the time and effort by avoiding duplication in designing multiple formula and its unnecessary maintenance.
User | Count |
---|---|
6 | |
6 | |
5 | |
4 | |
3 | |
3 | |
3 | |
3 | |
3 | |
2 |