Understanding the Scenario
Imagine you have an Account entity in Dynamics 365 CRM with two subgrids:
-
N Subgrid – where you track NDA-related attachments.
-
S Check Subgrid – where you track internal review attachments.
When a user clicks “Add” on a subgrid, you want to:
-
Open a Quick Create form for a related entity (e.g.,
Attachment). -
Prepopulate the type of attachment based on the subgrid.
-
Associate the new record with the current account.
-
Prevent users from modifying the preselected type.
This requires capturing which subgrid the user interacted with, storing the account context, and applying logic when the form loads.
Key Concepts in Dynamics 365 CRM
Before diving into code, here are a few Dynamics 365 concepts that we rely on:
-
formContext – Represents the current form, giving access to fields, controls, and entity data.
-
Xrm.Navigation.openForm – Opens a new form (main or quick create) programmatically.
-
Quick Create Forms – Lightweight forms for adding related records quickly.
-
Subgrid Controls – Interactive grids embedded in forms to display related records.
-
Local Storage – Browser storage used temporarily to pass context between forms.
Reusable JavaScript Template
The following JavaScript module, AttachmentHandler, handles dynamic subgrid creation in a reusable way:
How It Works
-
OpenQuickCreate Function
-
Determines the current account and the subgrid from which the user clicked.
-
Stores context in localStorage.
-
Opens a Quick Create form for the attachment entity with prefilled parameters.
-
-
OnLoad Function
-
Runs when the Quick Create form opens.
-
Retrieves the stored context from localStorage.
-
Prepopulates the type of attachment (
NDAorSanity Check) based on the originating subgrid. -
Locks the field so users cannot modify it.
-
Benefits of This Approach
-
Reusable: Can be applied to multiple subgrids or entities without hardcoding names.
-
Maintainable: Clear separation of
OpenQuickCreateandOnLoad. -
User-Friendly: Prepopulates fields to reduce user errors.
-
Clean: Avoids global prefixes and cluttered code.
Implementation Tips
-
Always use formContext instead of global
Xrm.Pagefor modern CRM versions. -
Store temporary data in
localStorageorsessionStoragewhen passing between forms. -
Use constants or enumerations for option set values (
TYPES.NDA) instead of magic numbers.