Study FAQ
How can a model-driven JavaScript command read a saved row instead of unsaved form values?
Also asked as 2
- How do I use PrimaryControl for a main-form command that reads Dataverse?
- How can a main-form button check a saved account value safely?
Answer
For a JavaScript command on a model-driven main form, configure PrimaryControl and use it directly as the form context. Capture the current table and row ID, then use Xrm.WebApi.retrieveRecord to read saved Dataverse data under the signed-in user's access.
In an isolated Account form with a published web resource, capture primaryControl.data.entity.getId() before awaiting a result and strip any braces. If no row ID is present, ask the user to save before checking. For a saved account, Xrm.WebApi.retrieveRecord("account", id, "?$select=creditonhold") requests only the required column. A denied, missing or failed read should show an unavailable result. A grid command needs its own selection context and must handle its selection count.
A form event handler receives execution context, while this command receives form context directly. Avoid using retained form state after an asynchronous read. Keep the result associated with the captured ID and suppress obsolete responses where the host workflow requires it. Check the actual command's save behavior and display a saved value as a snapshot for the user.
Test an unsaved row, a saved row with unsaved edits, missing read privileges and navigation while a read is pending. A later business operation must enforce its own permission and current-state checks.
Form and grid command parameters · retrieveRecord · Client API execution context
Full explanation
AB-400: Client Scripting and Commands in Model-Driven Apps
Study guide · AB-400
Read the full explanation