Blog 7: Resolving Message Size Exceeded When Sending Context to Sandbox

Dataverse plug-ins are powerful tools for extending business logic, but sometimes, they run into issues that can disrupt operations. One such problem is the “Message size exceeded when sending context to Sandbox” error.

This error occurs when a plug-in operation generates a message payload larger than 116.85 MB, causing it to fail. In this blog, we’ll explore the causes, troubleshooting methods, and best practices to prevent this error in Dataverse plug-ins.


🚨 Understanding the Error

Error Message:

Error Code: -2147220970  
Message: Message size exceeded when sending context to Sandbox. Message size: ### Mb

This error occurs when a plug-in’s execution context is too large and surpasses the 116.85 MB limit imposed by Dataverse.


🔍 Why Does This Happen?

The most common causes include:

1️⃣ Large Data Volumes – The plug-in is retrieving a large number of records, attachments, or binary data in a single request.
2️⃣ RetrieveMultiple Queries with File Attachments – A plug-in triggers a query that returns emails with attachments, causing the response size to exceed the limit.
3️⃣ Processing Multiple Related Records – The plug-in attempts to retrieve and process multiple related records (e.g., retrieving all contacts linked to an account).
4️⃣ Improperly Designed Business Logic – The plug-in attempts to store too much data in the execution context, causing unnecessary overhead.


🔧 How to Troubleshoot and Fix the Issue

1️⃣ Identify the Plug-in Causing the Issue

The first step is to identify the plug-in that is exceeding the message size limit. To do this:

  • Review the Plug-in Trace Logs in Dataverse
  • Look for entries that reference large data retrieval or multiple related records
  • Check the error message to identify the specific operation that exceeded the size limit

2️⃣ Optimize Data Retrieval in Plug-ins

One of the most effective solutions is to reduce the amount of data being retrieved by the plug-in.

🔹 Use Retrieve Instead of RetrieveMultiple

Instead of retrieving multiple records with related data, fetch only the necessary record using Retrieve.

// Instead of using RetrieveMultiple
QueryExpression query = new QueryExpression("email");
query.ColumnSet = new ColumnSet("subject", "attachment");
EntityCollection emails = service.RetrieveMultiple(query);

// Use Retrieve to fetch only a single email
Entity email = service.Retrieve("email", emailId, new ColumnSet("subject", "attachment"));

🔹 Limit the Columns Retrieved

If you must use RetrieveMultiple, ensure that only essential columns are retrieved. Avoid fetching entire records when you only need specific fields.

QueryExpression query = new QueryExpression("email");
query.ColumnSet = new ColumnSet("subject"); // Only retrieve necessary columns
EntityCollection emails = service.RetrieveMultiple(query);

🔹 Paginate Large Datasets

If retrieving multiple records is necessary, implement paging to avoid exceeding the message size limit.

QueryExpression query = new QueryExpression("email");
query.PageInfo = new PagingInfo { PageNumber = 1, Count = 50 }; // Retrieve 50 records at a time
EntityCollection emails;
do
{
emails = service.RetrieveMultiple(query);
foreach (Entity email in emails.Entities)
{
// Process email records
}
query.PageInfo.PageNumber++;
} while (emails.MoreRecords);

📌 Best Practices to Avoid Message Size Errors

Retrieve only necessary data – Avoid fetching unnecessary records or attributes.
Implement paging for large queries – Process data in smaller chunks.
Optimize plug-in logic – Avoid storing excessive data in the execution context.


📢 Coming Up Next…

In Blog 8, we’ll cover “The Given Key Wasn’t Present in the Dictionary” error and how to handle missing attributes in Dataverse plug-ins.

Stay tuned for more troubleshooting tips! 🚀

Leave a comment