Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Answered

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

Posted on by 2,095

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

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: What's the Javascript code snippet to set the "To" field of an email in Dynamics?

    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.,

  • MikeC282 Profile Picture
    MikeC282 2,095 on at
    RE: What's the Javascript code snippet to set the "To" field of an email in Dynamics?

    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
    RaviKashyap 55,410 on at
    RE: What's the Javascript code snippet to set the "To" field of an email in Dynamics?

    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.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans