Hello
I have a JS code that worked in crm2011.I updated for crm365. The code retrieve some record and match and show in a sub grid. All things works right expect of matching result to sub grid. Here is the code:
var updateFlag = false;
function updateSubGrid() {
if (updateFlag) return; // Prevent multiple executions
updateFlag = true;
alert("Starting updateSubGrid function");
// Get the related products grid details
var relatedProducts = Xrm.Page.getControl("ContactActivities");
if (!relatedProducts) {
alert("Control 'ContactActivities' not found.");
updateFlag = false; // Reset flag
return;
}
alert("Related Products control found");
// Initialize the lookup field
var lookupField = Xrm.Page.getAttribute("customerid").getValue();
if (!lookupField || lookupField.length === 0) {
alert("No customer selected.");
updateFlag = false; // Reset flag
return;
}
var lookupId = lookupField[0].id.replace("{", "").replace("}", ""); // Extract GUID
alert("Lookup ID: " + lookupId);
// Fetch XML query
var fetchXml = `
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
<entity name='activitypointer'>
<attribute name='subject' />
<attribute name='description' />
<attribute name='createdon' />
<attribute name='statecode' />
<attribute name='ownerid' />
<attribute name='activitytypecode' />
<attribute name='regardingobjectid' />
<order attribute='createdon' descending='false' />
<link-entity name='activityparty' from='activityid' to='activityid' alias='aa'>
<filter type='and'>
<condition attribute='partyid' operator='eq'
uiname='Contact Name' uitype='contact' value='${lookupId}' />
</filter>
</link-entity>
</entity>
</fetch>`;
alert("Fetch XML: " + fetchXml);
// Fetch data using Xrm.WebApi
Xrm.WebApi.retrieveMultipleRecords("activitypointer", "?fetchXml=" + encodeURIComponent(fetchXml)).then(
function success(result) {
var items = result.entities;
if (items.length > 0) {
alert("Data retrieved successfully. Count: " + items.length);
items.forEach(function(item) {
console.log("Activity Subject: " + item.subject);
});
// Ensure the grid is ready before attempting to bind data
ensureGridReady(relatedProducts, items);
} else {
alert("No data found.");
updateFlag = false; // Reset flag
}
},
function error(error) {
console.error("Error in fetch request: ", error.message);
alert("Error in fetch request: " + error.message);
updateFlag = false; // Reset flag
}
);
}
function ensureGridReady(relatedProducts, items) {
var retryCount = 0;
var maxRetries = 10; // Increase the retry limit to ensure the grid is ready
function checkGrid() {
if (retryCount >= maxRetries) {
alert("Max retries reached. The grid might not be ready or there is another issue.");
updateFlag = false; // Reset flag
return;
}
try {
var gridControl = relatedProducts.getGrid();
if (gridControl) {
alert("Grid control is ready");
// Bind data to the grid
bindDataToGrid(relatedProducts, items);
alert("Data bound successfully. Attempting to refresh the grid.");
relatedProducts.refresh();
// Verify if records are set in the grid
setTimeout(function() {
var gridControl = relatedProducts.getGrid();
var gridRows = gridControl.getRows();
if (gridRows.getLength() > 0) {
alert("Related Products grid updated with new records");
} else {
alert("No records found in the Related Products grid");
}
updateFlag = false; // Reset flag
}, 2000); // Wait for 2 seconds before checking
} else {
retryCount++;
console.log("Grid control not ready, retrying... (" + retryCount + ")");
setTimeout(checkGrid, 1000); // Retry after 1 second
}
} catch (e) {
console.error("Error checking grid: ", e);
alert("Error checking grid: " + e.message);
updateFlag = false; // Reset flag
}
}
checkGrid();
}
function bindDataToGrid(relatedProducts, items) {
try {
// Implement a function to bind the data to the subgrid
alert("Binding data to subgrid. Item count: " + items.length);
// In this context, we assume data binding means just refreshing the grid
relatedProducts.refresh();
} catch (e) {
console.error("Error binding data to grid: ", e);
alert("Error binding data to grid: " + e.message);
updateFlag = false; // Reset flag
}
}