There are a lot of different blog posts and video reviews of features available as a part of the Dynamics 365 2020 Wave 1 release. I played with this release a bit and found 2 client-side code enhancements that can be interesting (or even useful) for developers.
I wrote that Xrm.Navigation.navigateTo can be used to open Html web resources in modal dialog in my previous post. As a part of the new release there will be the possibility to open the record in a similar manner. The following code opens the account record in a modal window:
Xrm.Navigation.navigateTo({ pageType: "entityrecord", entityName: "account", entityId: "cf84d93a-3b5a-ea11-a811-000d3a5a1e46", //don't pass anything to entityId if you want to create new record formId: "B053A39A-041A-4356-ACEF-DDF00182762B" //formId parameter is optional. If you want to open particular form //you should pass form id inside }, { target: 2, //2 - to open record in modal dialog position: 1, //1 - dialog in center, 2 - side panel width: { value: 80, unit:"%" } }).then(function(result) { var recordReference = result.savedEntityReference; //recordReference has the same structure as value of lookup field: //[{ // id: "cf84d93a-3b5a-ea11-a811-000d3a5a1e46", // entityType: "account", // name: "Record Name" //}] });
One issue I found here is that when the dialog is closed using “X” button, or the record is deleted using “Delete” button from command bar – the promise will remain unresolved so there is no way to handle these scenarios at the moment. Source information is available here – https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/customize-entity-forms#open-an-existing-record
The second interesting feature introduced is the possibility to override the “onclick” event on the record selected in lookup. Default behavior – app navigates you to that record. Now it’s possible to cancel that event or override the behavior. Here is the code:
function onFormLoad(executionContext) { var formContext = executionContext.getFormContext(); var lookupControl = formContext.getControl("lookup"); lookupControl.addOnLookupTagClick(onLookupClick); } function onLookupClick(executionContext) { //to cancel navigation event you can just call following code executionContext.getEventArgs().preventDefault(); //to get entityreference of record from lookup you can use following code: var recordReference = executionContext.getEventArgs().getTagValue(); //structure of object is following //{ // id: "cf84d93a-3b5a-ea11-a811-000d3a5a1e46", // entityType: "account", // name: "Record Name" //} }
More information about this event is available here – https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/events/onlookuptagclick
I thought that it would be really cool to combine those 2 pieces together to override behavior of the lookup item click. Here is the code for it:
function onFormLoad(executionContext) { var formContext = executionContext.getFormContext(); var lookupControl = formContext.getControl("parentaccountid"); lookupControl.addOnLookupTagClick(onLookupClick); } function onLookupClick(executionContext) { executionContext.getEventArgs().preventDefault(); var recordReference = executionContext.getEventArgs().getTagValue(); Xrm.Navigation.navigateTo({ pageType: "entityrecord", entityName: recordReference.entityType, entityId: recordReference.id }, { target: 2, width: { value: 80, unit:"%" } }); }
The following screen capture demonstrates the result:
Great information Andrew,
I’m new to this whole world and I was playing around with this code. Everything seems to be correct but for some reason it wants to skip over my navigationOptions when it comes time to set the modal. Am I missing something?
function onFormLoad(executionContext) {
var formContext = executionContext.getFormContext();
var lookupControl = formContext.getControl(“cr247_relatedaccount”);
lookupControl.addOnLookupTagClick(onLookupClick);
}
function onLookupClick(executionContext) {
debugger;
executionContext.getEventArgs().preventDefault();
var recordReference = executionContext.getEventArgs().getTagValue();
var pageInput = {
pageType: “entityrecord”,
entityName: recordReference.entityType,
formType: 2,
entityId: recordReference.id
};
var navigationOptions = {
target: 2,
position: 2,
width: {
value: 80,
unit: “%”
}
};
Xrm.Navigation.navigateTo(pageInput, navigationOptions);
}
Eric,
To make it work you should activate D365 2020 April Release in your instance. Following article describes what should you do in order to activate it – https://carldesouza.com/how-to-update-dynamics-365-to-2020-release-wave-1/
Andrew,
Have you found the form within the modal dialog to be responsive to Javascript? e.g. A form that hides/shows tabs via script is opened in a modal dialog and shows/hides your desired tabs appropriately .
I really like the functionality you presented in this post and have a potential use case for this exact scenario but have not had free time to play around with it myself. Just wondering if you happened to notice this while testing with modal dialogs.
Scott,
I haven’t thought about this usecase. I will check if it could be received in the script or not.
Andrew
Hello Andrew,
is there a way to set formid to be opened ?
Something like:
Xrm.Navigation.navigateTo({
pageType: “entityrecord”,
entityName: tagValue.entityType,
formType: 2,
formId: ‘321873-821739127-39821’,
entityId: tagValue.id
}
Yuriy,
Thanks for a great question. I did the fast check and formId parameter works like a charm. I will update the post.
Andrew
Hi Andrew, It always open a default main form regardless of the form ID. Is this a Microsoft bug?
Hello Shubham,
What code do you use?
Andrew