Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Custom workflow activity - Output argument problem

(0) ShareShare
ReportReport
Posted on by

Hello,

I'm trying to create a custom workflow activity to get the contact entity reference from the email.

The goal is to use this functionnality when I create a email in order to get the link on the contact from the email address.

And I have a problem : I can't select my output argument in the update form of the workflow :

Here is my code :

using System.Activities;

using Microsoft.Crm.Sdk.Messages;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Workflow;

using Microsoft.Xrm.Sdk.Query;

using Microsoft.Xrm.Sdk.Messages;

using System;

using Microsoft.Xrm.Sdk.Client;

namespace ActivityLibrary3

{

public class CodeActivity1 : CodeActivity

{

[Input("To")]

public InArgument<string> to { get; set; }

 

[ReferenceTarget("contact")]

[Output("ContactId")]

public OutArgument<EntityReference> ContactId { get; set; }

// If your activity returns a value, derive from CodeActivity<TResult>

// and return the value from the Execute method.

protected override void Execute(CodeActivityContext executionContext)

{

// Obtain the runtime value of the Text input argument

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

//Create an Organization Service

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

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

// Create query using querybyattribute

QueryByAttribute querybyexpression = new QueryByAttribute("contact");

querybyexpression.ColumnSet = new ColumnSet("contactid", "emailaddress1");

// Attribute to query

querybyexpression.Attributes.AddRange("emailaddress1");

string email = this.to.Get(executionContext);

// Value of queried attribute to return

querybyexpression.Values.AddRange(email);

// Query passed to the service proxy

EntityCollection retrieved = service.RetrieveMultiple(querybyexpression);

// Returned value

foreach (var c in retrieved.Entities)

{

this.ContactId.Set(executionContext, c.Attributes["contactid"]);

break;

}

//Guid contact_id = new Guid();

//foreach (Entity entity in retrieved.Entities)

//{

// contact_id = ((EntityReference)entity.Attributes["contactid"]).Id;

// break;

//}

//this.ContactId.Set(executionContext, contact_id);

}

}

}

I've tried different ways (one is in green at the end of the code) but now, I don't know why I can't select my output parameter.

Thank you for your help !!

David

*This post is locked for comments

  • Community Member Profile Picture
    on at
    RE: Custom workflow activity - Output argument problem

    In fact, the problem of the output argument unavailable in the workflow form seems to be a Firefox problem.

    With IE, I have no issue.

    I've tried several codes and this one doesn't generate any issue during the execution, but my target field is empty as a result :

    using System.Activities;

    using Microsoft.Crm.Sdk.Messages;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Workflow;

    using Microsoft.Xrm.Sdk.Query;

    using Microsoft.Xrm.Sdk.Messages;

    using System;

    using Microsoft.Xrm.Sdk.Client;

    namespace ActivityLibrary6

    {

    public class CodeActivity1 : CodeActivity

    {

    [Input("To")]

    public InArgument<string> to { get; set; }

     

    [ReferenceTarget("contact")]

    [Output("ContactId")]

    public OutArgument<EntityReference> ContactId { get; set; }

     

     

    // If your activity returns a value, derive from CodeActivity<TResult>

    // and return the value from the Execute method.

    protected override void Execute(CodeActivityContext executionContext)

    {

    // Obtain the runtime value of the Text input argument

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

    //Create an Organization Service

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

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

    // Create query using querybyattribute

    QueryByAttribute querybyexpression = new QueryByAttribute("contact");

    querybyexpression.ColumnSet = new ColumnSet("contactid", "emailaddress1");

    // Attribute to query

    querybyexpression.Attributes.AddRange("emailaddress1");

    string email = this.to.Get(executionContext);

    // Value of queried attribute to return

    querybyexpression.Values.AddRange(email);

    // Query passed to the service proxy

    EntityCollection retrieved = service.RetrieveMultiple(querybyexpression);

    // Returned value

    // ************ Solution 1

    foreach (Entity Entity in retrieved.Entities)

    {

    this.ContactId.Set(executionContext, Entity.Attributes["contactid"]);

    break;

    }

    }

    }

    }

    Do you have any idea why result is empty ?

  • Community Member Profile Picture
    on at
    RE: Custom workflow activity - Output argument problem

    In fact, the problem of the output argument unavailable in the workflow form seems to be a Firefox problem.

    With IE, I have no issue.

    I've tried several codes and this one doesn't generate any issue, but my target field is empty as a result :

    using System.Activities;

    using Microsoft.Crm.Sdk.Messages;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Workflow;

    using Microsoft.Xrm.Sdk.Query;

    using Microsoft.Xrm.Sdk.Messages;

    using System;

    using Microsoft.Xrm.Sdk.Client;

    namespace ActivityLibrary6

    {

    public class CodeActivity1 : CodeActivity

    {

    [Input("To")]

    public InArgument<string> to { get; set; }

     

    [ReferenceTarget("contact")]

    [Output("ContactId")]

    public OutArgument<EntityReference> ContactId { get; set; }

     

     

    // If your activity returns a value, derive from CodeActivity<TResult>

    // and return the value from the Execute method.

    protected override void Execute(CodeActivityContext executionContext)

    {

    // Obtain the runtime value of the Text input argument

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

    //Create an Organization Service

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

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

    // Create query using querybyattribute

    QueryByAttribute querybyexpression = new QueryByAttribute("contact");

    querybyexpression.ColumnSet = new ColumnSet("contactid", "emailaddress1");

    // Attribute to query

    querybyexpression.Attributes.AddRange("emailaddress1");

    string email = this.to.Get(executionContext);

    // Value of queried attribute to return

    querybyexpression.Values.AddRange(email);

    // Query passed to the service proxy

    EntityCollection retrieved = service.RetrieveMultiple(querybyexpression);

    // Returned value

    // ************ Solution 1

    foreach (Entity Entity in retrieved.Entities)

    {

    this.ContactId.Set(executionContext, Entity.Attributes["contactid"]);

    break;

    }

    }

    }

    }

    Do you have any idea why result is empty ?

  • Community Member Profile Picture
    on at
    RE: Custom workflow activity - Output argument problem

    I gives me the following error with your code :

    System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

    Parameter name: index

      at Microsoft.Crm.Sandbox.SandboxCodeUnit.Execute(IExecutionContext context)

      at Microsoft.Crm.Workflow.Services.ProxyCustomActivity.Execute(CodeActivityContext executionContext)

    Same error with index[1]

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Custom workflow activity - Output argument problem

    Try Using

    executionContext.SetValue(ContactId, retrieved.Entities[0].GetAttributeValue<EntityReference>("contactid"));

    Instead of

    foreach (var c in retrieved.Entities)

    {

    this.ContactId.Set(executionContext, c.Attributes["contactid"]);

    break;

    }

  • Community Member Profile Picture
    on at
    RE: Custom workflow activity - Output argument problem

  • Community Member Profile Picture
    on at
    RE: Custom workflow activity - Output argument problem

    Custom-workflow-activity.png

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

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

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,865 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,723 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans