Thursday, November 8, 2012

Some Advanced InfoPath Rules

In this tutorial I will create a "smart" InfoPath form that will change its contents based on some scenarios.


> employees subscribe each month for some company benefit in the upcoming month.
> subscriptions are open from 24th till the end of the month otherwise closed.
> once an employee has subscribed it cannot subscribe again in that month (show instead a thank you page).


This can be achieved by creating a Site Workflow that registers the data into a SharePoint List. The Workflow form is customized with InfoPath. In this post I will insist on the form.


1. Have a SharePoint List to store subscriptions. Fields:

  • Person (Person type)
  • MonthTag (String type. It will contain "2012-11" like strings to identify the month when the subscription was made. This will allow easy grouping and filtering for later processing.)
  • SuperscriptionID (String type. Combination of Person Username and MonthTag to determine if a person already subscribed for that month. Its value is calculated by the workflow and filled in at creation. E.g.: "2012-11.Levente.Rog")
  • Any other fields that need input from the user.

2. A Site Workflow that creates the subscription (adds an entry in the above mentioned list). End users will Start the workflow itself (by giving them a link). This Workflow will have the following steps:

  • Generate the Month Tag (2012-11 like strings)
  • Generate SuperscriptionID (2012-11.Levente.Rog -like strings)
  • Create an Item in the Subscriptions List with the previously mentioned values. Using the Workflow Context:Initiator variable we know who is the logged in user so the end user does not have to fill out his own name in the form (that would be lame isn't?)
  • Send a confirmation email
Upon request I can create a tutorial explaining in greater detail covering these workflow steps.

Form Customization

The form of the Workflow is the one that I will customize now.
This is how I managed to solve this scenario:

What I did is I created separate sections for three scenarios (disregard the 201-12. section for now, I will explain that later).

1. First scenario is when registrations are open. Two conditions have to fulfill for this section to show up:

  • Date of month >= 24
  • User has not subscribed yet in the upcoming month

To achieve this, I created the following InfoPath Formatting rules with the action to Hide the section.

  • The expression: number(substring(xdDate:Today(), 9, 2)) < 24

  • Any occurence of Subscription ID is equal to concat(substring(addDays(today(), 30), 1, 7), ".", translate(userName(), "ABCDEFGHIJKLMNOPQRSTUVWYXZ", "abcdefghijklmnopqrstuvwyxz"))

    What I did with that function is:
    Generate the "Month Tag" that is a unique string each month. Since we subscribe for benefits for the next month we calculate one month (addDays 30) forward and crop the first 7 characters from the ISO Date to get a 2012-11-like key. We concatenate that with the lowercase username. This way we'll get something like:
    2012-11.levente.rog . We also record that same string in the SharePoint list as well. This way we know if that user already subscribed for that month.
    - Any occurrence of Subscription ID - we get that by creating a second connection to the same SharePoint List (Data Ribbon -> To SharePoint List). If any of those empty comes out true, the section will be hidden.

2. If the user has already subscribed show a thank you message.
One rule I have here, basically the negated version of rule #2 seen previously:

  • All occurences of Subscription ID are not equal to concat(substring(addDays(today(), 30), 1, 7), ".", translate(userName(), "ABCDEFGHIJKLMNOPQRSTUVWYXZ", "abcdefghijklmnopqrstuvwyxz"))

3. Show a registrations are closed message if date is <= 24th.
This is also a variation, negating the #1 rule used in the first scenario.

  • The expression: number(substring(xdDate:Today(), 9, 2)) >= 24

4. That dummy "2012-12" section is just a calculated value containing the Subscription ID generation formula. For some reason, when I haven't actually used this formula, it was not evaluated in the rule. This section is always hidden from the end user (by creating a dummy rule: myfield does not equal "asdf" => Hide).
  • Calculated Value XPath expression: concat(substring(xdDate:AddDays(xdDate:Today(), 30), 1, 7), ".", translate(xdUser:get-UserName(), "ABCDEFGHIJKLMNOPQRSTUVWYXZ", "abcdefghijklmnopqrstuvwyxz"))

That's it. Save and Publish.

The workflow is redirected to itself by changing the &Source= parameter in the URL the link to the workflow itself. For example:

After completing the workflow, it will be redirected to itself again, showing the same form. But since the user now is subscribed, the "Thank you" section will show up this time.

No comments:

Post a Comment