Dynamics 365 – Custom Dialogs in UCI – Part 4
- 3 minutes read - 553 wordsNote: this is an unsupported customization and may be subject to changes by Microsoft.
So I wanted to recreate the convert email to opportunity functionality and started to search online for any solutions. That is where I stumbled upon Guidinger’s and Capek’s fantastic posts on how you can create custom dialogs. So I experimented a bit with this and here I will share my own experiences.
How to find CRM’s OOB-dialogs
I didn’t want to recreate the entire form-xml dialog from scratch, so I tried to find a way to fetch the OOB-dialogs in dynamics. Using Jonas Rapp’s excellent tool FetchXML Builder I was able to find the form-XML of the “convert activity”-dialog (and plenty other dialogs as well). Here is the query I used:
<fetch mapping="logical" count="50" version="1.0">
<entity name="systemform">
<attribute name="formxml" />
<filter type="and">
<condition attribute="formid" operator="eq" value="96a67849-153a-461d-aeca-b95862b92887"/>
</filter>
</entity>
</fetch>
So using the FXB-tool you can query dynamis, find the dialog-xml and recreate similar functionality. On this link you can see, how the original “Convert Activity”-dialog XML looks like in Dynamics.
Creating the Custom “Convert Activity”-dialog
So in the OOB Convert Activity dialog you have to pick Customer(Account or Contact) and also decide if you want to open the new Opportunity and a couple of other options. I wanted to simplify this dialog, so the user only gets to selecet Account on the Customer field and nothing else.
So how to create a dialog is pretty well explained in the aforementioned blog posts. But what I did not know was how to create a lookup. And this I was helped doing by the Original Dialog that I found the XML for. It looks like this:
<control id="customerLookup" classid="{270BD3DB-D9AF-4782-9025-509E298DEC0A}" isunbound="true" isrequired="true">
<parameters>
<TargetEntities>
<TargetEntity>
<DefaultViewId>{A9AF0AB8-861D-4CFA-92A5-C6281FED7FAB}</DefaultViewId>
<EntityLogicalName>account</EntityLogicalName>
</TargetEntity>
<TargetEntity>
<EntityLogicalName>contact</EntityLogicalName>
</TargetEntity>
</TargetEntities>
</parameters>
</control>
I only wanted to show accounts so I removed the TargetEntity-tag containing the contact.
Calling the action to perform the conversion
So after we have created our dialog, now we need to add som logic. We need to call the action that converts the email to an opportunity. Here is the code for calling the ConvertActivity-action:
var reqObj = {
"ActivityId":param_entityId,
"ActivityEntityName":param_entityTypeCode,
"TargetEntity":{
"@odata.type":"#Microsoft.Dynamics.CRM.opportunity",
"name": param_subject,
"customerid_account@odata.bind":"/accounts("+ customerLookup +")",
"transactioncurrencyid@odata.bind":"/transactioncurrencies("+ param_currencyId + ")",
"ownerid@odata.bind":"/"+ param_ownerType +"s("+ param_ownerId + ")"
},
"TargetEntityName":"opportunity",
"CreateCampaignResponse":false
};
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/ConvertActivity", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
Xrm.Utility.closeProgressIndicator()
var resp = JSON.parse(this.response);
Xrm.Navigation.openForm({entityId:resp.RecordId, entityName:"opportunity"})
} else {
Xrm.Utility.closeProgressIndicator()
}
}
};
req.send(JSON.stringify(reqObj));
Finally we open the dialog with following code snippet, make sure to enter your own transactioncurrency-guid:
Xrm.Navigation.openDialog("ConvertActivityToOpportunity", {position:1}, {
param_entityId:Xrm.Page.data.entity.getId(),
param_entityTypeCode: Xrm.Page.data.entity.getEntityName(),
param_subject: Xrm.Page.getAttribute("subject").getValue(),
param_ownerId: Xrm.Page.getAttribute("ownerid").getValue()[0].id,
param_ownerType: Xrm.Page.getAttribute("ownerid").getValue()[0].entityType,
param_currencyId: "93345104-29e7-e911-a990-000d3a44ae4f"
});
##Conclusion In conclusion, using the Fetch Xml Builder you can mimic almost any OOB-functionality and tailor it to your needs. If you want to take a closer look on the code, you can view it in my github-repo. If you want to download the solution and install it to your environment you can do that here. Once you click the link, just hit the grey download-button and you should be all set.
I hope that you have found this interesting and I look forward to get your feedback on this.
Until next time!