Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum
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 139
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
    Dengliang Li 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
    Joseph Nasr 139 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
    Dengliang Li 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

Community Spotlight of the Month

Kudos to Mohamed Amine Mahmoudi!

Blog subscriptions now enabled!

Follow your favorite blogs

TechTalk: How Dataverse and Microsoft Fabric powers ...

Explore the latest advancements in data export and integration within ...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 284,876 Super User

#2
Ludwig Reinhard Profile Picture

Ludwig Reinhard Microsoft Employee

#3
Martin Dráb Profile Picture

Martin Dráb 225,425 Super User

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans