Issues with Xrm.WebApi.retrieveMultipleRecords

Hexcode 0 Reputation points
2024-08-25T16:53:46.1066667+00:00

I am trying to display subgrid based on the value found in the form field.

for that I am using below code but unable to execute Xrm.WebApi.retrieveMultipleRecords

code:

function changeSubgridView(context) {

var formContext = context.getFormContext();

var fieldValue = formContext.getAttribute("x").getValue(); // Replace 'x' with the actual logical name of your field

console.log("Field Value: " + fieldValue);

**Xrm.WebApi.retrieveMultipleRecords("viewmappings", "?$filter=new_fieldvalue eq '" + fieldValue + "'")**.then(

    function success(result) {

        console.log("Retrieved Records: ", result.entities);

        if (result.entities.length > 0) {

            var viewMapping = result.entities[0];

            console.log("View Mapping: ", viewMapping);

            var viewToSet = {

                entityType: "contact", // Replace with the correct logical name

                id: viewMapping.viewid,

                name: viewMapping.viewname

            };

            console.log("View to Set: ", viewToSet);

            var gridContext = formContext.getControl("Contacts");

            gridContext.getViewSelector().setCurrentView(viewToSet);

            gridContext.setVisible(true);

            formContext.ui.clearFormNotification("viewNotFound");

        } else {

            console.log("No matching view found.");

            formContext.ui.setFormNotification("No matching view found for the selected option.", "ERROR", "viewNotFound");

            formContext.getControl("Contacts").setVisible(false);

        }

    },

    function error(error) {

        console.log("Error retrieving view mappings: " + error.message);

    }

);

}

while in debug mode the it doesn’t go inside the Xrm.webapi

Would appreciate your help. Thanks in advance

JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
996 questions
Microsoft Copilot for Microsoft 365 Development
Microsoft Copilot for Microsoft 365 Development
Microsoft Copilot for Microsoft 365: Microsoft 365 Copilot refers collectively to Copilot experiences within Microsoft 365 applications.Development: The process of researching, productizing, and refining new or existing technologies.
154 questions
Dynamics 365 Training
Dynamics 365 Training
Dynamics 365: A Microsoft cloud-based business platform that provides customer relationship management and enterprise resource planning solutions.Training: Instruction to develop new skills.
146 questions
Microsoft Power Platform Training
Microsoft Power Platform Training
Microsoft Power Platform: An integrated set of Microsoft business intelligence services.Training: Instruction to develop new skills.
454 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Vivek Bhakta MSFT 20 Reputation points Microsoft Vendor
    2024-09-12T10:21:38.5966667+00:00

    Ensure the Field Name is Correct: Double-check that the logical name of the field you're using in formContext.getAttribute("x").getValue(); is correct.

    Check for Errors in the Console: Make sure there are no errors in the browser console that might be preventing the execution of the Xrm.WebApi.retrieveMultipleRecords function.

    Verify the Query: Ensure that the query string "?$filter=new_fieldvalue eq '" + fieldValue + "'" is correctly formatted and that new_fieldvalue is the correct logical name of the field in the viewmappings entity.

    Check Permissions: Ensure that the user has the necessary permissions to retrieve records from the viewmappings entity.

    Debugging: Add additional logging to see where the code might be failing. For example, log the entire query URL before calling Xrm.WebApi.retrieveMultipleRecords.

    You may try this modified version of your code with additional logging:

    function changeSubgridView(context) {

       var formContext = context.getFormContext();

       var fieldValue = formContext.getAttribute("x").getValue(); // Replace 'x' with the actual logical name of your field

      console.log("Field Value: " + fieldValue);

     

       var query = "?$filter=new_fieldvalue eq '" + fieldValue + "'";

      console.log("Query: " + query);

     

      Xrm.WebApi.retrieveMultipleRecords("viewmappings", query).then(

           function success(result) {

              console.log("Retrieved Records: ", result.entities);

               if (result.entities.length > 0) {

                   var viewMapping = result.entities;

                  console.log("View Mapping: ", viewMapping);

                   var viewToSet = {

                      entityType: "contact", // Replace with the correct logical name

                      id: viewMapping.viewid,

                      name: viewMapping.viewname

                   };

                  console.log("View to Set: ", viewToSet);

                   var gridContext = formContext.getControl("Contacts");

                  gridContext.getViewSelector().setCurrentView(viewToSet);

                  gridContext.setVisible(true);

                  formContext.ui.clearFormNotification("viewNotFound");

               } else {

                  console.log("No matching view found.");

                  formContext.ui.setFormNotification("No matching view found for the selected option.", "ERROR", "viewNotFound");

                  formContext.getControl("Contacts").setVisible(false);

               }

           },

           function error(error) {

              console.log("Error retrieving view mappings: " + error.message);

           }

       );

    }

    Please Refer to this link

    retrieveMultipleRecords (Client API reference) in model-driven apps - Power Apps | Microsoft Learn

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.