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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Invalid Cast System.Activities.InArguement<Microsoft.Xrm.Sdk.EntityReference> to string

(0) ShareShare
ReportReport
Posted on by 100

Hello, I am trying to create a custom workflow to attach notes attachment and send it. I am stuck at the conversion. I cant get around it. Please help! I have highlighted the error below. 


[Input("SourceEmail")]
[ReferenceTarget("systemuser")]
public InArgument<EntityReference> SourceEmail { get; set; }

[Input("TargetEmail")]
[ReferenceTarget("systemuser")]
public InArgument<EntityReference> TargetEmail { get; set; }

[Input("Subject")]
public InArgument<string> Subject { get; set; }

[Input("Description")]
public InArgument<string> Description { get; set; }

protected override void Execute(CodeActivityContext executionContext)

{
// Get workflow context

IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

//Create service factory

IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();

// Create Organization service

IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

// Get the target entity from the context
Entity Attachid = service.Retrieve("activitypointer", context.PrimaryEntityId, new ColumnSet(new string[] { "activityid" }));
Guid emailid = Guid.Empty;
Entity email = new Entity("email");
Entity fromParty = new Entity("activityparty");
Entity toParty = new Entity("activityparty");
fromParty["partyid"] = new EntityReference(SourceEmail); <-------------Error Here------------>
email.Attributes["subject"] = Subject;
email.Attributes["description"] = Description;
emailid = service.Create(email);
AddAttachmentToEmailRecord(service, Attachid.Id,email);

47471.Capture.PNG

*This post is locked for comments

I have the same question (0)
  • RARO91 Profile Picture
    100 on at
    RE: Invalid Cast System.Activities.InArguement<Microsoft.Xrm.Sdk.EntityReference> to string

    (SourceEmail.Get<string>(executionContext)) worked. I figured it. Thank you so much everyone!!!!

  • erhan.keskin Profile Picture
    2,253 on at
    RE: Invalid Cast System.Activities.InArguement<Microsoft.Xrm.Sdk.EntityReference> to string

    Hi,

    "SourceEmail" is an InArgument type, not an EntityReference. It is needed to read EntityReference from it.

    E.g. EntityReference sourceEmailReference = SourceEmail.Get(executionContext);

    Change your line like this; fromParty["partyid"] = new EntityReference(sourceEmailReference);

    and do not forget to assign fromParty and toParty to the email entity.

    email["from"] = new Entity[] { fromParty };

    email["to"] = new Entity[] { toParty };

    Regards,

  • Suggested answer
    Temmy Wahyu Raharjo Profile Picture
    2,914 on at
    RE: Invalid Cast System.Activities.InArguement<Microsoft.Xrm.Sdk.EntityReference> to string

    Hi,

    You can refer to this link https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/workflow/sample-create-custom-workflow-activity

    Supposed when you want to take the attribute value you do it like this:

    fromParty["partyid"] = SourceEmail.Get<EntityReference>(executionContext);

  • Suggested answer
    ajyendra Profile Picture
    1,738 on at
    RE: Invalid Cast System.Activities.InArguement<Microsoft.Xrm.Sdk.EntityReference> to string

    Hi ,

    Try this :

    new EntityReference("systemuser", SourceEmail);

    Thanks

    Ajyendra

    If this answer is helpful to you . Please Mark as Verified.

  • ajyendra Profile Picture
    1,738 on at
    RE: Invalid Cast System.Activities.InArguement<Microsoft.Xrm.Sdk.EntityReference> to string

    Hi ,

    Try this :

    new EntityReference("systemuser", SourceEmail);

    Thanks

    Ajyendra

    If this answer is helpful to you . Please Mark as Verified.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#2
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans