Skip to main content

Notifications

Announcements

No record found.

Service | Customer Service, Contact Center, Fie...
Suggested answer

Send email from case - populate to field with email address from a case field?

Posted on by 65

For cases, we do not use contacts, only accounts and have a field on the case form to capture an email address for the person raising the case called "preferred email".

By default the "to" field when sending an email is the account email address.Is it possible to configure with JavaScript so that when an agent sends an email from the case the "to" field is automatically populated with the email address from "preferred email" field on the case form?

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Send email from case - populate to field with email address from a case field?

    Hi,

    You can achieve your requirement with Pre-Operation plugin on Email Entity.

    First check if Regarding field value is of type Case then fetch preferred email address from Case.

    Once you have email address, use below code to set to activity party with that email address.

    // Create a new unresolved activity party

    Entity party2 = new Entity("activityparty");

    party2["addressused"] = "unresolved@email.com";//Replace with preferred email address from Case

    // Create a new EntityCollection and add the 2 parties

    EntityCollection to = new EntityCollection();

    to.Entities.Add(party2);

    // Set an email with the EntityCollection

    email["to"] = to;

    email is plugin context target.

    blog.magnetismsolutions.com/.../working-with-dynamics-crm-activity-party-lists-in-c--plugins

    Please mark my answer verified if i were helpful

  • dvn92 Profile Picture
    dvn92 65 on at
    RE: Send email from case - populate to field with email address from a case field?

    Hi @leah-ju1

    Thank you for this the JS code example is very useful

    However I wanted to see if the to field could be populated with just an email address value not a lookup. It is possible to manually type an email address into the to field (unresolved values) and I was hoping to do this via JS via the field captured on the case form.

    So if I have a field on my case form for email address (e.g. "user_email") I wanted that value to populate as an unresolved email recipient in the "to" field when an email is sent from that case? I tried doing this using the example JS but the response error was: Uncaught UciError: Value should be of type: LookupValue[]

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Send email from case - populate to field with email address from a case field?

    Hi dvn92,

    The ‘To’ field in email is special field, it displays name in the field and store email address in database.

    It consists following five types, so if you want to update ‘To’ field with the value you want, you need create a lookup field from these type instead of an address field on the case form.

    pastedimage1600925182821v1.png

    For example, I new a lookup field(Accounts) named organisation on the case form, and use it to update ‘To’ field when I create an email from the case.

    1.Js code:

    function UpdateToField(executionContext) {
    
        var formContext = executionContext.getFormContext();
    
        var lookup = formContext.getAttribute("regardingobjectid").getValue();  // case lookup on email form 
    
        var newid = lookup[0].id.slice(1, -1);
    
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl()   "/api/data/v9.1/incidents("   newid   ")?$select=_new_organisationid_value", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var result = JSON.parse(this.response);
                var _new_organisationid_value = result["_new_organisationid_value"];//id
                var _new_organisationid_value_formatted = result["_new_organisationid_value@OData.Community.Display.V1.FormattedValue"];//name
                var _new_organisationid_value_lookuplogicalname = result["_new_organisationid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];//entity type
    
                if (_new_organisationid_value != null) {
                    var value = new Array();
                    value[0] = new Object();
                    value[0].id = _new_organisationid_value;
                    value[0].name = _new_organisationid_value_formatted;
                    value[0].entityType = _new_organisationid_value_lookuplogicalname;
                    formContext.getAttribute("to").setValue(value); //set the lookup value finally
                }
                else
                alert("organisation is null");
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
    }

    2.Test:

    pastedimage1600925311610v2.pngpastedimage1600925330704v3.png

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans