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 :
Customer experience | Sales, Customer Insights,...
Answered

i'm not able to send an email on share of account to other user ,i have change some if condition now i'm getting an error as "Unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'Microsoft.Xrm.Sdk.Entity'.

(0) ShareShare
ReportReport
Posted on by 85

namespace LearnPlugin
{
public class send_eamil : IPlugin

{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = servicefactory.CreateOrganizationService(context.UserId);
//
// CrmServiceClient conn = new CrmServiceClient(connectionString);

try
{
if (context.MessageName == "GrantAccess")
{

Entity acc_entity = (Entity)context.InputParameters["Target"];

WhoAmIRequest systemUserRequest = new WhoAmIRequest();
WhoAmIResponse systemUserResponse = (WhoAmIResponse)service.Execute(systemUserRequest);
Guid userId = systemUserResponse.UserId;

Entity fromActivityParty = new Entity("activityparty");
Entity toActivityParty = new Entity("activityparty");
// EntityReference sharedRecord = ((PrincipalAccess)context.InputParameters["PrincipalAccess"]).Principal;
PrincipalAccess PrincipalAccess1 = (PrincipalAccess)context.InputParameters["PrincipalAccess"];
var userOrTeam = PrincipalAccess1.Principal;
var userOrTeamId = userOrTeam.Id;
fromActivityParty["partyid"] = new EntityReference("systemuser", userId);
toActivityParty["partyid"] = new EntityReference(userOrTeam.LogicalName, userOrTeamId);

Entity email = new Entity("email");
email["from"] = new Entity[] { fromActivityParty };
email["to"] = new Entity[] { toActivityParty };
email["regardingobjectid"] = new EntityReference(acc_entity.LogicalName, acc_entity.Id);
email["subject"] = "This is the subject";
email["description"] = "This is the description.";
email["directioncode"] = true;
Guid emailId = service.Create(email);

// Use the SendEmail message to send an e-mail message.
SendEmailRequest sendEmailRequest = new SendEmailRequest
{
EmailId = emailId,
TrackingToken = "",
IssueSend = true
};

}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}

}


}
}

  • Manoj Profile Picture
    85 on at

    can any one help me....it's on plugins i'm learning it.

  • Suggested answer
    meelamri Profile Picture
    13,218 User Group Leader on at

    Hi,

    You need to execute your sendEmailRequest !

    SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailRequest );

    Please refer to this blog for a complete sample code:

    carldesouza.com/.../

    Good Luck !

  • Manoj Profile Picture
    85 on at

    i tried it but not working ,previously i'm able to send simple maillike on update/create.

    now on share i'm using grantaccess but some where it's failing.

  • meelamri Profile Picture
    13,218 User Group Leader on at

    could you share your error logs?

  • Manoj Profile Picture
    85 on at

    Business Process Error

    Unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'Microsoft.Xrm.Sdk.Entity'. If you contact support, please provide the technical details.

    Activity ID: 39d9046a-7735-4ff8-84bb-9b545553d020

    Timestamp: Mon Feb 08 2021 19:04:50 GMT+0530 (India Standard Time)

  • Manoj Profile Picture
    85 on at

    Unhandled exception:

    Exception type: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]

    Message: System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> Microsoft.Crm.CrmException: Unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'Microsoft.Xrm.Sdk.Entity'.

      at Microsoft.Crm.Dialogs.ShareDialogPage.ConfigureForm() +0x5e3

      at Microsoft.Crm.Application.Controls.AppUIPage.OnPreRender(EventArgs e) +0x75

      at Microsoft.Crm.Application.Controls.AppPage.OnPreRender(EventArgs e) +0x7

      at System.Web.UI.Control.PreRenderRecursiveInternal() +0x54

      at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +0x6d3

      --- End of inner exception stack trace ---

      at System.Web.UI.Page.HandleError(Exception e) +0x11b

      at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +0x95a

      at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +0x69

      at System.Web.UI.Page.ProcessRequest() +0x14

      at System.Web.UI.Page.ProcessRequest(HttpContext context) +0x38

      at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +0x18d

      at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +0x64

      at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +0x15: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #6C050651Detail:

    <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts">

     <ActivityId>39d9046a-7735-4ff8-84bb-9b545553d020</ActivityId>

     <ErrorCode>-2147220970</ErrorCode>

     <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" />

     <HelpLink i:nil="true" />

     <Message>System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> Microsoft.Crm.CrmException: Unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'Microsoft.Xrm.Sdk.Entity'.

      at Microsoft.Crm.Dialogs.ShareDialogPage.ConfigureForm() +0x5e3

      at Microsoft.Crm.Application.Controls.AppUIPage.OnPreRender(EventArgs e) +0x75

      at Microsoft.Crm.Application.Controls.AppPage.OnPreRender(EventArgs e) +0x7

      at System.Web.UI.Control.PreRenderRecursiveInternal() +0x54

      at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +0x6d3

      --- End of inner exception stack trace ---

      at System.Web.UI.Page.HandleError(Exception e) +0x11b

      at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +0x95a

      at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +0x69

      at System.Web.UI.Page.ProcessRequest() +0x14

      at System.Web.UI.Page.ProcessRequest(HttpContext context) +0x38

      at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +0x18d

      at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +0x64

      at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +0x15: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #6C050651</Message>

     <Timestamp>2021-02-08T13:34:50.0016635Z</Timestamp>

     <ExceptionRetriable>false</ExceptionRetriable>

     <ExceptionSource i:nil="true" />

     <InnerFault>

       <ActivityId>39d9046a-7735-4ff8-84bb-9b545553d020</ActivityId>

       <ErrorCode>-2147220891</ErrorCode>

       <ErrorDetails xmlns:d3p1="schemas.datacontract.org/.../System.Collections.Generic" />

       <HelpLink i:nil="true" />

       <Message>Unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'Microsoft.Xrm.Sdk.Entity'.</Message>

       <Timestamp>2021-02-08T13:34:50.0016635Z</Timestamp>

       <ExceptionRetriable>false</ExceptionRetriable>

       <ExceptionSource i:nil="true" />

       <InnerFault i:nil="true" />

       <OriginalException i:nil="true" />

       <TraceText>

    [LearnPlugin: LearnPlugin.send_eamil]

    [93d33831-4768-eb11-a812-0022486e9611: LearnPlugin.send_eamil: Create of account]

    </TraceText>

     </InnerFault>

     <OriginalException i:nil="true" />

     <TraceText i:nil="true" />

    </OrganizationServiceFault>

  • Verified answer
    meelamri Profile Picture
    13,218 User Group Leader on at

    Yes,  you need to replace this line:

    Entity acc_entity = (Entity)context.InputParameters["Target"];

    by this:

    EntityReference acc_entity = (EntityReference)context.InputParameters["Target"];

    Then if you need some data from your acc_entity you need to execute a simple retrieve.

  • Manoj Profile Picture
    85 on at

    thank you for your time Mehdi.

  • meelamri Profile Picture
    13,218 User Group Leader on at

    You are welcome !

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 93 Super User 2026 Season 2

#2
Travis South Profile Picture

Travis South 71

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 54 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans