Skip to main content
Suggested answer

How can I Fetch records and show them within the same Table/Entity based on a field value?

editSubscribe (0) ShareShare
ReportReport
Posted on by 132
Hello, 
 
I have entity called Prospects (lead) and in it I have a global choice field named Country. I have 2 other fields: Yes/No choice field 'Previous Projects in Country' and Text field 'Previous Project Names'.
What I am trying to do using JS, is that while creating a new Prospect when I select the country:
   1) If there are other Prospects with that same Country Value, set  Previous Projects in Country to true or Yes. Else set it to no.
   2) If there are other Prospects with that same Country Value: get their names and show them in Previous Project Names. 
 
I am trying the code below but nothing is working. I also would Ideally want to show the prospect names in a subgrid under each other. I really would appreciate any help.
 
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);
            }
        );
    }
}