Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
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'.

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);
}

}


}
}

Categories:
  • meelamri Profile Picture
    meelamri 13,198 User Group Leader on at
    RE: 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

    You are welcome !

  • Manoj  Profile Picture
    Manoj 85 on at
    RE: 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

    thank you for your time Mehdi.

  • Verified answer
    meelamri Profile Picture
    meelamri 13,198 User Group Leader on at
    RE: 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

    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
    Manoj 85 on at
    RE: 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

    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>

  • Manoj  Profile Picture
    Manoj 85 on at
    RE: 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

    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)

  • meelamri Profile Picture
    meelamri 13,198 User Group Leader on at
    RE: i'm not able to send an email on share of account to other user ,not able debug aswell

    could you share your error logs?

  • Manoj  Profile Picture
    Manoj 85 on at
    RE: i'm not able to send an email on share of account to other user ,not able debug aswell

    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.

  • Suggested answer
    meelamri Profile Picture
    meelamri 13,198 User Group Leader on at
    RE: i'm not able to send an email on share of account to other user ,not able debug aswell

    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
    Manoj 85 on at
    RE: i'm not able to send an email on share of account to other user ,not able debug aswell

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

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,564 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,651 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans