DEV Community

Peter + AI
Peter + AI

Posted on

Mastering Uniface numgen: Generate Unique Sequence Numbers Like a Pro πŸš€

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

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

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

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)