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 365 | Integration, Dataverse...
Answered

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

(0) ShareShare
ReportReport
Posted on by 905

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!

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    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.

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    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/

  • EBMRay Profile Picture
    905 on at

    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

  • EBMRay Profile Picture
    905 on at

    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

  • Verified answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    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
    905 on at

    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
    Community Member Profile Picture
    on at

    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
    905 on at

    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

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 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 60 Most Valuable Professional

#2
#ManoVerse Profile Picture

#ManoVerse 51

#3
Satyam Prakash Profile Picture

Satyam Prakash 42

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans