0
votes
Entity title = new Entity();
title = service.Retrieve("incident",((Guid)((Entity)context.InputParameters["Target"]).Id), new ColumnSet("title"));

I am using this code to get the current id of an Incident while i'am closing it! But received this error :

Unexpected exception from plug-in (Execute): FecharIncidente.Plugins.PostIncidenteClose: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

One of my mates uses exactly the same code and is working on his crm ! Some help !?!

2

2 Answers

0
votes

Apparently your InputParameters collection doesn't have a "Target" key value. Check that the request that you're using has a "Target" InputParameter.

if (context.InputParameters.Contains("Target") &&
    context.InputParameters["Target"] is Entity)
{
    // Obtain the target entity from the input parameters.
    title = service.Retrieve("incident", ((Entity)context.InputParameters["Target"]).Id, new ColumnSet("title"));
0
votes

Bet you "Target" in not Contained in InputParameters, resulting in KeyNotFoundException - "The given key was not present in the dictionary."

your can check for Target like Daryl explained or use the context available from the workflow rather like so ...

protected override void Execute(CodeActivityContext executionContext)
    {
        // Create the context
        var context = executionContext.GetExtension<IWorkflowContext>();
        var title = new Entity();
        //context.PrimaryEntityName - should hold string incident
        //context.PrimaryEntityId - should hold your guid
        title = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet("title"));
    }