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 CRM (Archived)

Lookup shows "no name" after a value is populated

(0) ShareShare
ReportReport
Posted on by 320

Hello,

We are using CRM Online version 9.1

Have implemented this code to populate value from Accounts to Invoice (Contact lookup) on onChange of Customer field.

function test()

{
if(Xrm.Page.getAttribute("customerid").getValue()!=null)

//read

var opportunity = Xrm.Page.getAttribute("customerid").getValue()[0].id;

var opportunityId = opportunity.substring(1, 37);

opportunityId = opportunityId.replace('{', '').replace('}', '');

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/accounts(" + opportunityId + ")?$select=_new_invoicetocontactname_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.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
                var result = JSON.parse(this.response);
                var _new_invoicetocontactname_value = result["_new_invoicetocontactname_value"];
                var _new_invoicetocontactname_value_formatted = result["_new_invoicetocontactname_value@OData.Community.Display.V1.FormattedValue"];
                var _new_invoicetocontactname_value_lookuplogicalname = result["_new_invoicetocontactname_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                
                var lookupValue = new Array();
                lookupValue[0] = new Object();
                lookupValue[0].id =_new_invoicetocontactname_value; // GUID of the lookup id
                lookupValue[0].name = _new_invoicetocontactname_value_formatted; // Name of the lookup
                lookupValue[0].entityType = "contact"; //Entity Type of the lookup entity
                Xrm.Page.getAttribute("new_invoicetocontactname").setValue(lookupValue);

            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();

}


After value is populated, it shows no name in the lookup and shows correct value when the record is saved.

I have already asked about this here https://community.dynamics.com/crm/f/117/p/350287/938135#938135 but haven't got the solution yet.

Any help would be appreciated.
Thanx.

*This post is locked for comments

I have the same question (0)
  • StefanS365 Profile Picture
    3,575 Most Valuable Professional on at

    Hi Huzefa,

    Does the record in the lookup have a value for the Name field?

  • Huzefa Bootwala Profile Picture
    320 on at

    Hi Stefan,

    Lookup value is from "Contact" entity. It has value in the First Name and Last Name fields.

    Not sure if this is the answer to your question.

    Thanx.

  • Suggested answer
    Dynamics365 Rocker Profile Picture
    7,755 on at

    Can you please check below code:

    alert("Name is :"+ _new_invoicetocontactname_value_formatted);

    Let me know if you are getting name of record in alert.

  • Huzefa Bootwala Profile Picture
    320 on at

    Hi,

    It says this Name: undefined

    It means its not getting the name of the record for lookup. But then how does it get the name on onSave?

    What is the solution for this?

    Thanx.

  • gdas Profile Picture
    50,091 Moderator on at

    Try to use hard code value to see if your code is working fine or not .  Then proceed with dynamics value .

    community.dynamics.com/.../how-to-set-lookup-field-value-using-javascript-dynamics-crm

    With that debug your code and compare what value you are assigning in the lookup you will definitely find the root cause.

  • Huzefa Bootwala Profile Picture
    320 on at

    Hi, I did this and it worked perfectly fine

    var lookupValue = new Array();
                    lookupValue[0] = new Object();
                    lookupValue[0].id = "{2466ABB2-0951-E911-A96F-000D3AD24182}"; // GUID of the lookup id
                    lookupValue[0].name = "Huzefa Bootwala"; // Name of the lookup
                    lookupValue[0].entityType = "contact"; //Entity Type of the lookup entity
                    Xrm.Page.getAttribute("new_invoicetocontactname").setValue(lookupValue);


    And even the code works fine but only after I save the record.

  • Huzefa Bootwala Profile Picture
    320 on at

    I added alert after the id and name is assigned to a variable.

    The id which I got is the correct value of a Contact record but the name says undefined.

  • Clint Woods Profile Picture
    1,031 on at

    @Huzefa Bootwala I think you need to validate your select statement in the debugger. is the attribute name correct? "_new_invoicetocontactname_value". See what values it returns.

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!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans