Create an absence report form using rules
Use rules to implement logic for calculating leave balances.
Problem
You want to create an InfoPath form that can register and report on hours of leave entitlement, hours of leave taken, cumulative hours of leave taken, and hours of leave left (balance) without using code.
Solution
Use Rules to implement logic for calculating leave balances.
Discussion
You can accomplish this functionality as follows:
- Design an InfoPath form as shown in figure 1 with one Text Box control
called entitlement and a Repeating Table with five fields:
one Date Picker control called date and four Text Box
controls called hoursTaken, cumulative, balance,
and balanceDays, respectively. Your InfoPath form should resemble the form in figure 1.
Figure 1. The InfoPath form template in design mode. - Open the Data Source pane.
- Add a Field (element) called position of type Whole Number (integer) under the myFields node. This field will not be made visible on the form, but will only be used behind the scene.
- The Main data source of your form should now resemble the following figure:
Figure 2. The structure of the Main data source of the form template. - Double-click on the hoursTaken field to open its Properties dialog box.
- Click on the Rules... button and add a Rule with the following Action:
By counting all of the preceding siblings of the my:group2 node of the current context node, you can derive the position of the my:group2 node of the current context node in the repeating table. The calculated position is then saved in the position field to be used later in other rules.Set a field's value: position = count(ancestor::*[self::my:group2]/preceding-sibling::*) + 1 -
Add a second Rule that has the Condition:
and the Action:
position > 1This rule calculates the current cumulative value by adding the current hours taken to the cumulative value in the previous row.Set a field's value: cumulative = number(/my:myFields/my:group1/my:group2[/my:myFields/my:position - 1]/my:cumulative) + . -
Add a third Rule that has the Condition:
and the Action:
position <= 1This rule sets the cumulative value equal to the amount of hours taken if the context node is currently in the first row of the repeating table.Set a field's value: cumulative = . -
Add a fourth Rule that has the following two Actions:
and
Set a field's value: balance = ../../../my:entitlement - ../my:cumulativeWhere 7.5 is the amount of hours for one day of work.Set a field's value: balanceDays = ../my:balance div 7.5 - Make the cumulative, balance, and balanceDays fields Read-only by double-clicking on each, going to the Display tab on their Properties dialog box, and checking the Read-only check box.
You should now have a fully functional absence report form. Please note that if the hours taken value of previously filled in rows are changed afterwards, recalculation of rows that come before or after that row will not take place. For a total recalculation of all rows you will have to write code.
Related InfoPath Articles:
