By Chris Malek | 30 August 2011
Explaining the PeopleCode @ operator
Have you ever seen the PeopleCode @ operator and wondered when and why you would use it? Let’s look into it in more detail. The @ operator is used to tell the PeopleCode intepreter that you are passing in a string name for an object name.
First let’s look at how you normally create records passing an object name. Here we are going to create a record object based on PSOPRDEFN.
local record &recOPR; &recOPR = createRecord(RECORD.PSOPRDEFN);
In the above example we knew the record name at design time because we hardcoded “RECORD.PSOPRDEFN” in the code. What if we do not know the object name at design time? This could be that we pull the record name out of a setup table. Or you have a generic piece of code that other pieces of code call and pass you the object name. This is where the “@” operator comes in.
Let’s look at the same example above using a dynamic operator.
local string &recName; local record &recDynamic; &recName = "RECORD.PSOPRDEFN"; &recDymanic = createRecord(@&recName);
Notice the @ in the createRecord parameter. So imagine that the “&recName” value is maybe pulled from a setup table at run-time and substituted in.
One common mistake that is often made is to forget the object type name at the front. In the code sample above that would be the “RECORD.” piece.
Let’s look at a field example. Here we are going to dynamically get the ACCTLOCK field off the PSOPRDEFN record.
local record &recOPR; &recOPR = createRecord(RECORD.PSOPRDEFN); local string &fieldName ; &fieldName = "FIELD.ACCTLOCK"; local string &locked; &locked = &recOPR.getfield(@&fieldName).value;