A report script belongs to an individual report design. It runs when that report’s data and output are calculated — in the Administration, for a supervisor or in an automatic report. It is separate from the agent script and script code in a flow.
Expressions and calculated fields are sufficient for many calculations. Use a script when a calculation needs to react to an event or perform several steps. Some supplied templates already calculate values this way.
Checking the language and existing code
- Open the report design in the report designer. Designing requires the permissions described under Report designs.
- Select the report itself, for example through the Report Explorer.
- Check Script Language under Properties. The examples below use C# or Visual Basic; use only the version that matches the selected language.
- Switch to Scripts at the top right or press F6. Check whether the design already contains code before replacing anything.
The script language applies to the entire design. Changing it does not automatically translate existing code. The supplied Agent report base template contains Visual Basic code; retain its language setting with it.
Assigning events
A script function runs only after it is assigned to the corresponding event of an element. The function name alone does not establish that connection.
- On the Scripts page, select Control: and Event:.
- Alternatively, select an element, open its Scripts under Properties and choose (New) for the required event. The designer generates the appropriate function body.
- Use that body instead of copying parameters and names from a different event. Bands, labels and calculated fields have different tasks.
| Event | Use |
|---|---|
GetValue of a calculated field |
calculate a value for the current data row and return it through e.Value |
BeforePrint of an element or band |
prepare properties before the element becomes part of the report output |
An event can run repeatedly for many data rows or output elements. Do not assume a single call per report. A self-incrementing counter or a global variable is not a reliable substitute for the designer’s summaries.
Example: conversation time in minutes
This example adds conversation time in minutes to a list of conversations.
TotalConnectDuration contains seconds — see
Statistics fields.
- In the Field List, add a calculated field named
ConversationMinutesand assign it to the same query asTotalConnectDuration. - Do not add another calculation to its Expression. The event performs the calculation.
- Under Scripts › GetValue, create a function for this calculated field.
Keep the name generated by the designer; in this example it is
CalculateConversationMinutes. - Add the calculation using the version for your script language.
C#:
using System;
using DevExpress.XtraReports.UI;
private void CalculateConversationMinutes(object sender, GetValueEventArgs e)
{
const string durationColumn = "TotalConnectDuration";
const double secondsPerMinute = 60.0;
const int decimalPlaces = 1;
object duration = e.GetColumnValue(durationColumn);
if (duration is null || duration == DBNull.Value)
{
e.Value = default(double);
return;
}
e.Value = Math.Round(Convert.ToDouble(duration) / secondsPerMinute, decimalPlaces);
}
Visual Basic:
Private Sub CalculateConversationMinutes(ByVal sender As Object, ByVal e As DevExpress.XtraReports.UI.GetValueEventArgs)
Dim duration As Object = e.GetColumnValue("TotalConnectDuration")
If duration Is Nothing OrElse duration Is System.DBNull.Value Then
e.Value = 0.0
Return
End If
e.Value = System.Math.Round(System.Convert.ToDouble(duration) / 60.0, 1)
End Sub
- Drag the calculated field onto a label in the Detail band.
- Click Validate and open Preview for a period that contains data.
- Check a known row: 90 seconds produce 1.5 minutes; an empty database value produces 0. Then save the design.
The event assignment must use exactly the function’s name. If you rename a function in the code, update its assignment under Scripts › GetValue too.
Validation and troubleshooting
Validate checks the script code. Successful validation confirms compilation; it does not prove that the script runs correctly for every data row. Open Preview afterwards.
| Problem | What to check |
|---|---|
| Compilation error | Script language, spelling, brackets and the reported line; messages appear in the Report Design Analyzer. |
| Missing function | The event assignment names a function that is absent or has a different name in the code. |
| Code does not run | The element is assigned to the correct event and occurs in the output. An unassigned function has no effect. |
| Column not found | The technical field name and the calculated field’s query match; this example needs TotalConnectDuration. |
| Error for individual rows only | Check empty database values, data types and conversions; an empty value may be DBNull.Value. |
| Different results in an automatic report | Compare parameters, period, language setting and access to local files or programs. The automatic report runs on the server. |
The report script is saved with the design. For an automatic report, the required code must therefore be in the design being used; code from an agent script does not run with it.