Performing complex calculations
You can use Report Text Fields and scripts to make more complex calculations than those provided by the Report Calculated Field. The following illustration shows the same report as in the previous section, but with a new column showing commissions as a percentage of salary. In the footer section of the field break, the average commission rate for each job type is shown, and in the footer section of the iterator, the average commission rate for all employees is shown.
To add this column, follow these steps:
1. Add the following fields to the report iterator:
a. A Report Text Field with the text Average % to the header section.
b. A Report Text Field to the body section. (Make no changes to it for now.)
c. A Report Calculated Field to the footer section of the field break.
d. A Report Calculated Field to the footer section.
3. Open the settings for the two Report Calculated Field parts and change the displayAttribute property to average.
4. Switch to the Script Editor and write the following script:
commissionAsPercentage
"Calculates the commission as percentage of salary"
((self subpartNamed: 'COMMField') object = nil) ifTrue: [^0].
^(((self subpartNamed: 'COMMField') object)
asDecimal) /
(((self subpartNamed: 'SALARYField') object)
asDecimal).
5. Switch back to the Composition Editor and connect the formatted event of the [COMM] field to the script.
6. Connect the normalResult attribute of the script to the object attribute of the new text field in the body section of the iterator.
Note:
Notice that the field changes to [normalResult].
7. Now make two connections from the [normalResult] field:
a. Its formatted event to the countObject action of the Report Calculated Field in the footer section of the field break.
b. Its formatted event to the countObject action of the Report Calculated Field in the footer section of the iterator.
8. Change the converter for the normalResult text field, and the two average] fields to decimal, with a decimalPlace value of 2.
Your report should appear similar to the following in the Composition Editor.
When you test your report and select Preview from the Reports menu of the Report Tester, it should look very similar to the example at the beginning of this section.
Last modified date: 08/17/2018