Search code examples
javascriptdynamics-crmmicrosoft-dynamics

Set "to" field on email form i dynamics 365


I am trying to open a new email form in Dynamics 365 using JavaScript. I can easily pre-fill various fields like "Regarding" but when I try to fill the "To" field it just comes out empty.

Here is a small code snippet reproducing my problem:

let contactid = "91be7fec-2142-ec11-8c62-000d3ac23084";
let activityParameters = {};

// Set the regarding field -- this works!
activityParameters["regardingobjectid"] = contactid ;
activityParameters["regardingobjectidname"] = "Regarding your last email";
activityParameters["regardingobjectidtype"] = "contact";

// Set the to field -- this is not working!
var parties = [];
var receiver = {};
receiver["partyid_contact@odata.bind"] = `/contacts(${contactid})`;
receiver["participationtypemask"] = 2; //To
parties.push(receiver);
activityParameters["email_activity_parties"] = parties;
        
await window.parent.Xrm.Navigation.navigateTo(
            { pageType: "entityrecord", entityName: "email", data: activityParameters },
            { target: 2 } // Open in modal
        )

I have also tried some variations based on how lookups are set in other places, like:

var parties = [];
var receiver = {
    activityparty: {
        id: contactid,
        entityType: "contact",
        name: "XXX"
    },
    participationtypemask: 2
};

Googling the problem only gave me solutions for creating emails using the web API (as I have tried above), but I am unsure how to change the code so that navigateTo() recognizes the parameter.

Does anyone have an idea how to go about this?


Solution

  • I finally figured it out. Andrew Butenko described how to do it for openForm(), and it turns out the same solution can also be applied to navigateTo().

    I'll leave my solution here in case anyone else has the same problem.

    let contactid = "91be7fec-2142-ec11-8c62-000d3ac23084";
    let activityParameters = {};
    ​
    // Set the to field -- this IS working!
    activityParameters["to"] = [{
        id: contactid,
        name: "Contact Name Here",
        entityType: "contact"
    }];
          
    await window.parent.Xrm.Navigation.navigateTo(
                { pageType: "entityrecord", entityName: "email", data: activityParameters },
                { target: 2 } // Open in modal
            );