Question

I'm trying to set the "description" field on the Appointment entity in the Create event in a Post plugin.

This is CRM 2013 Online.

This is the error I'm getting

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: System.ArgumentException: This lookup can only display one item. Lookup ID = Detail:
-2147220970 System.ArgumentException: This lookup can only display one item. Lookup ID =
2014-04-25T11:35:30.0267579Z

This field: enter image description here

How the plugin is registered enter image description here

Code I'm using:

using System;
using System.ServiceModel;
using System.Web.Services.Protocols;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
using System.Linq;
using System.Linq.Expressions;

namespace Itrim.Plugin
{
    public class Appointment_SetDayName : IPlugin
    {
        private const string _EntityName = "appointment";

        private bool IsContextValid(IPluginExecutionContext context)
        {
            if (
                context.InputParameters.Contains(ParameterName.Target) &&
                context.InputParameters[ParameterName.Target] is Entity &&
                context.PrimaryEntityName == _EntityName)
            {
                return true;
            }
            return false;
        }

        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            if (!IsContextValid(context))
                return;

            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService organization = factory.CreateOrganizationService(context.UserId);

            Entity target = (Entity)context.InputParameters[ParameterName.Target];
            string descName = "description";

            SetValue(target, descName, "test: " + DateTime.Now.ToString());

            // if post, need to save the entity, if pre, then just set values.
            organization.Update(target);
        }

        private static void SetValue(Entity entity, string fieldName, object value)
        {
            if (entity.Attributes.Contains(fieldName) == false)
                entity.Attributes.Add(fieldName, value);

            entity.Attributes[fieldName] = value;
        }
    }
}
Was it helpful?

Solution

You don't need to call update method. Steps to solve your issue are:

  1. Rewrite code of plugin using following Execute method:

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

    if (!IsContextValid(context)) return;

    Entity target = (Entity)context.InputParameters[ParameterName.Target]; target["description"] = "test: " + DateTime.Now.ToString();

    1. Register your plugin on Pre-Execution or Pre-Operation eventing pipeline.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top