Simple custom activity plugin for MS Dynamics that retrieves and displays a value won't work

213 Views Asked by At

Ignore the name of this plugin because at this point I'm only trying to achieve something very simple from which then I could build something more complex. But even the simplest thing will not work. I'm still fairly new to plugins and custom activities so if anyone can pinpoint what I'm doing wrong, I'd be really grateful. First, here is the code of this custom activity, and at this point I'm only trying to retrieve the contactid, convert it to string and then be able to display it in some string field out there after I run a workflow on demand:

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Activities;

namespace WheatonPluginDelta.Activities
{
    public class RetrieveRaceOfPerson : CodeActivity
    {
        //Required input:
        [RequiredArgument]
        [Input(PersonReference)]
        [ReferenceTarget(contact)]
        public InArgument<EntityReference> PersonRef { get; set; }
 
        [Output(StringTest2)]
        public OutArgument<string> StringTest2 { get; set; }

        protected override void Execute(CodeActivityContext executionContext)
        {
            ITracingService tracingService = executionContext.GetExtension<ITracingService>();
            IWorkflowContext workflowContext = executionContext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService orgService = serviceFactory.CreateOrganizationService(workflowContext.UserId);

            EntityReference personRef = PersonRef.Get(executionContext);
            var contact = orgService.Retrieve(contact, personRef.Id, new ColumnSet(contactid));
            StringTest2.Set(executionContext, contact[contactid].ToString());
        }
    }
}

Here is the setup of this custom activity where it asks for the input for the workflow:

input

And then I set up the output to be StringTest2 from the custom activity to go to any other field that is of type string. Then I run the workflow on demand, and it doesn't work! Field does not get populated with the person's contactid.

Can someone please enlighten me on what I'm doing wrong on something so simple?

Thanks.

1

There are 1 best solutions below

1
marc_s On

I believe you're just missing a few double quotes - many things in Dynamics are string literals / constants - your post didn't show any of those - just because you really don't use any, or did you somehow leave these out when posting?

Have a look at this code:

public class RetrieveRaceOfPerson : CodeActivity
{
    //Required input:
    [RequiredArgument]
    [Input("PersonReference")]     // double quotes here - this is a string as name
    [ReferenceTarget("contact")]   // double quotes here, too - around the type of entity
    public InArgument<EntityReference> PersonRef { get; set; }

    [Output("StringTest2")]        // again double quotes 
    // I recommend using a *different* name than inside the [Output()] attribute
    public OutArgument<string> outStringTest2 { get; set; }  

    protected override void Execute(CodeActivityContext executionContext)
    {
        ITracingService tracingService = executionContext.GetExtension<ITracingService>();
        IWorkflowContext workflowContext = executionContext.GetExtension<IWorkflowContext>();
        IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
        IOrganizationService orgService = serviceFactory.CreateOrganizationService(workflowContext.UserId);

        EntityReference personRef = PersonRef.Get(executionContext);

        // double quotes around the entity type to retrieve ....
        var contact = orgService.Retrieve("contact", personRef.Id, new ColumnSet("contactid"));

        // double quotes around the attribute name
        outStringTest2.Set(executionContext, contact["contactid"].ToString());
    }
}