PeopleCode

Read a File Using File Layout

A sample code to read a file using file layouts and insert data read into a record.

Local File &MYFILE;
Local Record &REC;
Local array of string &ARRAY;
Local string &FILE_DIRECTORY, &FileName;

&FileName = "MY_FILE_NAME.txt";
&FILE_DIRTORY = "/MYDIRECTORY/"
/*open file for reading*/
&MYFILE = GetFile(&FILE_DIRECTORY | &FileName, "R", %FilePath_Absolute);
/*create record object*/
&REC = CreateRecord(Record.MY_RECORD);
&ARRAY = CreateArrayRept("", 0);
/*check if file is open*/
If &MYFILE.IsOpen Then

Check for User Role

A very handy PeopleCode function to check for a user role(s) and perform specific actions depending on the value returned.

Function RoleToMatch(&RoleToMatch) Returns boolean;
   
   &bFound = False;
/*Returns an array containing all roles associated to the current user*/
   &aRoles = %Roles;
/*Loop through the array*/
   For &I = 1 To &aRoles.Len
      &RoleName = &aRoles.Shift();
      If Upper(LTrim(RTrim(&RoleName))) = Upper(LTrim(RTrim(&RoleToMatch))) Then
         &bFound = True;
         Break;
      End-If;
   End-For;
   
   Return &bFound;
   

Absolute Value and Date Functions

Here is a quick PeopleCode snippet to check the difference between two dates.

&date = Abs(Days365(&yourDate, %Date));

Use the Abs PeopleCode function to return a decimal value equal to the absolute value. This is very useful if you are validating that a date must be within X days before or X days after sysdate.
/* notice since we are using the Abs function, we don't need to code for &date < 90 */
IF &date > 90 Then
   /*your error message*/
End-IF;

How to Add Items to a Drop Down List?

The PeopleCode below will do couple things. First, it will restrict grid on a page to show only rows of address type "HOME". Second, allow users to only be able to add a "Home" mailing address by restricting the values in the drop down list to show only "Home" by using the ClearDropDownList and AddDropDownItem properties.

/* Hide Rows on the grid for any thing that is not of Address Type "HOME" */
FOR &i = 1 TO &Level1.ActiveRowCount
   &AddrType = FetchValue(Scroll.PERSON_ADDRESS, &i, PERSON_ADDRESS.ADDRESS_TYPE);
   IF &AddrType <> "HOME" Then

Hide Fields on PeopleSoft Page for Some Users

Sometimes you would like to hide fields from some users and not others. The only way that I know of is by creating a new role and assigns it to users who should not be seeing the fields.

This is how to go about doing that:

%EffDtCheck PeopleCode Built-in Function

Effective dated quires are used often in PeopleSoft and a great build-in function that will save you time is the %EffDtCheck. Lets take a closer look!

Syntax

%EffDtCheck(recordname [correlation_id1], correlation_id2, as_of_date)

The %EffDtCheck construct expands into an effective date subquery suitable for a Where clause.

IsAlpha and IsAlphaNumeric Built-in Functions

IsAlpha

Use the IsAlpha function to determine if
String contains only textual characters. It excludes all punctuation,
numerics, spaces and control codes.

The function returns a
boolean value: true if the string contains only alphabetic and textual
characters, false if it contains any umbers, punctuation or spaces.

AddToTime PeopleCode Built-in Function

You can use the AddToTime function to add hours, minutes, and/or
seconds to time. This function returns the result as a Time value. To
subtract from time, you can simply use negative numbers. The resulting
value
is always adjusted such that it represents an hour less than 24 (example: 18:45)

Syntax
AddToTime(time, hours, minutes, seconds)

So, how does it look like using an example?

%CurrentDateIn Meta-SQL Function

A very useful PeopleCode Meta-SQL function. I will be providing an
example and how %CurrentDateIn function will be resolved at run time.
But first, this is how PeopleSoft explains it:

The %CurrentDateIn meta-SQL variable expands to a platform-specific SQL
substring representing the current date in the Where clause of a SQL
Select or Update statement, or when the current date is passed in an
Insert statement.

And here is an example:

Hide Add Button if a Row Exists on the Grid

I have been working a lot with Grids lately and this is an example on how to hide an "Add" button under the grid so that users can only enter one row.

The comments between the code should explain what I'm trying to do here. But please, don't hesitate to ask if need be!

Local Rowset &RSEthnic;