Hey fellow developers! ๐ Today I want to share some insights about one of the most powerful loop constructs in Uniface 10.4: the forentity statement. This feature is essential for processing entity occurrences efficiently, and I'll show you exactly how to leverage it in your applications.
Note: This article is based on the official Uniface Documentation 10.4, with assistance from AI to structure and present the information clearly.
๐ฏ What is forentity?
The forentity
statement defines a loop that processes all occurrences of a specified entity. It's incredibly useful when you need to iterate through datasets and perform operations on each record.
๐ Basic Syntax
forentity EntityName Your ProcScript endfor
๐ง Parameters
Parameter | Data Type | Description |
---|---|---|
EntityName | String | Entity name; can be a string, or a field, variable, function, or parameter that evaluates to a string |
โก How It Works
The forentity
loop processes each occurrence in the hitlist, automatically changing the value of $curocc
with each iteration. The loop continues until one of these conditions is met:
- ๐ No more occurrences to process (
$curocc
is larger than the last occurrence) - ๐ญ No occurrences of the entity (
$empty
is true) - ๐ A
break
statement is encountered
โ ๏ธ Important Gotcha!
Be careful when using statements like remocc
and discard
inside forentity
blocks! These statements also modify $curocc
, which can cause your loop to skip occurrences. Here's why:
trigger detail ; of field DELETE forentity "ENTITY1" remocc ; This increases $curocc by 1 endfor ; This also increases $curocc by 1 end; detail
The result? Every second occurrence gets skipped! ๐ฑ
๐ก Practical Examples
๐ต๏ธ Example 1: Finding a Specific Record
Let's say you want to find a person named "Donald Duck" in your PERSON.ORG entity:
variables numeric vLoops string vFullName endvariables retrieve/e "PERSON.ORG" vLoops = 0 forentity "PERSON.ORG" vLoops += 1 vFullName = FULLNAME.PERSON.ORG if (vFullName = "Donald Duck") putmess "Loop processing stopped on Name: %%vFullName Loop count: %%vLoops" break endif putmess "Processing %%vFullName, Loop count: %%vLoops " endfor
This will output:
Processing Bruce Banner, Loop count: 1 Processing Lois Lane, Loop count: 2 Processing Bruce Wayne, Loop count: 3 Processing Clark Kent, count: 4 Loop processing stopped on Name: Donald Duck, Loop count: 5
๐งฎ Example 2: Calculating Totals
Here's how you can calculate order totals by looping through order lines:
trigger preSerialize TOTAL.ORDERS = 0 ;initialize field value forentity "ORDERLINE" ;loop through each ORDERLINE entity LINE_TOTAL.ORDERLINE = UNIT_PRICE.ORDERLINE * QUANTITY.ORDERLINE ;calculate the LINE_TOTAL TOTAL.ORDERS += LINE_TOTAL.ORDERLINE ;calculate the TOTAL endfor end; preSerialize
๐ Error Handling
Always check for errors! The forentity
statement can return error code -1102 (UPROCERR_ENTITY) when the EntityName is invalid or the entity isn't in the component structure.
๐ Final Thoughts
The forentity
loop is a powerful tool in your Uniface arsenal. It's perfect for data processing, calculations, and search operations. Just remember to be cautious with statements that modify $curocc
to avoid unexpected behavior!
Have you used forentity
in your projects? Share your experiences in the comments below! ๐ฌ
Happy coding! ๐
Top comments (0)