Accessing Related Fields in PeopleCode
By Chris Malek | Tue, Oct 12, 2010
In PeopleTools Application Designer Page development, there is a concept of a “Display Control Field” and a “Related Display field.” For example, You have an employee’s department code of 45923 on a page and you want to display the department description on the page along side it. On the page field properties of JOB.DEPTID, you can make the field a “display control field.” You can then place the DEPT_TBL.DESCR field on the page as a related field to the JOB.DEPTID field.
There are times where you need to get access to this related display in code. For example, lets say you are generating an automated email with some of the page detail in the email body and you want to display the Department Description along with the code.
One approach would be to get the JOB.DEPTID from the buffer and then do a SQLEXEC or similar look-up on the DEPT_TBL. This works but is creates another redundant call to the database. The component processor has already looked up the description from the database and it is in memory in the component buffer. Here is how to get access to it in the code.
For this example assume that the JOB record is at level zero in the component.
Here is a “one liner” which is my preference as you need to declare less variables.
LOCAL string &deptDescr; &deptDescr = getlevel0()(1).JOB.DEPTID.GetRelated(DEPT_TBL.DESCR).VALUE;
Here is a more verbose version of the same code broken down.
local field &fldDeptid; &fldDeptid = getlevel0()(1).JOB.DEPTID; local field &fldRelatedDeptDescr; &fldRelatedDeptDescr = &fldDeptid.getRelated(DEPT_TBL.DESCR); local string &deptDescr; &deptDescr = &fldRelatedDeptDescr.value;
What I really don’t like about this code is that you have to hard code the record field reference in the code. So if someone changes the related display on the page do something like DEPT_EFF_VW instead of DEPT_TBL this code will be broken. Unfortunately, there is no way in the current PeopleCode Field API to “ask” for a list of related fields.
You can pass dynamic references to the getRelated method. For example lets say you looked up the related field from the PeopleTools tables or you had it in a setup table by passing the related record dot field reference as a string prefaced by the @ symbol.
Local String &relatedField; &relatedField = "DEPT_TBL.DESCR"; LOCAL string &deptDescr; &deptDescr = getlevel0()(1).JOB.DEPTID.GetRelated(@&relatedField).VALUE