Thursday, May 30, 2013

Showing/Hiding Fields using PeopleCode

When developing pages in PeopleTools, there are times when you might want to conditionally show or hide a group of fields based on some logic.  It is actually very simple to do - here are the steps:

Note:  This works best if you think of your page as one long, single column, with fields grouped all the way horizontally across the screen.  If you have multiple "columns" of content across the screen, PeopleTools may not be able to adjust the layout appropriately.

For our example, let's suppose that you have a page with a group box containing employee info fields, as well as a button that you'd like to use to show or hide the fields.  Also, when the group box is hidden, we want the layout to automatically adjust itself to fill in the white space where the group box used to be.

My Info Group Box Expanded


My Info Group Box Hidden and Layout Adjusted


To accomplish this, perform the following steps:

Set Page Properties

  1. Right-click on the Page and choose "Page Properties".
  2. On the "Use" tab, check the box for "Adjust Layout for Hidden Fields".

Set Group Box Properties

  1. Right-click on the Group Box and choose "Page Field Properties".
  2. On the "Record" tab, ensure that the Group Box has a Record and Field name associated with it.  It is usually best to associate it with a Work Record:

  3. On the "Label" tab, check the box for "Adjust Layout for Hidden Fields".
  4. On the "Use" tab, check the box for "Hide all Fields when Group Box Hidden".

Add "Show/Hide" Button PeopleCode

  1. Right-click on your button and choose "View Record PeopleCode".
  2. Choose the "FieldEdit" event and add the following PeopleCode:
If MY_WORK_RECORD.GROUP_BOX_1.Visible = True Then
  MY_WORK_RECORD.GROUP_BOX_1.Visible = False;
Else
  MY_WORK_RECORD.GROUP_BOX_1.Visible = True;
End-If;

Automatically Hide on Page Load

If you'd like for the group box to default to being hidden when the page first loads, you can add Page Activate PeopleCode to accomplish this.
  1. Right-click on the Page and choose "View Page PeopleCode".
  2. You will default to the "Activate" event.  Add the following PeopleCode:
MY_WORK_RECORD.GROUP_BOX_1.Visible = False;

Summary

That's it! In just a few easy steps, we've created the ability to toggle the visibility of a group of fields. This technique can be expanded upon to conditionally show or hide groups of fields depending on a variety of conditions.