Using Record Objects as Parameters and Returns

By Chris Malek | Wed, Oct 13, 2010

There are often times where I would like to create a PeopleCode function with optional parameters. PeopleCode does not support optional parameters like more advanced languages (i.e. Ruby as one of many examples). One work around for this is to use a record object as the function parameters.

The function declaration might look something like this rough sketch:

Function MyFunction(&inRec as record)

If NONE(&inRec.FIELD3.VALUE) THEN
   &Param3 = "defaultValue";
else
   &Param3 = "defaultValue";
END-IF;
/* do some important work with &inRec field values and &param3 */
end-function;

Then to call the function it might look like this:

Local Record &rec = CreateRecord(record.MY_FUNC_PARAM)
&rec.FIELD1.VALUE = "123";
&rec.FIELD2.VALUE = "ABC";
MyFunction(&rec);

In this case we did not set a value of FIELD3. The function implementation looks for the optional value and defaults it to something if not provided.

Another benefit of using record objects as parameters, is that you can later add fields to the record object which represent new parameters and any calling client code in your production database will not have to be updated. Of course, the functional implementation will need to have some “smarts” to default the value to something if it was not passed until all the clients of the function have been changed.

You can also use record objects as a return to return more than one value from a function since Peoplecode does not support multiple return values like Ruby does.

The function implementation might look something roughly like this:

Function MyFunction2() returns record

  local record &rec = createrecord(record.MY_RETURN_REC);
   /* Do some important stuff */
   &rec.FIELD1.VALUE = "123";
   &rec.FIELD2.VALUE = "ABC";
   &rec.FIELD3.VALUE = "XYZ";
   return &rec;

end-function;

Of course, if you start using application Packages you can get around many of these limitations using Class Instance variables. However, I will save that for another discussion.

Article Categories