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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

Update "To" field on email form with account lookup value from case entity

(0) ShareShare
ReportReport
Posted on by 344

Hi Experts,

I have an issue with the unified interface, in the classic interface when ever user try to create an email from case activities timeline. by default email to field used to populate with the account, in UCI this is changed and populating with contact value. So to fix this issue i wrote Jscript to get the account from related incident record and update it on Email to field. it's coming up with not found error.

can someone please let me know where the error is? here is the Jscript

function UpdateToField(executionContext) {
    var formContext = executionContext.getFormContext();
    var form = formContext.ui.getFormType() 
    
    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"formContext.context.getClientUrl() + "/api/data/v9.1/incident(" + newid + ")?$select=_new_organisation_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 () {
        alert("stage1")
    if (this.readyState === 4) {
    req.onreadystatechange = null;
    if (this.status === 200) {
    var result = JSON.parse(this.response); // you will get the retrieved value in object we stored in result var.
    
    var retrivedvalue= result._new_organisation_value//get the id of the field
    var retrivedformatedvalue= result["_new_organisation_value@OData.Community.Display.V1.FormattedValue"]; //get the formatted name of the field
    if (retrivedvalue!= null) {
    var value = new Array();
    value[0= new Object();
    value[0].id = retrivedvalue;
    value[0].name = retrivedformatedvalue;
    value[0].entityType = "account";
    alert("stage2")
    formContext.getAttribute("to").setValue(value); //set the lookup value finally
    }
    else
    alert("regarding value empty!!!!!!"// optional
    } else {
    Xrm.Utility.alertDialog(this.statusText);
    }
    }
    };
    req.send();
    
    }

Any help would be greatly appreciated.

I have the same question (0)
  • Community Member Profile Picture
    on at

    Hi scopecrm,

    The ‘To’ field value of the email is filled with the value of the ‘customer’ field in the case form.

     pastedimage1599187540709v1.pngpastedimage1599187639698v2.png 

    The ‘customer’ field is special, which type is customer. You can select both account and contact records.

    pastedimage1599186969004v1.pngpastedimage1599186974839v2.png

    So your JS code can’t find account value if you fill ‘Customer’ field with contacts.

    If you want to fill email ‘to’ field with contacts, you must fill case form ‘customer’ field with contacts.

    Regards,

    Leah Ju

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

  • scopecrm Profile Picture
    344 on at

    Hi Leah, Thanks for checking this for me. However we have created a custom attribute on case form "new_organisation". this field is populated and has value. I am still getting "not found" error.  

  • Verified answer
    Community Member Profile Picture
    on at

    Hi scopecrm,

    The code that get id of ‘new_organisation’ field exsits error, you need write as following:

    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_organisation_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_organisation_value = result["_new_organisation_value"];//id
                var _new_organisation_value_formatted = result["_new_organisation_value@OData.Community.Display.V1.FormattedValue"];//name
                var _new_organisation_value_lookuplogicalname = result["_new_organisation_value@Microsoft.Dynamics.CRM.lookuplogicalname"];//entity type
    
                if (_new_organisation_value != null) {
                    var value = new Array();
                    value[0] = new Object();
                    value[0].id = _new_organisation_value;
                    value[0].name = _new_organisation_value_formatted;
                    value[0].entityType = _new_organisation_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();
    }

    Test:

    pastedimage1599547471914v1.pngpastedimage1599547477623v2.png

    Regards,

    Leah Ju

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

  • Community Member Profile Picture
    on at

    Hi Partner,

    Has the problem been solved? Any updates?

    Please click Yes under "Did this answer your question?" to close this thread.

    pastedimage1599644081217v1.png

    Thanks. 

    Regards,

    Leah Ju

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

  • scopecrm Profile Picture
    344 on at

    Sorry for not coming back quickly. Yes this works well. I have simplified the Jscript using rest builder and WebApi

  • scopecrm Profile Picture
    344 on at

    Thanks Leah, I have now updated the status

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
Pallavi Phade Profile Picture

Pallavi Phade 102 Super User 2026 Season 1

#2
ManoVerse Profile Picture

ManoVerse 58 Super User 2026 Season 1

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 55 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans