Header Ads Widget

🚫 Restrict Access to a Form If Already Opened by Another User in D365FO

 🎯 Objective

In many scenarios, record locking at the UI level is essential to prevent multiple users from editing the same data simultaneously. In this blog, we’ll walk through how to restrict users from opening a Sales Order form if another user already has it open, and throw a relevant error message like:

"The same Sales order is already opened by another user."


🏗️ Step-by-Step Implementation


✅ Step 1: Add a New Field to SalesTable

  • Navigate to AOT → Data Dictionary → Tables → SalesTable

  • Add a new field named UserLockId

    • Type: String

    • Label: User Lock ID

    • Purpose: Store the ID of the user who has opened the Sales Order


✅ Step 2: Override init() Method on SalesTable Form

Update the SalesTable FormMethodsinit() as follows:

x++
public void init() { SalesTable currentOrder = element.args().record(); FormRun callingForm = element.args().caller(); if (!currentOrder.UserLockId) { ttsBegin; currentOrder.UserLockId = curUserId(); currentOrder.doUpdate(); ttsCommit; } else if (currentOrder.UserLockId != curUserId()) { throw error(strFmt("Sales Order %1 is already opened by user: %2", currentOrder.SalesId, currentOrder.UserLockId)); } super(); }

✅ Step 3: Release the Lock in close() Method

Also, override the close() method in the same form to release the lock when the user closes the form:

x++
public void close() { SalesTable lockedOrder = element.args().record(); if (lockedOrder.UserLockId) { ttsBegin; SalesTable orderToUpdate; select forUpdate orderToUpdate where orderToUpdate.RecId == lockedOrder.RecId; orderToUpdate.UserLockId = ''; orderToUpdate.doUpdate(); ttsCommit; } super(); }

💡 Key Considerations

  • This solution assumes all users are opening Sales Orders from a standard menu item or list page and not through batch processes or APIs.

  • You can expand this logic using flags or timestamps for better control (e.g., lock expiry).

  • Don’t forget to refresh the SalesTable cache if you're facing visibility issues on the updated field.


📌 Use Case Scenarios

  • Preventing duplicate data entry on transactional forms

  • Ensuring single-user access to critical financial records

  • Custom user-specific form locks in approval workflows


✅ Summary

This approach provides a lightweight form-level record lock in D365FO using a custom field and simple logic in the init() and close() methods. It’s particularly useful for high-importance forms where data integrity and editing control are crucial.

Post a Comment

0 Comments