0
votes

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")

2
I think the error is with activitypointer, you need to specify the exact activity entity type you want to create (task, appointment, ...)Guido Preite
Any suggestions on how i do that? I am a beginner with CRM plugin development. All the help i can get is much appreciated.Alexander Johansen
You are saying that you want to create activity when user creates a contact but in code you are looking for "account" entity.Scorpion
@Scorpion you spotted another error! @DJZorrow you need to use task instead of activitypointer for example, and change account to contact as @Scorpion wroteGuido Preite

2 Answers

2
votes

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.

0
votes

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"] = ...