Microsoft InfoPath 2010
The official blog of the Microsoft InfoPath team

Firing rules when removing InfoPath controls

Firing rules when removing InfoPath controls

  • Comments 1

The built-in support InfoPath offers for rules can be used to generate a relatively large and powerful set of conditions that trigger a rule action. There are some scenarios though were the default options available through the condition builder may not be sufficient. One of them is firing a rule when certain control has been deleted.

The above limitation can be worked around by writing form code. However in some cases it may be possible to achieve this scenario only by using the declarative logic InfoPath provides through rules. The pre-condition for this is to have a document schema that allows for some extra helper nodes.

The workaround uses two extra fields for each control on which we want to have a rule triggered on delete. The fields are simply counters of the old/new number of controls. One of them is bound through an XPath to the current number of the controls (i.e. repeating table rows) we are interesting in. For example, in the attached sample I want to know when a row in a repeating table has been deleted. The XPath expression is simply a count like:

count(/my:myFields/my:group3/my:group4)

The second field will keep track of the existing (“old”) number of the controls. We need to set a default value on it corresponding to the number of controls existing when the form is opened. There are two rules applied to the first field to keep the values in sync:

The first one is used to perform the main action and it fires when the “new” counter is less than the old. The second rule updates the value of the old counter.

Using the above procedure looks a little bit hacky but it works well for InfoPath 2003/2007. Note that the approach will work for browser-enabled forms also. The sample form template is attached; download XSN to your desktop, then right-click it and select "Open in Design Mode".

Silviu Ifrim
Software Design Engineer

Attachment: RulesOnDeletionSample.xsn
Leave a Comment
  • Please add 8 and 6 and type the answer here:
  • Post
  • This is a somewhat hacky, but definitely worth reading! ...

Page 1 of 1 (1 items)