🚀 What is the Compare Statement?
If you're working with Uniface applications, you've probably encountered situations where you need to compare data between adjacent records. The compare
statement is your go-to tool for this task! 💪
The compare
statement allows you to compare fields between the current occurrence and either the previous or next occurrence in your dataset. It's particularly useful for reporting scenarios where you need to detect changes or group similar records.
📝 Basic Syntax
compare{/previous | /next} (FieldList) {from Entity}
🔧 Key Components:
- /previous - Compare with the previous occurrence
- /next - Compare with the next occurrence (default behavior)
- FieldList - Comma-separated list of field names to compare
- Entity - Optional entity name (uses current entity if omitted)
🎯 Practical Example
Let's look at a real-world example from invoice processing:
trigger leavePrinted
compare/next (INVDATE) from "INVOICE"
if ($result <= 0)
printbreak "SUBTOTAL"
if ($result = 0)
eject
printbreak "TITLE"
endif
endif
ends; leavePrinted
This code compares the invoice date of the current record with the next one. If they're different, it triggers a subtotal break in the report! 📊
🔍 Understanding Return Values
$status Values:
Value | Description |
---|---|
-1 | ❌ Error occurred |
0 | ✅ No error |
$result Values:
Value | Meaning |
---|---|
1 | 🎯 Perfect match |
0 | ❌ Fields don't match |
-1 | 🚫 No previous/next occurrence |
💡 Pro Tips
- Always use /next explicitly for better code clarity 📖
- The statement formats data before comparing - no need to worry about data type mismatches! 🔄
- Can be used in all component types - very flexible! 🌟
- Check $procerror for detailed error information when $status = -1 🔍
⚠️ Common Pitfalls
- Don't enclose field names in quotes
- Don't qualify field names with entity.model format
- Remember that parentheses are optional for single fields
🎉 Conclusion
The compare
statement is a powerful tool for data comparison in Uniface applications. Whether you're building reports, implementing business logic, or processing data transformations, understanding this statement will make your development life much easier! 🚀
Have you used the compare statement in your projects? Share your experiences in the comments below! 💬
📚 This article is based on the official Uniface Documentation 10.4 and was created with AI assistance to help developers better understand this important Uniface feature.