Hi,
I have a task that requires me to create the functionality to convert an email to a specific custom entity, much the same as the OOB for converting an email to a case or opportunity.
I'll run through the process in case others are struggling with a similar scenario and can get some ideas from this, then present the difficulty described in the subject field in the end.
I solved this task by adding a button under the "Convert to" dropdown and adding javascript to it that opens the quick create form for the custom entity. The code is as follows:
function convertToAHV(primaryControl) {
'use strict';
var formContext = primaryControl;
var from = formContext.getAttribute("from").getValue();
var to = formContext.getAttribute("to").getValue();
var currentRecGuid = [];
currentRecGuid[0] = formContext.data.entity.getEntityReference();
var entityOptions = {};
entityOptions["entityName"] = "cap_extendedcontrol";
entityOptions["useQuickCreateForm"] = true;
var formParams = {};
formParams["cap_originatingactivity"] = currentRecGuid;
if ((from?.[0]?.entityType === "account" || from?.[0]?.entityType === "contact")) {
formParams["cap_customer"] = from;
} else if ((to?.[0]?.entityType === "account" || to?.[0]?.entityType === "contact")) {
formParams["cap_customer"] = to;
}
Xrm.Navigation.openForm(entityOptions, formParams).then(
(success) => {
//console.log(success);
},
(error) => {
console.log(error);
}
);
}
When converting an activity record, one should add a pointer to the newly created record in the emails regardingobjectid field. When this is done, the email will show up on the timeline on the new record the email was converted to.
Here I ran into a couple issues.
- The formContext from the parent email was not available in the quick create form through Xrm.Utility.getPageContext().input.createFromEntity. It is available when the parent is Contact or Account (e.g. when creating a new custom entity record from a subgrid on Contact with quick create enabled), but somehow not in Email. I suspect this has something to do with email being defined as an activity/activitypointer..
- Since quick create forms purpose is a flyout data entry, the guid of the record created from the quick create will not be available before you have saved&closed the record.
To work around these issues we had to create a "originatingActivity" lookup field on the quick create form, and make use of the addOnPostSave client api ref.
When the "Convert to X" button is clicked, the originatingActivity lookup field is populated with the email which the button is being clicked from.
When the quick create form opens, it adds an event handler to be called after the quick create record is saved(&closed). See the following code.
function onLoad(executionContext) {
var formContext = executionContext.getFormContext();
formContext.data.entity.addOnPostSave(onPostSave);
}
function onPostSave(executionContext) {
var formContext = executionContext.getFormContext();
var originatingActivity = formContext.getAttribute("cap_originatingactivity").getValue();
var ahvId = formContext.data.entity.getId().replace(/[{}]/g, "");
(async () => {
var orgActEntityType = originatingActivity[0].entityType;
if (originatingActivity === null || originatingActivity === undefined) {
return;
}
await updateEmailRecord(orgActEntityType, originatingActivity[0].id, ahvId);
//await formContext.data.refresh(false);
})();
}
async function updateEmailRecord(entityLogicalName, emailId, ahvId) {
if (entityLogicalName === "email") {
const payLoad = {
"regardingobjectid_cap_extendedcontrol@odata.bind": "/cap_extendedcontrols(" ahvId ")",
};
if (emailId) {
await Xrm.WebApi.updateRecord("email", emailId, payLoad);
}
}
}
This is all well and good, and works as intended, however we want one last functionality in place before we go live - refresh the parent email form when the convertTo process is done.
This way the newly created record will be visible in the regardingobjectid field without users having to make any additional clicks (or simply remebering doing it).
Do anyone have any tips on how this can be done? Since the parent email context is not available when we're in the scope of the quick create form, I can't get my head around how this is remotely possible to achieve.
Looking forward to any replies.
Best regards,
Marius