一些peoplecode小技巧【一】

时间:2023-03-09 09:23:43
一些peoplecode小技巧【一】

1. Get the description of the translate value:

No need to write SQLEXEC on PSXLATITEM passing fieldname and Field value. Just use LongTranslateValue property to get the long description of the field as: 

Local Any &VALUE; 
Local Field &MYFIELD;

&MYFIELD = GetField(); 
&VALUE = &MYFIELD.LongTranslateValue; 

If the field has a null value, a null string is returned. If the field isn’t based on a translate table, or the value isn’t in the translate table, the field’s current value is returned. Because the current value can be of any type, this property has a type of Any.

In the same way we can use the ShortTranslateValue property to return the short description of a translate value of a field.

2. Hide/Unhide the asterisk(*) on required fields:

You have made some field as required in record field property but don't want that asterisk to appear with field.

In this case just use the property ShowRequiredFieldCue and make that true or false to show or hide that asterisk (*, also known as required field cue). 

Local Field &MYFIELD; 
&MYFIELD.ShowRequiredFieldCue = False; 

One thing to be remembered here is that no cue is ever shown on a pushbuttons or display-only fields.

3. Getting or checking the field type in peoplecode:

If you wish to get the type of a field or want to write peoplecode and want to write the conditional statement based on the type of the field then the type property can be used there.

This property returns the type of field. And peoplecode may be written as: 

Local Field &MYFIELD;

If &MYFIELD.Type = "CHAR" Then 
--- processing logic --- 
Else 
--- processing logic --- 
End-If; 

The returned value from type property can be one of the following strings:

1. CHAR 
2. DATE 
3. DATETIME 
4. IMAGE (for static images) 
5. IMAGEREFERENCE 
6. LONGCHAR 
7. NUMBER 
8. SIGNEDNUMBER 
9. TIME

Note. Fields of type Attachment have a type of IMAGE. Remember that This property is read-only and you can only get the return value of type string.

4. Increment the key (Request ID/Transaction Number/....) and get from database:

Very common requirement, many times while saving a new transaction we run SQLEXEC on database, get the highest key value and increment it by one and save a new row with that key.

Why to run query and do this mathematics if we can do this by using GetNextNumber peoplecode functions?

Use GetNextNumber function to increment the value in a record for the field you specify by one and returns that value. If the new value generated exceeds max number which is specified as second parameter, a negative value (-1 or -2 or -3) is returned and the field value isn't incremented. 

&Val = GetNextNumber(RECORD.FIELD, 99999999); 

An evaluate can be written after that to error handling or default processing as: 

Evaluate &Val 
When = %GetNextNumber_SQLFailure 
-- do processing -- 
When = %GetNextNumber_TooBig 
-- do processing -- 
When = %GetNextNumber_NotFound 
-- do processing -- 
When-other 
-- do default processing using &Val -- 
End-Evaluate; 

5. Expand or collapse a groupbox/scroll using peoplecode:

This is required when we want to display or hide the fields in groupbox on page organized in different groups using hyperlink or button.

For this we can use DataAreaCollapsed property and functionality can be achieved by setting this property to true or false.

One thing to remeber is that you must set the group box as Collapsible Data Area on the page field properties for the group box in PeopleSoft Application Designer before you could this property.

To collapse (Hide): 
&TEST1.DataAreaCollapsed = True;

To expand (show): 
&TEST1.DataAreaCollapsed = False;

Here &TEST1 can be either a collapsible group box or scroll area.