简体   繁体   中英

CRM2011 - “The given key was not present in the dictionary”

I am what you call a "n00b" in CRM plugin development. I am trying to write a plugin for Microsoft's Dynamics CRM 2011 that will create a new activity entity when you create a new contact. I want this activity entity to be associated with the contact entity.

This is my current code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;

namespace ITPH_CRM_Deactivate_Account_SSP_Disable
{
    public class SSPDisable_Plugin: IPlugin
    {

        public void Execute(IServiceProvider serviceProvider)
        {

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            if (context.InputParameters.Contains("Target") && context.InputParameters["target"] is Entity)
            {
                Entity entity = context.InputParameters["Target"] as Entity;

            if (entity.LogicalName != "account")
            {
                return;
            }

            Entity followup = new Entity();
            followup.LogicalName = "activitypointer";
            followup.Attributes = new AttributeCollection();
            followup.Attributes.Add("subject", "Created via Plugin.");
            followup.Attributes.Add("description", "This is generated by the magic of C# ...");
            followup.Attributes.Add("scheduledstart", DateTime.Now.AddDays(3));
            followup.Attributes.Add("actualend", DateTime.Now.AddDays(5));

            if (context.OutputParameters.Contains("id"))
            {
                Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString());
                string regardingobjectidType = "account";

                followup["regardingobjectid"] = new EntityReference(regardingobjectidType, regardingobjectid);

            }

            service.Create(followup);

        }


    }

}

But when i try to run this code: I get an error when i try to create a new contact in the CRM environment. The error is: "The given key was not present in the dictionary" (Link *1). The error pops up right as i try to save the new contact.

Link *1: http://puu.sh/4SXrW.png
(Translated bold text: "Error on business process")

Microsoft Dynamics CRM uses the term activity to describe several types of interactions. The types of activities are: Phone Call, Task, E-mail, Letter, Fax and Appointment.

ActivityPointer (Activity) Entity

To make your code working Replace the following line:

Entity followup = new Entity();

with

Entity followup = new Entity("task");

And remove the following line:

followup.LogicalName = "activitypointer";

Also please read my comment and Guido Preite's commend above. You need to modify your code to make it working with contact.

Edited

Make sure ContactId does exist in CRM before referencing it to Activity.

This can often happen if you are explicity adding an attribute value to the target entity in you plugin where it already has been added.

Rather than entity.Attributes.Add(...)

use entity["attributename"] = ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM