DEV Community

Peter + AI
Peter + AI

Posted on

🌐 Understanding Uniface websetocc: Managing Entity Occurrences in Web Applications

This blog post was created with the assistance of AI to help explain complex Uniface concepts in a more accessible way.

If you're working with Uniface web applications, specifically Dynamic Server Pages (DSPs), you've probably encountered the websetocc statement. Today, let's dive into what it does, when to use it, and how it differs from the regular setocc statement! πŸš€

πŸ€” What is websetocc?

The websetocc statement is a specialized ProcScript command used in Dynamic Server Page components (DSPs). Its main purpose is to set the current entity and occurrence based on information received from the web client, rather than using position-based indexing.

Entity: Think of an entity as a table structure in your application - it defines what data fields are available.

Occurrence: An occurrence is like a specific row of data within that entity structure - it's the actual instance containing values.

Dynamic Server Page (DSP): A special type of Uniface component designed for web applications that can be updated dynamically at runtime.

⚑ How websetocc Differs from setocc

While both statements set the current occurrence, they work differently:

  • setocc: Uses position numbers in a hit list (like "go to row 5") πŸ“
  • websetocc: Uses entity names and occurrence IDs received from the web client (like "find the customer with ID 12345") πŸ”

This makes websetocc perfect for web environments where you need to identify specific data records across HTTP requests!

🎯 When Should You Use websetocc?

Use websetocc when:

  • Working with Dynamic Server Pages (DSPs) 🌐
  • Processing user interactions from web browsers πŸ’»
  • Managing entity occurrences in web applications πŸ“Š
  • Handling form submissions or AJAX requests πŸ“€

πŸ“ Syntax and Usage

The basic syntax is simple:

websetocc
Enter fullscreen mode Exit fullscreen mode

That's it! No parameters needed because websetocc automatically reads the entity information from the web request.

πŸ”§ Step-by-Step Implementation

Here's a typical workflow using websetocc:

operation processWebRequest
    public web

    ; Step 1: Load web data
    webload

    ; Step 2: Reconnect to ensure data integrity
    reconnect/readcheck

    ; Step 3: Set current occurrence based on web client data
    websetocc

    ; Step 4: Process your business logic here
    if ($status = 0)
        ; Success! Current occurrence is now set
        ; You can safely work with entity data
        message "Processing occurrence: %%ENTITY.KEYFIELD%%"
    else
        ; Handle errors (see error codes below)
        message "Error setting occurrence: %%$procerror%%"
    endif
end
Enter fullscreen mode Exit fullscreen mode

🚨 Understanding Return Values

websetocc provides helpful feedback through system variables:

Success Values ($status)

  • 0: Success! βœ… Current occurrence has been successfully set
  • < 0: Error occurred ❌ Check $procerror for details

Common Error Codes ($procerror)

  • -1: General error - Entity is the outer entity of a Record component
  • -1102: Entity not found - The requested entity name isn't painted on the component
  • -1133: Occurrence not found - The specific occurrence couldn't be located

πŸ› οΈ Practical Example

Let's say you have a customer management DSP. When a user clicks on a specific customer in the web interface:

operation editCustomer
    public web

    ; Load the request data
    webload

    ; Reconnect to database
    reconnect/readcheck

    ; Set current occurrence based on customer ID from web
    websetocc

    if ($status = 0)
        ; Now we can work with the specific customer
        $webinfo("output") = "Editing: %%CUSTOMER.NAME%%"

        ; Perform your editing logic here
        ; The correct customer occurrence is now active

    elseif ($procerror = -1133)
        $webinfo("output") = "Customer not found!"

    elseif ($procerror = -1102)  
        $webinfo("output") = "Invalid entity requested!"

    else
        $webinfo("output") = "Unexpected error occurred!"
    endif
end
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Best Practices

  • Always call websetocc after webload and reconnect - This ensures proper data loading and connection state πŸ”„
  • Check $status after each websetocc call - Proper error handling prevents unexpected behavior πŸ›‘οΈ
  • Use meaningful error messages - Help users understand what went wrong πŸ“’
  • Only use in DSP components - websetocc is specifically designed for web environments 🌐

πŸŽ‰ Conclusion

The websetocc statement is an essential tool for managing entity occurrences in Uniface web applications. By understanding how it differs from setocc and following the proper workflow (webload β†’ reconnect β†’ websetocc), you can build robust web applications that correctly handle user interactions and data management!

Remember: websetocc bridges the gap between web client requests and your Uniface component's data processing, making it a crucial piece of the DSP puzzle. Happy coding! πŸš€


Keywords: Uniface, websetocc, Dynamic Server Pages, ProcScript

Top comments (0)