0
votes

I am trying to write a custom workflow for Microsoft Dynamics CRM 2011 as a training exercise. The code I have used is below, which works fine for a standard plugin but, when running as part of a custom workflow gives me a key not present in dictionary error. Can anyone spot any reason for this? I've checked the entity and field names which are all correct.

Thanks

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Collections.ObjectModel;

using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;

using System.Diagnostics;


namespace TestWflow
{

    public class SampleCustomActivity : CodeActivity
    {


        protected override void Execute(CodeActivityContext executionContext)
        {
            //Activity code

            // Get the context service.
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();

            // Use the context service to create an instance of IOrganizationService.
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);

            if (context.Depth == 1)
            {

                Entity targetCont = null;
                targetCont = (Entity)context.InputParameters["Target"];
                Guid contID = targetCont.Id;
                ColumnSet contCols = new ColumnSet("jobtitle");

                targetCont = service.Retrieve("contact", contID, contCols);

                targetCont.Attributes["jobtitle"] = "test jobtitle here";
                service.Update(targetCont);


            }



        }



    }


}
1
How is the workflow firing?Bvrce
I am triggering it manually at the minute.user2463758
Try new ColumnSet(true)Bvrce
@Bvrce - Thank you, but it's still doing the same if I try thatuser2463758
Please post the exception.Bvrce

1 Answers

1
votes

My guess is that your code is blowing up on: targetCont = (Entity)context.InputParameters["Target"];

My plugin code has always been a little different from my workflow code in how it gets data.

So try to add to your code:

context.PrimaryEntityId

In the end your code should look like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Collections.ObjectModel;

using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;

using System.Diagnostics;


namespace TestWflow
{

    public class SampleCustomActivity : CodeActivity
    {
        protected override void Execute(CodeActivityContext executionContext)
        {
            //Activity code

            // Get the context service.
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>   ();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();

            // Use the context service to create an instance of IOrganizationService.
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);

            if (context.Depth == 1)
            {
                Guid contID = context.PrimaryEntityId;
                ColumnSet contCols = new ColumnSet("jobtitle");

                var entity = service.Retrieve("contact", contID, contCols);

                entity.Attributes["jobtitle"] = "test jobtitle here";
                service.Update(entity);


            }



        }



    }


}