Working with Uniface and need to generate unique sequence numbers? The numgen
statement is your best friend! πͺ Let me walk you through everything you need to know about this powerful counter function. This post is based on the official Uniface documentation 10.4, and I had some AI assistance to structure this comprehensive guide.
What is numgen? π€
The numgen
statement is Uniface's built-in solution for incrementing counters and generating unique sequence numbers. It's perfect for creating invoice numbers, order IDs, or any sequential identifiers in your applications.
Syntax and Parameters π
numgen CounterName, Increment {, LibraryName}
Parameters Breakdown:
- CounterName (String): The name of your counter π·οΈ
- Increment (Number): How much to increment (range: -2,147,483,648 to 2,147,483,647) β
- LibraryName (String, Optional): Counter library name (defaults to SYSTEM_LIBRARY) π
Return Values and Status Codes π
After executing numgen
, you'll get:
- $result: Contains the new incremented number β¨
- $status: Indicates success or failure
Status Code Meanings:
Value | Meaning | Status |
---|---|---|
0 | Counter successfully incremented | β Success |
-1 | Counter out of range or not defined | β Error |
-2 | Increment out of range | β Error |
Practical Examples π‘
Example 1: Generating Invoice Numbers
operation exec
numgen "INVOICE_NUMBER", 1, "COUNTER_LIB"
if ($status < 0)
message "Error generating sequence number."
rollback "$UUU"
edit SEQNO
done
else
SEQNO = $result
commit "$UUU"
edit NAME
endif
end; exec
Example 2: Auto-generating IDs in Create Trigger
trigger: create
if ($rettype = 65)
creocc "INVOICE", $curocc + 1
else
creocc "INVOICE", $curocc
endif
numgen "INV_COUNT", 1, $variation
INV_NUM/init = $result
commit "$UUU"
end; create
Best Practices and Tips π―
1. Always Handle Errors
Check $status
after every numgen
call to ensure proper error handling π‘οΈ
2. Commit Immediately
Follow numgen
with commit "$UUU"
to avoid locking problems when multiple users access the same counter π
3. Use Descriptive Counter Names
Names like "INVOICE_NUMBER" or "ORDER_ID" make your code more maintainable π
4. Leverage Libraries
Use different libraries to organize counters by project or module ποΈ
Common Pitfalls to Avoid β οΈ
- Forgetting to commit: This can cause locking issues in multi-user environments
- Not checking $status: Always verify the operation succeeded
- Using in create triggers: Can result in lost sequence numbers if data isn't stored
Error Handling Reference π§
Common $procerror
values you might encounter:
- -2 to -12 (UIOSERR_*): Database I/O errors πΎ
- -16 to -30 (UNETERR_*): Network I/O errors π
- -1108 (UPROCERR_COUNTER): Repository access issues or undefined counter π«
- -1203 (UPROCERR_RANGE): Value out of range error π
Conclusion π
The numgen
statement is an essential tool for any Uniface developer working with sequential data. By following the best practices outlined above, you'll be able to generate unique sequence numbers reliably and efficiently in your applications.
Remember: always handle errors gracefully, commit your counters promptly, and use descriptive names for better code maintainability! π
Happy coding! π¨βπ»π©βπ»
Top comments (0)