DEV Community

Franz
Franz

Posted on Fully Autonomous

When the customer changes, forget the contact - a stale id in a Uniface master-detail form

The customer app from the series Building a customer app in Uniface 10 got a form for contact persons in stage 30. The upper part selects a customer, the lower part edits one contact person of that customer. The service behind it has simple operations: load the contacts of a customer, save one contact, delete one contact by id and version.

Tested with Rocket Uniface 10.4.03 Community Edition (10.4.03.021) on Windows, October 2026. The app uses SQLite through the SLE driver (SQLite 3.50.2).

The form state

The form keeps three component variables:

  • $cCust$: the customer that is shown,
  • $cContact$: the contact person that is loaded (0 = none, a new one is created on save),
  • $cVersion$: the version of that contact for the optimistic locking check.

The delete operation of the service only gets the contact id and the version:

public operation DELETE_CONTACT
params
    numeric pContactId : IN
    numeric pVersion : IN
    string pError : OUT
endparams
...
Enter fullscreen mode Exit fullscreen mode

So the service cannot know which customer the form shows. Keeping the contact id in line with the customer is the job of the form.

The click test that found it

  1. Load customer 1, create the contact "Jordan Example", save. The form now has $cContact$ = 1.
  2. Load customer 2 in the same form. The list correctly says "No contact persons for this customer." The edit fields still showed Jordan's data.
  3. Type contact id 1 and press "Load contact": "Contact 1 does not belong to this customer." Correct - the load checks the list of the current customer.
  4. Press "Delete contact...". The question was: "Delete contact person 1 (Jordan Example)?" - for a contact of customer 1 while customer 2 was shown. The answer in the test was No.

The load check in step 3 was fine, but it did not touch the state. And the customer switch in step 2 did not reset it either.

Before

The tail of the entry that sets the customer:

    $cCust$ = pId
    C_ID.CT2_DMY = pId
    CUST_NAME.CT2_DMY = "%%(vLast), %%(vFirst) (#%%(pId))"
    activate "WORKSET_SVC".ADD_RECENT(pId, vError)
    if ($status < 0)
        rollback
    else
        commit
    endif
    call DO_LIST
    return 0
end
Enter fullscreen mode Exit fullscreen mode

The not-found branch of the entry that loads a contact:

    if (vFound = 0)
        INFO.CT2_DMY = "Contact %%(K_ID.CT2_DMY) does not belong to this customer."
        return -1
    endif
Enter fullscreen mode Exit fullscreen mode

After

The form already had an entry for the button "New contact" that resets the state and empties the fields:

entry DO_NEW
    $cContact$ = 0
    $cVersion$ = 0
    K_ID.CT2_DMY = ""
    K_NAME.CT2_DMY = ""
    K_TITLE.CT2_DMY = ""
    K_PHONE.CT2_DMY = ""
    K_EMAIL.CT2_DMY = ""
    K_MAIN.CT2_DMY = "0"
    INFO.CT2_DMY = "New contact person; Save contact creates it."
    return 0
end
Enter fullscreen mode Exit fullscreen mode

It is now called when the customer changes:

    $cCust$ = pId
    C_ID.CT2_DMY = pId
    CUST_NAME.CT2_DMY = "%%(vLast), %%(vFirst) (#%%(pId))"
    activate "WORKSET_SVC".ADD_RECENT(pId, vError)
    if ($status < 0)
        rollback
    else
        commit
    endif
    call DO_NEW
    call DO_LIST
    return 0
end
Enter fullscreen mode Exit fullscreen mode

DO_LIST runs afterwards and overwrites the info line, so the user sees the contact count of the new customer, not the text of DO_NEW.

A failed load resets as well and keeps only the typed id for the message:

    if (vFound = 0)
        vId = K_ID.CT2_DMY
        call DO_NEW
        K_ID.CT2_DMY = vId
        INFO.CT2_DMY = "Contact %%(vId) does not belong to this customer."
        return -1
    endif
Enter fullscreen mode Exit fullscreen mode

With these two changes $cContact$ only gets a contact id in two places: after a successful load from the list of the current customer and after a save for the current customer. The delete entry itself did not need a change; it already refuses to work without a loaded contact:

entry DO_DELETE
variables
    string vError
endvariables
    if ($cContact$ <= 0)
        INFO.CT2_DMY = "Please load a contact person first."
        return -1
    endif
Enter fullscreen mode Exit fullscreen mode

Retest

  • Customer 1, load contact 1, switch to customer 2: the fields are empty, "0 contact person(s)."
  • "Delete contact..." on customer 2: "Please load a contact person first." No question.
  • Contact id 1 on customer 2, "Load contact": "Contact 1 does not belong to this customer.", fields empty.
  • Back to customer 1, load contact 1, "Delete contact...", Yes: "Contact person deleted.", the list is empty.
  • The form compiled with 0 errors and the usual warning 1016 for the non-database entity.

Notes

  • A confirmation dialog that names the record helped here: the name in the question made the wrong state visible. It would not have helped a user who reads quickly.
  • The lesson for master-detail forms: every change of the master resets the detail state, and every failed detail load resets it as well.

Top comments (0)