如何在Create(预验证)插件中检测CRM 2013中的重复记录

时间:2015-02-20 11:43:27

标签: plugins dynamics-crm-2011 dynamics-crm crm dynamics-crm-2013

我正在编写一个插件,它将检测Create上的重复ID,并限制用户输入新的ID。注意:我不能使用2013年或2015年MICROSOFT DYNAMICS提供的默认重复方法。 这是一个特殊情况。 以下是我的插件的代码:

enter code here

if (entity.LogicalName == "new_studentinformation")
                {
                    // An accountnumber attribute should not already exist because
                    // it is system generated.
                    if (entity.Attributes.Contains("new_studentid") == false)
                    {
                        // Create a new accountnumber attribute, set its value, and add
                        // the attribute to the entity's attribute collection.
                        Random rndgen = new Random();
                        entity.Attributes.Add("new_studentid", rndgen.Next().ToString());
                    }

现在我面临的问题是在这一行

if(entity.Attributes.Contains(“new_studentid”)==“Something”)

我如何获取用户在crm中输入的值并将其与我现有的记录进行比较?

1 个答案:

答案 0 :(得分:0)

您需要按照documentation

中的说明从插件上下文中检索实体
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
    context.InputParameters["Target"] is Entity)
{
    // Obtain the target entity from the input parameters.
    Entity entity = (Entity)context.InputParameters["Target"];
    // Your code here...
    var desiredValue = entity.GetAttributeValue<desiredtype>("desiredfield");
}
相关问题