Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Mapping 1 PCF Control Choice Field to a Choice Field

Posted on by 166
Hello, 
 
I am using the PCF Control Country Picker: Country Picker | PCF Gallery This allows me to choose a country from a field named 'cra1c_countriespcf' on which this pcf control is applied.
 
The thing is, I already have a Choice field called new_ti_countries, synced with global choice option-set 'new_ti_country' which contains Country names as labels and their values.
 
Since the Country field that is synced with the global option-set was the field previously adopted and used where multiple events are connected to it and it is related to around 5 other fields, and since it is different from the Country field on which the PCF control is applied, I am trying to set the choice selected in the newly created PCF Control Country Field as the choice selected in the old Country field. (See Image Below)
 
//////
 
I am trying to get the value of the selected choice in the PCF Control field (France in the example above), find the Value of Label France in the global option-set, and set that fetched value as the selected choice of the old 'Country' field. My problem is that the line '
var selectedCountryValue = Xrm.Page.getAttribute(/cra1c_countriespcf/).getValue();
' is returning FRA instead of France. FRA does not exist as a label in my option-set. This is the case with any other selected choice: instead of getting New Zealand, I get NZL, AUS instead of Australia, etc...
 
// JavaScript function to find and set the corresponding value in the new_ti_countries fieldfunction setNewTiCountriesValue() {    // Get the selected country value from cra1c_countriespcf field    var selectedCountryValue = Xrm.Page.getAttribute(/cra1c_countriespcf/).getValue();    // Check if the selected country value is not null    if (selectedCountryValue !== null) {        // Log the selected country value for debugging        console.log(/Selected Country Value:/, selectedCountryValue);        // Fetch the global choice option-set new_ti_countries        var globalOptionSet = Xrm.Page.getAttribute(/new_ti_countries/).getOptions();        // Log all available values in new_ti_countries for debugging        console.log(/Available Values in new_ti_countries:/);        for (var i = 0; i < globalOptionSet.length; i++) {            console.log(globalOptionSet[i].value, globalOptionSet[i].text);            if (globalOptionSet[i].value === selectedCountryValue) {                // Set the corresponding value to the new_ti_countries field                Xrm.Page.getAttribute(/new_ti_countries/).setValue(selectedCountryValue);                return;            }        }        console.error(/Corresponding value not found for the selected country./);    }}
 
I would highly appreciate any hints on how to get the common name of the country instead of its code. I think it has to do with the control, but I can't seem to fetch it. Thank you in advance for any help!
  • Leah Ju Profile Picture
    Leah Ju Microsoft Employee on at
    Mapping 1 PCF Control Choice Field to a Choice Field
    Hi Partner,
    Agreed!
    Your method is presumably the most effective one.
    We can't change the country name inside the PCF control, we can only figure it out on our own customisable country field.
     
  • Verified answer
    Joseph Nasr Profile Picture
    Joseph Nasr 166 on at
    Mapping 1 PCF Control Choice Field to a Choice Field
    Hello Leah,
     
    I managed to make it work. getText() did not work, but I created a hardcoded list of country codenames mapped to an array of the Country name and Country Index based on how they are in the global choice like below:
     
    var countryCodeNames = {
    'FRA' : ["France", 24],
    'AUS': ["Australia, 26],
    ...
    }
     
    Then I use getValue() to get the codename of the selected result, and using the object above, I set its country index as the value of my other field. I know it's hardcoded, but it works and I don't think anyone will be changing country names soon :P
     
  • Suggested answer
    Leah Ju Profile Picture
    Leah Ju Microsoft Employee on at
    Mapping 1 PCF Control Choice Field to a Choice Field
    Choice field?
    I notice that you use 'getValue()' fucntion in your code.
    Maybe you can try to use 'getText()' function, which returns a string value of the text for the currently selected option for a choice or choices column.

    I hope you can mark my answer verified if it is helpful! If you have any questions, please feel free to contact me.
    Regards,
    Leah
     

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans