Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

How to populate a table name in a choice field using Web API?

(0) ShareShare
ReportReport
Posted on by 873

Hello,

I would like to populate in a choice column two different tables (Logical Name)s.

For example: I have two tables in dataverse called Table A, and Table B. Onload of the form I just want to populate the Name of both tables in the dropdown instead of hardcoding the table values in the choice column in dataverse.
Screenshot-2022_2D00_01_2D00_31-220254.png

What is Web API that should be executed to get them populated as options in a choice column?

Can please someone provide an example or a sample of the JS that will achieve that scenario?

Any help is highly appreciated.

Thank you!

  • EBMRay Profile Picture
    EBMRay 873 on at
    RE: How to populate a table name in a choice field using Web API?

    Hi ,

    Thank you so much for providing the updated code.

    I tested it out, and it is working perfectly.

    Have a nice day!

    Best regards,

    EBMRay

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to populate a table name in a choice field using Web API?

    Hi EBMRay,

    The reason why your code wouldn't work is that you need to get the array in the req.responseText. So you need to change the code  from var jsonObj = JSON.parse(req.responseText); to var jsonObj = JSON.parse(req.responseText).value;  And the value parameter of the addOption() is Number type. However, MetadataId is String.

    So please try the following code:

    function onload(executionContext) {
    var formContext = executionContext.getFormContext();

    formContext.getControl("jul_tablename").removeOption(973470000);
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.2/EntityDefinitions?$select=MetadataId,LogicalName,DisplayName,PrimaryIdAttribute,ObjectTypeCode,IsCustomizable,OwnershipType,CanCreateAttributes,LogicalCollectionName&$filter=IsValidForAdvancedFind eq true and LogicalName eq 'account' or LogicalName eq 'contact'",true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");

    var startnumber = 1000000001;
    req.onreadystatechange = function () {
    if (req.readyState === 4) {
    var jsonObj = JSON.parse(req.responseText).value;
    console.log(jsonObj);
    console.log(jsonObj[0].LogicalCollectionName);

    for (i = 0; i < jsonObj.length; i++) {
    alert("Nothing happening here");
    formContext.getControl("jul_tablename").addOption({ value: startnumber + i, text: jsonObj[i].LogicalCollectionName });
    }
    }
    }

    req.send();
    }

  • EBMRay Profile Picture
    EBMRay 873 on at
    RE: How to populate a table name in a choice field using Web API?

    Hello  Bipin Kumar ,

    Thank you for providing the API that will fetch both tables.

    I have tried the below JS code based on Steve suggestions:

    function _setFromUser(executionContext) {
    //Get form context
    var formContext = executionContext.getFormContext();

    formContext.getControl("jul_tablename").removeOption(973470000);

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.2/EntityDefinitions?$select=MetadataId,LogicalName,DisplayName,PrimaryIdAttribute,ObjectTypeCode,IsCustomizable,OwnershipType,CanCreateAttributes,LogicalCollectionName&$filter=IsValidForAdvancedFind eq true and LogicalName eq 'account' or LogicalName eq 'contact'", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");

    req.onreadystatechange = function () {
    if (req.readyState === 4) {
    var jsonObj = JSON.parse(req.responseText);
    console.log(jsonObj);
    console.log(jsonObj[0].LogicalCollectionName);

    for (i = 0; i < jsonObj.length; i++) {
    alert("Nothing happening here");
    formContext.getControl("jul_tablename").addOption({ value: jsonObj[i].MetadataId, text: jsonObj[i].LogicalCollectionName });
    }

    }
    }
    req.send();

    }

    Screenshot-2022_2D00_02_2D00_01-222955.png

    However, I am still stuck on how to add the LogicalCollectionName of the retrieved data from the Web API as options directly to the field. Based on the code I provided nothing is happening to the field and it does not show the values that I need to extract from the array.

    What am I doing wrong? Could you please provide the updated code?

    Looking forward to your response.

    Best regards,
    EBMRay

  • Verified answer
    Bipin D365 Profile Picture
    Bipin D365 28,959 Super User 2024 Season 1 on at
    RE: How to populate a table name in a choice field using Web API?

    Hi,

    Unfortunately you can not filter, it will always display list of all tables present in your instance.

    Try the code provided by Steve, you can add filter condition to fetch selected tables.

    https://YOUR Instance URL/api/data/v9.0/EntityDefinitions?$select=MetadataId,LogicalName,DisplayName,PrimaryIdAttribute,ObjectTypeCode,IsCustomizable,OwnershipType,CanCreateAttributes&$filter=IsValidForAdvancedFind%20eq%20true and LogicalName eq 'account' or LogicalName eq 'contact'

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • EBMRay Profile Picture
    EBMRay 873 on at
    RE: How to populate a table name in a choice field using Web API?

    Hello ,

    Thank you for your reply and for providing an example.

    By following your steps will it be possible to choose the tables that I want to populate? Because I want to display specific tables and not all of them.

    I look forward to your response.

    Best regards,

    EBMRay

  • EBMRay Profile Picture
    EBMRay 873 on at
    RE: How to populate a table name in a choice field using Web API?

    Hi ,

    Thank you for providing a sample.

    I will give it a try and get back to you the soonest.

    The idea is I don't want all the tables, I need to populate specific tables. For example: (Accounts and Contacts).

    I want to indicate for the API that I need Accounts and Contacts.

    Best regards,

    EBMRay

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,959 Super User 2024 Season 1 on at
    RE: How to populate a table name in a choice field using Web API?

    Hi,

    You don't need to write any code for that.

    Create new Text field, Add on form, Select properties -> Controls -> select below highlighted control and click on Add

    CR79.PNG

    Once you save your changes and publish, you will see dropdown on form with all tables auto populated.

    CR80.PNG

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to populate a table name in a choice field using Web API?

    Hi EBMRay,

    You could use the following code to get all tables in the dataverse:

    function _setFromUser(executionContext) {
    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.2/EntityDefinitions",true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");

    req.onreadystatechange = function () {
    if (req.readyState === 4) {
    console.log(JSON.parse(req.responseText));
    }
    }
    req.send();
    }

    Then Use addOption() function to add them to your choice field.

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,642 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,371 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans