You've correctly identified the limitations of directly triggering JavaScript on the click of a standard lookup field in Dynamics 365. Out-of-the-box, there isn't a direct "On Click" event available for lookup controls that you can hook JavaScript to.
Your idea of creating a new field that triggers JavaScript on change is a viable workaround, but it requires an extra field on the form and might not be the most intuitive user experience.
Here are a few alternative approaches and considerations, ranging from more straightforward to slightly more involved:
1. Leveraging the "Open Associated Records" Behavior (Configuration-Based - Limited Control):
While not a new window, you can configure the behavior of the lookup to open associated records in a new tab within the Dynamics 365 Unified Interface. This might be acceptable depending on your user's workflow.
- Go to Settings > Customization > Customize the System.
- Expand Entities and select the entity containing your lookup field.
- Expand 1:N Relationships (if it's a lookup to a related entity) or N:1 Relationships (if the lookup is on this entity).
- Find the relationship corresponding to your lookup field.
- In the relationship definition, look for the "Navigation Pane Item" section (for related entities) or the "Behavior" section (for N:1 relationships).
- For some relationship behaviors (like "Referential"), you might have options related to how associated records are opened. Experiment with these settings (in a non-production environment first) to see if any achieve a new tab behavior that suits your needs.
Limitations of this approach:
- Limited control over whether it's a new tab or a new window.
- Behavior might vary depending on the relationship type and configuration options available.
- Doesn't allow for custom JavaScript execution before opening.
2. Creating a Custom Control (PCFs - More Involved, but Flexible):
This is the most flexible approach but requires development skills. You could create a Power Apps component framework (PCF) control that mimics the appearance of a standard lookup but provides a custom click event.
- Develop a PCF Control: This control would:
- Render a UI similar to the standard lookup.
- Handle the click event on the rendered lookup element.
- Retrieve the selected record's ID and entity type.
- Use
Xrm.Navigation.openForm() with the windowOptions parameter to open the record in a new window.
- Add the PCF Control to Your Form: Replace the standard lookup field on your form with your custom PCF control.
Advantages of this approach:
- Full control over the click behavior.
- Ability to execute custom JavaScript before opening the new window.
- Provides a seamless user experience if the PCF control is designed well.
Disadvantages of this approach:
- Requires development expertise in PCF.
- More time and effort to implement.
3. Modifying the Ribbon (Less Ideal for Direct Lookup Click):
While you mentioned avoiding ribbon buttons, it's worth noting that you could add a button next to the lookup field that, when clicked, retrieves the lookup value and opens it in a new window using Xrm.Navigation.openForm() with windowOptions. However, this doesn't achieve the goal of clicking directly on the lookup.
4. Exploring Browser Extensions (Client-Side, Not Recommended for Managed Solutions):
Technically, a browser extension could potentially intercept clicks on lookup fields and trigger a new window. However, this is a client-side solution that is:
- Not part of your Dynamics 365 application: It would rely on users installing a specific browser extension.
- Difficult to manage and support: Ensuring all users have the extension and it works consistently can be challenging.
- Potentially impacted by Dynamics 365 updates: Changes to the Dynamics 365 UI could break the extension.
- Not a recommended approach for managed solutions or enterprise deployments.
Back to Your ".js on change" Idea:
If you want a less development-intensive approach than a PCF, your idea of using an additional field with an OnChange event could work, albeit with a slightly different user interaction:
- Add a New Single Line of Text Field: Place this field visually near your lookup field. You could even style it to look like a button or an icon.
- Add JavaScript
OnChange Event: On the OnChange event of this new field:
- Retrieve the value (ID) from your primary lookup field.
- Open the lookup record in a new window using
Xrm.Navigation.openForm() with windowOptions.
- Optionally, clear the value of this trigger field after opening to allow for subsequent clicks.
- Guide Users: Instruct users to click on this new "button-like" field to open the related record in a new window.
Example of the ".js on change" Approach:
function openLookupInNewWindow(executionContext) {
var formContext = executionContext.getFormContext();
var lookupField = formContext.getAttribute("your_lookup_field_name"); // Replace with your lookup field schema name
var triggerField = formContext.getAttribute("your_trigger_field_name"); // Replace with your new trigger field schema name
if (triggerField.getValue()) { // Check if the trigger field has a value (i.e., was "clicked")
var lookupValue = lookupField.getValue();
if (lookupValue && lookupValue.length > 0) {
var recordId = lookupValue[0].id;
var entityName = lookupValue[0].entityType;
var windowOptions = {
openInNewWindow: true
};
Xrm.Navigation.openForm({
entityName: entityName,
entityId: recordId
}, windowOptions);
}
triggerField.setValue(null); // Clear the trigger field after opening
}
}
You would then associate this JavaScript function with the OnChange event of your newly created trigger field. Users would need to "click" (enter and exit, or somehow trigger the change event) on this field to open the lookup record. You might need to visually style this trigger field to make it clear to the user what its purpose is.
Recommendation:
The PCF control approach offers the most seamless and direct way to achieve the desired "on click" behavior with a new window, but it requires development effort.
The "new field with .js on change" approach is a more accessible workaround if you want to avoid code development, but it involves an extra UI element and a slightly less intuitive user interaction.
The "Open Associated Records" behavior is worth exploring if a new tab is acceptable, as it's configuration-based.
Ultimately, the best approach depends on your technical resources, the level of customization required, and the desired user experience.