Automatically set an OptionSet value when creating a new record from different filtered subgrid views using a single ribbon button
In this article I’ll show a practical and clean pattern to prepopulate an OptionSet field when creating a new record, depending on which view or subgrid the user clicks + New from.
The solution uses:
- JavaScript
- Ribbon Workbench
- One JavaScript file
- One ribbon button
- One command
This approach keeps a single, structured JavaScript architecture (OnLoad, OnSave, ribbon handlers, utility functions) and avoids duplicating UI elements or commands.
Scenario
We have:
- A custom entity (example:
custom_record) - An OptionSet field (example:
custom_category) with multiple values - Multiple views or subgrids, each filtered by a different OptionSet value
Requirement
- From View A →
custom_categorymust be prepopulated with Value A - From View B →
custom_categorymust be prepopulated with Value B - The user should not manually choose the value
Core idea
The solution is based on:
- Ribbon Workbench passing the
SelectedControlparameter - Each subgrid having a unique control name
Xrm.Navigation.openForm()supportingformParameters- Enable Rules used only to detect context, not to duplicate commands
JavaScript structure (single file)
The JavaScript is organized under one namespace and contains:
- Global constants
- OnLoad
- OnSave
- Ribbon functions
- Enable rules
- Shared utility logic
This keeps the solution clean, readable, and easy to maintain.
OptionSet constants
var CATEGORY = { VALUE_A: 100000000, VALUE_B: 100000001 }; Ribbon actions
Create record from View A
CreateFromViewA: function () { if (!parent.window.location.href.toString().includes("etn=parententity&id=")) return; var parentId = Xrm.Page.data.entity.getId().replace("{", "").replace("}", ""); var parentName = Xrm.Page.getAttribute("name").getValue(); var entityFormOptions = { entityName: "custom_record", useQuickCreateForm: true }; var formParameters = {}; formParameters["custom_category"] = CATEGORY.VALUE_A; formParameters["custom_parent"] = [{ id: parentId, name: parentName, entityType: "parententity" }]; Xrm.Navigation.openForm(entityFormOptions, formParameters); }, Create record from View B
CreateFromViewB: function () { if (!parent.window.location.href.toString().includes("etn=parententity&id=")) return; var parentId = Xrm.Page.data.entity.getId().replace("{", "").replace("}", ""); var parentName = Xrm.Page.getAttribute("name").getValue(); var entityFormOptions = { entityName: "custom_record", useQuickCreateForm: true }; var formParameters = {}; formParameters["custom_category"] = CATEGORY.VALUE_B; formParameters["custom_parent"] = [{ id: parentId, name: parentName, entityType: "parententity" }]; Xrm.Navigation.openForm(entityFormOptions, formParameters); }, Enable rules (context detection)
Enable rule for View A
CreateFromViewAEnableRule: function (selectedControl) { var controlName = selectedControl?._controlDescriptor?.Name; return controlName === "Subgrid_ViewA"; }, Enable rule for View B
CreateFromViewBEnableRule: function (selectedControl) { var controlName = selectedControl?._controlDescriptor?.Name; return controlName === "Subgrid_ViewB"; }, Each rule simply checks which subgrid invoked the command.
Ribbon Workbench configuration
- Create one button on the SubGrid ribbon (+ New)
- Create one command
- Add:
- JavaScript Action
- Enable Rule
- Pass
SelectedControlto both the action and the enable rule - Associate the command with the button
No duplicated buttons. No duplicated commands.
Why this approach works
- The same entity behaves differently depending on context
- Users always get the correct default values
- Ribbon customization remains minimal
- JavaScript remains centralized
- The UX feels native
When to use this pattern
This approach is ideal for:
- Document management
- Categorized records
- Attachments
- Contracts
- Certifications
- Any scenario where views represent logical categories
Conclusion
Even with modern Power Platform tools, Ribbon Workbench + JavaScript is still extremely powerful when used correctly.
By leveraging SelectedControl and Xrm.Navigation.openForm(), you can build context-aware record creation with minimal UI customization and a clean, maintainable code base.