Skip to main content

Notifications

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.

  • scopecrm Profile Picture
    scopecrm 344 on at
    RE: Update "To" field on email form with account lookup value from case entity

    Thanks Leah, I have now updated the status

  • scopecrm Profile Picture
    scopecrm 344 on at
    RE: Update "To" field on email form with account lookup value from case entity

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update "To" field on email form with account lookup value from case entity

    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.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update "To" field on email form with account lookup value from case entity

    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.

  • scopecrm Profile Picture
    scopecrm 344 on at
    RE: Update "To" field on email form with account lookup value from case entity

    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.  

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update "To" field on email form with account lookup value from case entity

    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.

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,979 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,848 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans