Undocumented Useful PeopleCode Functions

Those of us that have been working on PeopleSoft for a while know that we occasionally stumble across undocumented features and functions.

Logical Operators are one of the fundamental building blocks of computer programming languages.  Evaluating if a predicate (or expression) is logically false or true determines program flow and results in correct and predictable behaviour.  The 3 Logical Operators in PeopleCode (sometimes called logical connectives) are AND, OR and NOT

In this example the Boolean typed variable &foo must be True and variable &bar must be False in order for this predicate to be True.  (In the Truth Table a False and False (i.e. Not False) always returns True).
   If &foo And
         Not &bar Then
         
         /* do something... */

   End-If;

I've been working on a software project recently and I stumbled upon a new logical operator in PeopleCode.

I found the Maybe operator.

The beauty of this operator is that the evaluation of a Boolean using the Maybe operator might return a False or True but you're never quite sure which.   In this example &foo must be True and variable &bar could be False or True.  The result of the Maybe operator may result in True or may result in False.
   If &foo And
         Maybe &bar Then
         
         /* do something... */

   End-If;

This simple device can save us writing loads of quite complex code and that makes it a very powerful operator indeed.

There are many other useful shorthand functions we can use for evaluating field values and I'm particularly fond of  the functions AllNoneAllOrNoneOnlyOne and OnlyOneOrNone.  You can read all about there here in the PeopleSoft Online Documentation.

To check if a field has a value we could write
  If &foo <> " " Then
      
  End-If;

or we could simplify this with...
  If All(&foo) Then
      
  End-If;

This is particularly useful if you need to evaluate more than one field. So the following...
   If &foo <> " " And
         &bar <> " " And
         &dee <> " " Then
      
      /* All fields must have a non empty value */
      
   End-If

would become a nice tidy....
   If All(&foo, &bar, &dee) Then

      /* All fields must have a non empty value */
      
   End-If;


And our newly discovered Maybe operator also works as a Function too.  We can therefore return True or Maybe False if &foo has a value.
   If Maybe(&foo) Then
         
         /* do something... */

   End-If;

Happy coding. And if you too come across any hidden functions please let me know.

Comments

Ricky said…
Thanks, never saw that before.

Now:
If &foo And
Maybe &bar Then

/* do something... */

End-If;

Is it safe to say that Maybe &bar is irrelevant? Or is it just ensuring it is false or true versus null? So simply validating the &bar has a value.

Jim Marion said…
@Ricky, Maybe ;)
Greg said…
Sounds like something posited by Waldo and Magic inc.
or the foundation of AI.
Maybe it's 5 o'clock somewhere? ...
Ricky said…
@Jim, hilarious! Wondering "Maybe" this was Graham's April Fools joke...
DarkPadawan said…
Oh Man!!! I fall for it!!!! good one!