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 :
Customer experience | Sales, Customer Insights,...
Answered

What's the Javascript code snippet to set the "To" field of an email in Dynamics?

(0) ShareShare
ReportReport
Posted on by 2,175

Hi all,

I just need some pointers on getting started with this. I've got a custom lookup field on an entity that points to a lead. I want to when an email is created and regarding is that entity to default the to field to the lead lookup on that entity.

I've been searching around for a sample code snippet and looking through the CRM REST builder but just can't seem to figure it out. Just a simple code snippet will do to point me in the right direction.

I know you've got to create an activitypointer record I think...

Kind regards,

Mike

I have the same question (0)
  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Mike,

    You need to use Web API to retrieve the lead lookup field from your custom entity when you set it as regarding...i.e. on change of regarding.

    You can refer the below log which has the same implementation-

    community.dynamics.com/.../get-lookup-value-from-other-entity-and-set-it-on-the-form-using-web-api-in-microsoft-dynamics-crm

    Hope this helps.

  • MikeC282 Profile Picture
    2,175 on at

    Hi Ravi,

    I'm fine with retrieving the lead ID. It's just I'm not sure how to set it onto the email's "To" field upon form load of the email. So basically as per the below screenshot. When I add a email from the timeline. I don't want it to default to the Customer field of the case but instead default the "To" field to the lead lookup field instead.

    How-to-set-the-email.png

    So this is what happens when I select email from the timeline:

    8877.Email.png

    I then tried retrieving the regardingobjectid upon form load of the email, which I'm getting weird results.

    It seems like the getValue result is null which is strange.

    function setEmail (executionContext) {
        var formContext = executionContext.getFormContext();
        var regarding = formContext.getAttribute("_regardingobjectid_value").getValue();
        console.log(regarding);
    }

    webresource-error.png

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Mike,

    The last error 'getValue of null' is due to incorrect schema name of regarding field. It is regardingobjectid not '_regardingobjectid_value'.

    However this is not you need to do. You need to retrieve the lead lookup value from the case entity using web api (refer the previously shared blog). Once you get the details of the leade (i.e. name, id), you set the to field as follows-

    -----------------

    var partlist = new Array();

    partlist[0] = new Object();

    partlist[0].id = "ff4070a3-d5eb-e911-a812-000d3a7957a4";

    partlist[0].name = "Susane";

    partlist[0].entityType = "lead";

    formContext.getAttribute("to").setValue(partlist);

    ---------------------

    Hope this helps.,

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans