web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Unanswered

Refresh parent form when quick create form has saved&closed (Custom ConvertTo functionality)

(1) ShareShare
ReportReport
Posted on by 54

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

I have the same question (0)
  • LenCRM Profile Picture
    25 on at

    Did you ever come up with a solution for this issue?  I have the same need at the moment.

  • Mariusssf Profile Picture
    54 on at

    Hi LenCRM,

    Ended up not using the quick create form but the main form, so I don't think I have the solution for you if your problem is like the topic description for this post.

  • Max77 Profile Picture
    22 on at

    Hi, did you solve the issue?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
iampranjal Profile Picture

iampranjal 41

#2
Martin Dráb Profile Picture

Martin Dráb 36 Most Valuable Professional

#3
Satyam Prakash Profile Picture

Satyam Prakash 35

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans