Skip to main content
Answered

Mapping 1 PCF Control Choice Field to a Choice Field

editSubscribe (1) ShareShare
ReportReport
Posted on by 132
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!