【问题标题】:create and update plugin in crm 2013 c#在 crm 2013 c# 中创建和更新插件
【发布时间】:2014-10-20 06:21:23
【问题描述】:

我需要在 CRM 2013 中编写插件来做两件事:

  1. 如果 statecode = 3 并且字段 el_meeting_in_outlook_id 为空 I 需要创建一个新的预约。
  2. 如果 statecode = 3 并且字段 el_meeting_in_outlook_id 不为空 我需要更新现有的预约。

这是我写的:

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

namespace tmura_Entity__Plugins
{
    public class postCreateUpdateServiceAppointment : Plugin
    {
        public postCreateUpdateServiceAppointment()
            : base(typeof(postCreateUpdateServiceAppointment))
        {
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Create", null, new Action<LocalPluginContext>(ExecutePostCreate)));
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", null, new Action<LocalPluginContext>(ExecutePostUpdate)));

        }

        private void ExecutePostCreate(LocalPluginContext obj)
        {
            Logger.WriteMessage("Enter ExecutePostCreate", CrmLogService.MessageLevel.Info, "");
            if ((obj.PluginExecutionContext.InputParameters.Contains("Target")) && (obj.PluginExecutionContext.InputParameters["Target"] is Entity))
            {
                Entity serviceAppontment = (Entity)obj.PluginExecutionContext.InputParameters["Target"];
                if (serviceAppontment.LogicalName != "serviceappointment")
                    return;

                Logger.WriteMessage("", CrmLogService.MessageLevel.Info, "");

                if ((serviceAppontment.Attributes.Contains("statecode")) || ((int)(serviceAppontment.Attributes["statecode"]) == 3) && (serviceAppontment.Attributes["el_meeting_in_outlook_id"] == null))
                {
                    try
                    {
                        Entity appointment = new Entity("appointment");
                        appointment.Attributes.Add("subject", "Opened automatically");
                        appointment.Attributes.Add("description", "Just Checking");
                        appointment.Attributes.Add("el_serviceappointment_id", new EntityReference("serviceappointment", serviceAppontment.Id));
                        appointment.Attributes.Add("scheduledstart", DateTime.Now.AddDays(7));
                        appointment.Attributes.Add("scheduledend", DateTime.Now.AddDays(7));

                        obj.OrganizationService.Create(appointment);
                    }                      
                    catch (Exception ex)
                    {
                        Logger.WriteException(ex);
                    }
                }
                else if (((int)(serviceAppontment.Attributes["statecode"]) == 3) && (serviceAppontment.Attributes["el_meeting_in_outlook_id"] != null))
                {
                    //TODO
                }
            }
        }
    }
}

我不知道在应该更新约会的第二部分中写什么。我试图搜索网络但没有成功。

你能帮忙吗?

【问题讨论】:

  • 请问您为什么在POST操作中注册了插件?它将在同一实体记录上导致两次更新。第一次在实际过程中,第二次通过您的插件触发更新。如果您在 PRE Operation 上注册您的插件,这将更适合给定场景。另一方面,PRE 插件只会更新一次。
  • 嗯,这是需求任务。我会与我的经理核实。谢谢!

标签: plugins dynamics-crm-2011 crm


【解决方案1】:

在你定义了注册事件的地方,你必须把“serviceappointment”,即实体的逻辑名称,而不是null。

然后将:(serviceAppontment.Attributes.Contains("statecode")) || ((int)(serviceAppontment.Attributes["statecode"]) == 3) 替换为(serviceAppontment.Attributes.Contains("statecode")) &amp;&amp; ((OptionSetValue)(serviceAppontment.Attributes["statecode"]).Value == 3),因为“statecode”字段是一个 OptionSet。还要替换 ||通过&&,因为当serviceAppontment.Attributes.Contains("statecode")) 为false 时,((OptionSetValue)(serviceAppontment.Attributes["statecode"]).Value 会抛出NullReferenceException。

要更新现有约会,看起来在约会实体中查找到 serviceappointment 实体。因此,您必须检索与 serviceappointment 相关的所有约会。你可以使用这个查询:

QueryExpression queryExp = new QueryExpression
{
    EntityName = "appointment",
    ColumnSet = new ColumnSet("subject", "description", "scheduledstart", "scheduledend"),
    Criteria = new FilterExpression
    {
       Conditions = { new ConditionExpression("el_serviceappointment_id", ConditionOperator.Equal, serviceAppontment.Id) }
    }

};

EntityCollection collection = obj.OrganizationService.RetrieveMultiple(queryExp);

if (collection.Entities.Count > 0)
{
    // now that you have all the appointments related with the serviceappoitement 
    // you can update de any appointment you want
    obj.OrganizationService.Update(appointment);
}

【讨论】:

  • 非常感谢。现在我需要更新我的记录。我需要插入一个值来从 serviceappoitnment 中另一个查找字段的字段中查找字段 requiredattendees。你能帮我怎么做吗?
【解决方案2】:

使用 serviceappoitnment 的查找字段中的值更新您的记录,以检索 serviceappoitment 记录 Entity svcApp = obj.OrganizationService.Retrieve(serviceAppontment.LogicalName, serviceAppontment.Id, new ColumnSet("&lt;lookup_field_in_serviceappoitnment&gt;");

你需要在查询表达式的collumnset中添加requiredattendees属性,然后更新appoitment

if(appointment.Attributes.Contains("requiredattendees") == true)
{
    appointment.Attributes["requiredattendees"] = svcApp.Attributes["<lookup_field_in_serviceappoitnment>"];
}
else
{
    appointment.Attributes.Add("requiredattendees", svcApp.Attributes["<lookup_field_in_serviceappoitnment>");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多