function setPreviousProjectsInCountry(executionContext){
// Get the form context
var formContext = executionContext.getFormContext();
// Get the chosen Country regarding the prospect
var selectedCountryControl = Xrm.Page.getControl(/new_ti_countries/);
var perviousProjectsControl = Xrm.Page.getControl(/cra1c_previousprojectsincountry/);
var previousProjectNamesControl = Xrm.Page.getControl(/cra1c_previousprojects/);
if(selectedCountryControl!=null){
var selectedCountry = selectedCountryControl.getAttribute().getValue();
// Retrieve Prospect Records with the selected country
Xrm.WebApi.retrieveMultipleRecords(/lead/, /?$filter=new_ti_countries eq / + selectedCountry).then(
function success(result) {
if (result.entities.length > 0) {
// If Prospect Records exist, set Yes/No field value to Yes
formContext.getAttribute(/cra1c_previousprojectsincountry/).setValue(true); // Replace /cra1c_previousprojectsincountry/ with the schema name of your Yes/No field
// Display Prospect Names in a new field (e.g., new_displaynames)
var prospectNames = result.entities.map(function (entity) {
return entity[/subject/];
}).join(/, /);
formContext.getAttribute(/cra1c_previousprojects/).setValue(prospectNames); // Replace /new_displaynames/ with the schema name of the field to display names
} else {
// If no Prospect Records found, set Yes/No field value to No
formContext.getAttribute(/cra1c_previousprojectsincountry/).setValue(false); // Replace /new_yesno/ with the schema name of your Yes/No field
formContext.getAttribute(/cra1c_previousprojects/).setValue(null); // Clear the Prospect Names field
}
},
function error(error) {
console.log(error.message);
}
);
}
}