Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Auto-populate logical field name based on the table name selected value from a choice

Posted on by 873

Hello,

I would like to auto-populate in a choice column two different attributes' logical names based on the table name selected from the other choice column.

Example: If I selected accounts in the choice column, the field Name that I have in the form of type (choice) should be filtered and populate the attribute logical name emailaddress1 and emailaddress2, else If I selected the contacts table in the choice column it should show 'Address1: City' logical name field.

I found the Web API that should be executed to retrieve the attributes of an entity as below:

api/data/v9.2/EntityDefinitions(LogicalName='account')/Attributes?$filter=LogicalName eq 'emailaddress1' or LogicalName eq 'emailaddress2'

2. What is the function that should be used to do this filter onChange of the table name field option value?

3. To add the options retrieved from the WebAPI call based on the selected value I should use addOption to add the logical name fields to the dropdown right?

Screenshot-2022_2D00_02_2D00_02-224927.png

Can please someone provide an example to achieve the above requirements?

This is very similar to this thread but in that post it has some different functionalities should be used:
community.dynamics.com/.../how-to-populate-a-table-name-in-a-choice-field-using-web-api

Any help is highly appreciated.

Best regards,
EBMRay

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Auto-populate logical field name based on the table name selected value from a choice

    Hi EBMRay,

    Yes, I believe this could work. And it doesn't need any code. Only need to create 1:N relationship between table name table and field name table and then set "Related Records Filtering" in the field names lookup field.

    Please refer to this blog:

    How to Filter Lookups Without Custom Code in Dynamics 365 Customer Engagement (arbelatech.com)

  • EBMRay Profile Picture
    EBMRay 873 on at
    RE: Auto-populate logical field name based on the table name selected value from a choice

    Hi   ,

    Thank you for your reply.

    I will give it a try and I thought about another option:

    1. Create a Table for the Table Names.

    2. Create another Table for the field names that are associated with a table name from step 1.

    3. In the table (Expiry datatable) I will add two lookup fields for the table names that will give me the tables names and the field names that will give me all the fields.

    4. I need to configure the filtering options for the lookup field (cascading dropdown) to achieve the below: (Could you please provide any resource that shows how to configure filtering for the lookups?)

    Example: If I selected accounts in the table name it should automatically give me the fields associated with that table.

    Please let me know your opinion if the above option will work.

    I look forward to your response.

    Best regards,

    EBMRay

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Auto-populate logical field name based on the table name selected value from a choice

    Hi EBMRay,

    If you don't want to update it manually, you could use insertOption() function to insert your options into your field.

    Here is a sample about this: 

    java - InsertOptionValue Action on Local Option Set - Stack Overflow

    Note: Please distinguish which type is your field, local or global. Because it's parameter isn't same.

  • EBMRay Profile Picture
    EBMRay 873 on at
    RE: Auto-populate logical field name based on the table name selected value from a choice

    Hello  ,

    Thank you so much for providing the above code.

    After doing a test, when I try to save the record with the options retrieved from the API for both TableName and fieldName it is throwing the below error:
    The value 100000001 of 'jul_tablename' on record of type 'jul_expirydatatable' is outside the valid range. Accepted Values: 973470000

    I created three different options as below for both fields:

    ListofOp.png
    When I tested again it saved the record successfully. However, in the view, it is showing "New Option" instead of the exact saved value but in the form, it is showing the exact value name, and this is only for the Table Name. Regarding the field name, it is showing "New Option" in the form.

    TableResult.pngScreenshot-2022_2D00_02_2D00_03-221955.png

    Below is the updated code:

    function onchangeTbleName(executionContext) {

    var formContext = executionContext.getFormContext();

    var optionValue = formContext.getAttribute("jul_tablename").getValue();

    var url = Xrm.Page.context.getClientUrl();
    console.log(optionValue);
    // 1002 eq to contact
    if (973470002 === optionValue) {
    alert("Loan here");
    //contact' url
    console.log(1);
    url += "/api/data/v9.2/EntityDefinitions(LogicalName='cr884_loanedequipment')/Attributes?$filter=LogicalName eq 'cr884_enddate' or LogicalName eq 'cr884_warrantydate'";
    } else if (973470001 === optionValue) {
    alert("Account here");
    //accounts' url
    url += "/api/data/v9.2/EntityDefinitions(LogicalName='account')/Attributes?$filter=LogicalName eq 'emailaddress1' or LogicalName eq 'emailaddress2'";
    } else {
    return;
    }
    // clear options
    formContext.getControl("jul_fieldname").clearOptions();

    var req = new XMLHttpRequest();
    req.open("GET", url, true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");

    var startnumber = 973470000;
    req.onreadystatechange = function () {
    if (req.readyState === 4) {
    var jsonObj = JSON.parse(req.responseText).value;
    console.log(jsonObj);
    for (i = 0; i < jsonObj.length; i++) {
    formContext.getControl("jul_fieldname").addOption({ value: startnumber + i, text: jsonObj[i].LogicalName });
    }
    }
    }

    req.send();
    }

    1. Each time I change the filter of the Web API to fetch more attributes I need to manually add more options to the choice to reserve a value for it in order to be saved and to avoid throwing the above expectation error or there is a workaround for that?
    2. How we can handle that scenario to display the exact saved value in the view and in the form instead of showing "New option"?

    I look forward to your response.
    Thank you!

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Auto-populate logical field name based on the table name selected value from a choice

    Hi EBMRay,

    You could created an on change event on Table Name field. Sample code like this:

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

    var optionValue = formContext.getAttribute("jul_tablename").getValue();
    var url = Xrm.Page.context.getClientUrl();
    console.log(optionValue);
    if(1000000001 === optionValue){
    //accounts' url
    console.log(1);
    url += "/api/data/v9.2/EntityDefinitions(LogicalName='account')/Attributes?$filter=LogicalName eq 'emailaddress1' or LogicalName eq 'emailaddress2'";
    }else if (1000000002 === optionValue){
    //contacts' url
    url += "[contacts' url]";
    }else{
    return;
    }
    var req = new XMLHttpRequest();
    req.open("GET",url,true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");

    var startnumber = 1100000001;
    req.onreadystatechange = function () {
    if (req.readyState === 4) {
    var jsonObj = JSON.parse(req.responseText).value;
    console.log(jsonObj);
    for (i = 0; i < jsonObj.length; i++) {
    formContext.getControl("jul_fieldname").addOption({ value: startnumber + i, text: jsonObj[i].LogicalName });
    }
    }
    }

    req.send();
    }

    Please change the contacts' url in the sample and startnumber with yours.

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,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans