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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to populate lookup field dynamically client side

(0) ShareShare
ReportReport
Posted on by 645
My mission is to copy a record in client side (no plugins) so using onload event I'm calling a function that fetching an object with all my attributes(to be copy to..), I can fill simply all fields in my form except from lookup field. I have in my data from my fetch 3 values to use: name: the data shown in the field' value: the guid of the entity reference and a number of type.. To populate a lookup field I need the name of the entity the id and type.

Two questions rise up:

1.Is the type value usable to populate the entity type field if yes then how? 2.Can I populate the name attribute in the new field as is from the old field?

Example of the data that I get :

Attributes
:
name
:
"this is the name"
type
:
"10026"
__proto__
:
Object
Value
:
"{6BddfDC-8B10-47F4-BC49-A537d2E43E}"

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    refer to below post on how to set lookup field using JS

    www.magnetismsolutions.com/.../how-to-get-and-set-a-lookup-field-using-javascript-in-dynamics-365

    recordid is would be = {6BddfDC-8B10-47F4-BC49-A537d2E43E}

    recordName would be = "this is the name"

    entityName would be = the entity name , you cannot use Object Type code (10026)

    Sample Code

    functin SetLookup()

    {

    var lookupValue = new Array();

    lookupValue[0] = new Object();

    lookupValue[0].id = "92EC3861-8D59-E811-815C-080027878DE4";

    lookupValue[0].name = "Custom Team";

    lookupValue[0].entityType = "team";

    Xrm.Page.getAttribute("fieldname").setValue(lookupValue);

    }

  • sahara Profile Picture
    645 on at

    Hey Wei

    I know the basic way to popualte, but my case is that I don't know the Entity name a head, the function is generic dynamically, can I know by the type that I get the entity name?

  • Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    use webapi to query the metadata. below is the sample, but you have to modify a little bit.

    community.dynamics.com/.../retrieve-object-type-codes-via-webapi-url

    https://community.dynamics.com/crm/f/117/t/218707

  • gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    You need to pass entity name to populate the lookup which is mandatory . Seems you have objecttypecode so you can easily retrieve dynamically the entity  name using web API.

    Here is the web API query to retrieve the entity name. 

    xxxxxxxx.api.crm.dynamics.com/api/data/v8.2/EntityDefinitions?$select=LogicalName,ObjectTypeCode&$filter=ObjectTypeCode eq 10026

    metadatawebapi.JPG

    You can refer below reference to know more about retrieve metadata using WebAPI.

    https://msdn.microsoft.com/en-us/library/mt607522.aspx

    Hope this helps.

  • sahara Profile Picture
    645 on at

    Hey Goutam is that all the code I need? or should I use Sdk.request and set all request data, I tried using your code example and got errors..If I don't need the all code can you please add a full code example, I mean when I use the url on a browser I get my data but how do I use it on client side. insert in a var ? 

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi,

    Try with this , please change the version as per your CRM instance .

            var req = new XMLHttpRequest();
            req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/EntityDefinitions?$select=LogicalName,ObjectTypeCode&$filter=ObjectTypeCode eq 10026", 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) {
    debugger; var result = JSON.parse(this.response); var logicalName = result["LogicalName"]; } else { Xrm.Utility.alertDialog(this.statusText); } } }; req.send();


  • sahara Profile Picture
    645 on at

    Goutam, Thank you!

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans