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".