web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Email - Settings status back to Sent

(0) ShareShare
ReportReport
Posted on by 1,540

I need to do some work on old emails so I've created a console app that retrieves the emails, sets the status to draft, does the work then sets the status back to sent/received.

This works fine on received emails but I'm getting an error on setting the status back to sent. I've checked the Entity Reference logical name and id are populated correctly and the state and status of 1 and 3 are correctly set.

I've read that SetStateRequest is deprecated (I'm on v9.0) but it is working elsewhere and I'm not sure if that's for a message trigger only, or for setting a record status via SDK.

OptionSetValue state = (OptionSetValue)eEmail["statecode"]; //get original status
OptionSetValue status = (OptionSetValue)eEmail["statuscode"];

Console.Write("Set Email to Draft:");
SetStateRequest openRequest = new SetStateRequest
{
    EntityMoniker = new EntityReference("email", eEmail.Id),
    State = new OptionSetValue(0),
    Status = new OptionSetValue(1)
};
Console.WriteLine(" Execute");
service.Execute(openRequest);

Console.Write("Update Email:");
Entity eEmailUpdate = new Entity("email");
eEmailUpdate.Id = eEmail.Id;
//do work
service.Update(eEmailUpdate);

Console.Write("Reset Closed Status:");
SetStateRequest closeRequest = new SetStateRequest
{
    EntityMoniker = new EntityReference("email", eEmail.Id),
    State = state,
    Status = status
};
Console.WriteLine(" Execute");
SetStateResponse stateSet = (SetStateResponse)service.Execute(closeRequest); //gives error
Console.WriteLine("Done");


Error: System.ServiceModel.FaultException`1: 'An unexpected error occurred.'

Some traces from VS if helpful

System.ServiceModel.FaultException`1
  HResult=0x80131501
  Message=An unexpected error occurred.
  Source=mscorlib
  StackTrace:
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at Microsoft.Xrm.Sdk.IOrganizationService.Execute(OrganizationRequest request)
   at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.ExecuteCore(OrganizationRequest request)
   at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Execute(OrganizationRequest request)
   at EmailBody.Program.Main(String[] args) in C:\Users\Chris.Crawshaw\OneDrive - Voyager Software\Plugins\DocumentProcessing\EmailBody\Program.cs:line 84


RemoteStackTrace    "\r\n
Server stack trace: \r\n
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)\r\n
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)\r\n
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)\r\n
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)\r\n\r\n
Exception rethrown at [0]: \r\n
"    string

StackTrace
Microsoft.Crm.CrmException: An unexpected error occurred. ---> System.NullReferenceException: Object reference not set to an instance of an object.\r\n
   at Microsoft.Crm.Common.ObjectModel.EmailEngagementTrackingManager.RemoveSentEmailTrackingInformation(BusinessEntityMoniker moniker, Email email, ExecutionContext context)\r\n
   at Microsoft.Crm.Common.ObjectModel.EmailEngagementTrackingManager.ProcessSentEmail(BusinessEntityMoniker moniker, ExecutionContext context)\r\n   at Microsoft.Crm.Common.ObjectModel.EmailService.InternalSetState(BusinessEntityMoniker moniker, Int32 newState, Int32 newStatusCode, BusinessEntity originalEntity, ExecutionContext context)\r\n   at Microsoft.Crm.BusinessEntities.BusinessProcessObject.SetState(BusinessEntityMoniker moniker, Int32 newState, Int32 newStatusCode, BusinessEntity originalEntity, ExecutionContext context)\r\n   at Microsoft.Crm.Common.ObjectModel.EmailService.SetState(BusinessEntityMoniker moniker, Int32 newState, Int32 newStatusCode, ExecutionContext context)\r\n   at Microsoft.Crm.Common.ObjectModel.EmailService.Microsoft.Crm.Sdk.IStateful.SetStateStatus(BusinessEntityMoniker moniker, Int32 state, Int32 status, ExecutionContext context)\r\n   --- End of inner exception stack trace ---\r\n   at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context)\r\n   at Microsoft.Crm.Extensibility.PipelineInstrumentationHelper.Execute(Boolean instrumentationEnabled, String stopwatchName, ExecuteWithInstrumentation action, PipelineExecutionContext context)\r\n   at Microsoft.Crm.Extensibility.Pipeline.<>c__DisplayClass2_1.<Execute>b__0()"    object {string}


                         StackTrace    "\r\n
Server stack trace: \r\n
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)\r\n
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)\r\n
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)\r\n
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)\r\n\r\n
Exception rethrown at [0]: \r\n
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)\r\n
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)\r\n
   at Microsoft.Xrm.Sdk.IOrganizationService.Execute(OrganizationRequest request)\r\n
   at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.ExecuteCore(OrganizationRequest request)\r\n
   at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Execute(OrganizationRequest request)\r\n
   at EmailBody.Program.Main(String[] args) in C:\\Users\\Chris.Crawshaw\\OneDrive - Voyager Software\\Plugins\\DocumentProcessing\\EmailBody\\Program.cs:line 93"    string

*This post is locked for comments

I have the same question (0)
  • Radu Chiribelea Profile Picture
    6,667 on at

    Hi Chris,

    Have you tried using the Update request instead of the SetStateRequest - see docs.microsoft.com/.../dn932124%28v%3dcrm.8%29 ?

    Does that make any difference?

    Regards,

    Radu

  • ChrisJC Profile Picture
    1,540 on at

    Hi, I've just tried using the update method and I get the same error.

    Note this is only when setting the status back to Sent. It works fine when setting it to draft.

  • Suggested answer
    Radu Chiribelea Profile Picture
    6,667 on at

    What if you try a different approach. After setting the email to draft use the SendEmailRequest to send out the email - docs.microsoft.com/.../gg308290(v%3Dcrm.8) . However set the IssueSend Property so that the email appears as sent and not sent out for real docs.microsoft.com/.../gg332546%28v%3dcrm.8%29

    The downside of this is that the email will appear as being sent on the date when you ran the SendEmailRequest - so you would lose this details.

    Hope this helps,

    Radu

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans