Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

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

  • RARO91 Profile Picture
    RARO91 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
    erhan.keskin 2,247 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
    Temmy Wahyu Raharjo 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
    ajyendra 1,730 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
    ajyendra 1,730 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans