Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

CRM Lookup Field not being populated by Javascript Code

Posted on by Microsoft Employee

Hi all,

Could you please help me resolve the issue below?

Scenario:

MS Dynamics CRM 2016 (on premises)

Custom Entity with two lookup fields Opportunity and Account. The custom entity is related to the opportunity. I'm trying to set the Account Lookup value on the Form Load event.

Problem:

Although I'm not getting any error, the field is not being populated. Please see code below. Any help is greatly appreciated.

function SetLookup(query, callback) {
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + query, false);
    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);
                callback(result);
            }
        }
    };
    req.send();
}

function addParentAccountFilter() {

    var opportunityObj = Xrm.Page.getAttribute("new_opportunityid");

    if (opportunityObj == null)
        return;

    var id = opportunityObj.getValue()[0].id;
    SetLookup("/api/data/v8.2/opportunities(" + id + ")?$expand=customerid_account($select=accountid,name)", function(result)
    {
        if (result.hasOwnProperty("customerid_account")) {
            var accOptions = new Array();
            accOptions[0] = new Object();
            accOptions[0].id = result["customerid_account"]["accountid"];
            accOptions[0].name = result["customerid_account"]["name"];
            accOptions[0].entityType = "account";

            Xrm.Page.getAttribute("new_accountid").setValue(accOptions);
        }
    });
}

function Form_onload() {
    addParentAccountFilter();
}

*This post is locked for comments

  • gdas Profile Picture
    gdas 50,085 on at
    RE: CRM Lookup Field not being populated by Javascript Code

    great ! thats why I have mentioned sample data .

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CRM Lookup Field not being populated by Javascript Code

    Thank you all for your suggestions.

    Solution was simple. I had an extra pair of curly braces on the id variable. After removing them the code above worked as expected.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: CRM Lookup Field not being populated by Javascript Code

    Hi Daniel,

    what is  the error you are getting? I believe below code is correct , if you are getting the result like below. Considering the account lookup id is correct for your custom entity.

    var accOptions = new Array();
    accOptions[0] = new Object();
    accOptions[0].id = "7bF9000D1B-A393-4BBF-AD31-7678A1EBA056"; 
    accOptions[0].name = "Test Acc";
    accOptions[0].entityType = "account";
    
    Xrm.Page.getAttribute("new_accountid").setValue(accOptions);



  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CRM Lookup Field not being populated by Javascript Code

    Hi Goutam,

    Before posting here I did some debugging and seems to me that the error is happening at:

    Xrm.Page.getAttribute("new_accountid").setValue(accOptions);

    I just posted the whole scenario to make it easier to understand.

    Thank you,

    Daniel

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: CRM Lookup Field not being populated by Javascript Code

    Hi Daniel,

    Please try to debug the javascript.

    add a debugger before the function and go on from there.

  • gdas Profile Picture
    gdas 50,085 on at
    RE: CRM Lookup Field not being populated by Javascript Code

    Hi Daniel,

    Can you please debug your code  and share which line exactly you are getting error.

    Not sure looking your code seems the error is coming from  below lines when you are accessing the data.

    result["customerid_account"]["accountid"], result["customerid_account"]["name"];

    Please confirm which line actually you are getting error.

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