I am trying to send an email on update of the queueItem entity using pluginEventContext.GetOrgService(userId).
*** Side question here, what is the difference between using that and using pluginEventContext.XrmContext.OrgService? But I tried both and same error.
An exception has occurred in the [CSF.Xrm.Plugins.QueueItem.SendEmailPlugin].\nAn unhandled organization service fault occurred during execution of the plugin.\n{System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=9.0.0.0, Culture=neutral, PublicKeyToken=]]} from {Microsoft.CDSRuntime.SandboxWorker} - Entity 'Contact' With Id = 74f7aff7-50e7-ed11-a7c6-002248081803 Does Not Exist
Notice that the id on the error, it is the same as the regardingobjectid. But the type of object is queueItem not contact.
This is what is being sent to service.Create(email):
{"LogicalName":"email","Id":"00000000-0000-0000-0000-000000000000","Attributes":[{"Key":"from","Value":[{"LogicalName":"activityparty","Id":"00000000-0000-0000-0000-000000000000","Attributes":[{"Key":"partyid","Value":{"Id":"2f9953be-882c-ed11-9db1-000d3a334aeb","LogicalName":"systemuser","Name":null,"KeyAttributes":[],"RowVersion":null}}],"EntityState":null,"FormattedValues":[],"RelatedEntities":[],"RowVersion":null,"KeyAttributes":[]}]},{"Key":"to","Value":[{"LogicalName":"activityparty","Id":"00000000-0000-0000-0000-000000000000","Attributes":[{"Key":"partyid","Value":{"Id":"241e8a77-1b59-ec11-8f8f-002248083b75","LogicalName":"systemuser","Name":null,"KeyAttributes":[],"RowVersion":null}}],"EntityState":null,"FormattedValues":[],"RelatedEntities":[],"RowVersion":null,"KeyAttributes":[]}]},{"Key":"subject","Value":"Letter from Unassigned - SSN: Unassigned - UMI: Unassigned Assigned"},{"Key":"description","Value":"">mycompany-dev.crm.dynamics.com/.../main.aspx"Key":"regardingobjectid","Value":{"Id":"74f7aff7-50e7-ed11-a7c6-002248081803","LogicalName":"queueitem","Name":null,"KeyAttributes":[],"RowVersion":null}}],"EntityState":null,"FormattedValues":[],"RelatedEntities":[],"RowVersion":null,"KeyAttributes":[]}.
public static bool Send(Guid fromPartyId , Guid toPartyId, Entity entity, Guid entityId, string subject, string body, IOrganizationService service, ILogger logger)
{
var email = new Entity("email");
var toParty = new Entity("activityparty")
{
["partyid"] = new EntityReference("systemuser", toPartyId) // target
};
var fromParty = new Entity("activityparty")
{
["partyid"] = new EntityReference("systemuser", fromPartyId) //pre
};
email["from"] = new[] { fromParty };
email["to"] = new[] { toParty };
email["subject"] = subject;
email["description"] = body;
email["regardingobjectid"] = entity.ToEntityReference();
logger.LogInformation($"email: {JsonConvert.SerializeObject(email)}");
var emailId = service.Create(email);
var sendEmailRequest = new SendEmailRequest
{
EmailId = emailId,
//TrackingToken = "",
IssueSend = true
};
var response = (SendEmailResponse)service.Execute(sendEmailRequest);
logger.LogInformation(JsonConvert.SerializeObject(response));
return true;
}
}