DEV Community

Peter + AI
Peter + AI

Posted on

🔄 Mastering the forentity Loop in Uniface 10.4: A Developer's Guide

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
Enter fullscreen mode Exit fullscreen mode

🔧 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

🧮 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
Enter fullscreen mode Exit fullscreen mode

🎉 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)