Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

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

(0) ShareShare
ReportReport
Posted on by
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);
            }
        );
    }
}
  • Dengliang Li Profile Picture
    Microsoft Employee on at
    How can I Fetch records and show them within the same Table/Entity based on a field value?
    Hi,
     
    Are you trying to display the results as DropDown?
    Similar to the example provided in the links below.
    Lookdown | PCF Gallery
    Dynamic Dropdown | PCF Gallery

    If so, it is recommended that you consult professional developers for further support.

    Best Regards,
    Dengliang Li
     
  • Joseph Nasr Profile Picture
    on at
    How can I Fetch records and show them within the same Table/Entity based on a field value?
    Hi,
     
    Thank you so much for the reply. It works fine now I thought I already had them wrapped between quotations I apologize. It works fine now. 
    Quick question, is there a way to display the returned results as a list of items instead of a text of names separated by a comma? This would make it more user friendly and clearer.
     
    Thanks again!
  • Suggested answer
    Dengliang Li Profile Picture
    Microsoft Employee on at
    How can I Fetch records and show them within the same Table/Entity based on a field value?
    Hi,
     
    You should use double quotes to wrap strings instead of using /.
     
    When I changed your code as I described, it worked.
     
    If my answer was helpful, please click Like, and if it solved your problem, please mark it as verified to help other community members find more.
    If you have further questions, please feel free to contact me .
     
    Best Regards,
    Dengliang Li
     

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

Overall leaderboard

Product updates

Dynamics 365 release plans