如何在CRM Online 2013中拥有唯一的ID号

时间:2014-07-17 09:19:44

标签: javascript dynamics-crm-2011 dynamics-crm crm dynamics-crm-online

我正在使用CRM Online 2013,在我的自定义实体中,有一个名为ID的唯一字段。此ID是手动输入的,需要是唯一的。有没有办法检查在其他记录之前是否输入了ID?例如,如果存在具有ID< 111,222,333的先前记录,并且如果用户试图保存ID = 111的新记录,则它应该发出警报。

3 个答案:

答案 0 :(得分:1)

但是,我不赞成用户输入的唯一号码。出于性能原因,它应该是自动生成的。此外,如果用户必须多次键入数字以找到唯一的号码,则可能会令用户感到沮丧。除非您有任何已定义的模式,否则用户无法猜测唯一编号。

但是你仍然可以通过在预验证或预操作上编写插件来实现你想要的。在插件中检查Id等于给定Id的现有记录。如果以前使用过,则抛出新的InvalidPluginExecutionException并显示错误消息。

如果您在数据库中有数百万条记录,那将是一个沉重的查询,我建议您在此列上使用SQL索引来改善处理时间。或者使用唯一的自动生成填充字段。

答案 1 :(得分:1)

我使用两种方法。在Id字段的onChange事件期间通过javascript执行Odata,并警告用户它是否已在使用中。这可能是所有需要的,但它留下了人A和人B之间同时进入相同身份的竞争条件的可能性。在这种情况下,您需要一个插件来确认它是唯一的。

Odata调用使用户更容易,因为他们立即反馈,插件只是帮助关闭竞争条件差距。

答案 2 :(得分:0)

以下是您需要用来检查值唯一性的基本代码的粗略且未经测试的示例。我建议在唯一字段和主键字段上放置一个索引,以提高性能,如果您期望创建高记录数或创建大量记录。

/// <summary>
    /// Determines if the uniqueField contains a unique value.
    /// </summary>
    /// <param name="entityName">Logical Name of the entity being checked</param>
    /// <param name="uniqueField">Field on the entity to check for uniqueness</param>
    /// <param name="recordIdField">Field on the entity that is hte Primary Key (not attribute) of the Entity - should be logical name of the entity followed by 'id' (i.e., accountid)</param>
    /// <param name="valueToCheck">Value to ensure is unique to this record</param>
    /// <param name="currentRecordId">Id (Primary Key) of the current record</param>
    /// <param name="service">An instance of IOrganizationService with Organization-wide permission to read the entity</param>
    /// <returns></returns>
    public bool isUniqueRecord(string entityName, string uniqueField, string recordIdField, string valueToCheck, Guid currentRecordId, IOrganizationService service)
    {
        var query = new QueryExpression(entityName)
        {
            ColumnSet = new ColumnSet(true)
        };

        var criteria = new FilterExpression(LogicalOperator.And);
        criteria.AddCondition(new ConditionExpression(uniqueField, ConditionOperator.Equal, valueToCheck));
        criteria.AddCondition(new ConditionExpression(recordIdField, ConditionOperator.NotEqual, currentRecordId));

        query.Criteria = criteria;

        var result = service.RetrieveMultiple(query);

        if (result.Entities.Any()) return false;
        else return true;            

    }