Hey,
I have been running into an odd error when attempting to migrate from the Dynamics CRM SDK SDK.REST.js to the replacement API Xrm.WebApi. As the old client library uses deprecated enpoint /XRMServices/2011/OrganizationData.svc (See: https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/best-practices/business-logic/do-not-use-odata-v2-endpoint?tabs=odatav2)
In one use case, the library method, as shown, returns an error According to the plugin traces, and console error below the .NET Plugin runs successfully, and runs into unexpected error in client library.
CLIENT:
function executeQuery(queryString, callback) {
try {
var fetchXml = "?fetchXml=<fetch mapping='logical'><entity name='{entity_name}'><filter type='and'><condition attribute='{attribute_here}' operator='eq' value='" + queryString + "'/></filter></entity></fetch>";
window.parent.Xrm.WebApi.retrieveMultipleRecords("{entity_name}", fetchXml).then(
(result) => {
console.log(result)
var ret = JSON.parse(result[0].{second_attribute});
callback(ret);
},
).catch((err) => {
console.error(err)
errorHandler(err);
})
} catch (e) {
errorHandler(e);
}
}
CLIENT ERROR:
"{"errorCode":2147951877,"message":"Value cannot be null.\r\nParameter name: entityMetadata","code":2147951877,"title":"","raw":"{\"_errorCode\":-2147015419,\"_errorFault\":{\"_responseXml\":null,\"_errorCode\":0,\"_innerFault\":null,\"_callStack\":null,\"_responseText\":\"{\\\"error\\\":{\\\"code\\\":\\\"0x0\\\",\\\"message\\\":\\\"Value cannot be null.\\\\r\\\\nParameter name: entityMetadata\\\"}}\",\"_annotations\":{},\"_hasCustomerInfo\":false,\"_messages\":[\"Value cannot be null.\\r\\nParameter name: entityMetadata\"]},\"_message\":\"Value cannot be null.\\r\\nParameter name: entityMetadata\",\"_exception\":null,\"_innerError\":null,\"_errorSource\":3,\"_blockErrorReporting\":false,\"_stack\":null,\"_faultedRequestIndex\":-1,\"_clientRequestId\":\"7ce6bab7-015b-4f51-a8c0-d445e3263bed\",\"_serverResponseId\":\"0b21f478-e28b-45a1-bf1a-7a1eb7ef059f\",\"_httpStatusCode\":500,\"_retryAfter\":null,\"_date\":\"2025-02-15T13:05:37.000Z\",\"_isRequestTooLong\":false,\"message\":\"Value cannot be null.\\r\\nParameter name: entityMetadata\",\"name\":\"Error\",\"stack\":\"No stack available.\",\"__action\":{\"source\":{\"actionType\":\"modernDataSource.execute.odata.async\",\"actionStack\":\"modernDataSource.execute.odata.async\",\"contextId\":\"5ecadfdc-de49-4cb4-a464-cc24d0fcd0f3\"},\"child\":{\"actionType\":\"oData.retrieveMultiple.async\",\"actionStack\":\"oData.retrieveMultiple.async\",\"contextId\":\"52ea020a-e27d-43be-a483-3777993d437c\"}}}"}"
I have identified a piece of code that seems to trigger the error. It specifically happens when creating/handling a new Entity during Fetch Expression query and passing it to OutputParameters["BusinessEntityCollection"] in EntityCollection as shown in the code snippet below.
PLUGIN:
//NOTE: Critical information unique to implementation has been hidden. By replacing values with bracketed placeholders.
var e = new Entity("{entityname}");
e.Attributes.Add("{first_attribute}", localContext.PluginExecutionContext.PrimaryEntityId);
e.Attributes.Add("{second_attribute}", result);
var col = new EntityCollection();
col.Entities.Add(e);
localContext.PluginExecutionContext.OutputParameters["BusinessEntityCollection"] = col;
Any ideas on how to get around this or mitigate the issue?
Thank you in advance.