I want to add a field "Single Line of Text" to my custom entity which should be unique. How can I make Crm to throw me an exception when I try to create the duplicating value of the record?
3 回答
您必须为此要求创建一个插件,以处理该实体的预创建/预更新步骤。在此插件中,您必须检查传递的值是否唯一。如果不是,则抛出一个异常,该异常会取消操作并向用户显示一个对话框(如果插件同步运行)。
throw new InvalidPluginExecutionException("Value passed for 'attribute' is not unique.");
You can't do this through configuration, as far as I know.
You would need to use some client code to query existing values for that field to see if the newly entered value is unique. You could use something like jQuery and the CRM REST service (ODATA) to perform an asynchronous validation on the field; it might not be instant though, you may need to consider how this is presented to the user.
Alternatively you could do this with workflow, but that'd be even less instant - it'd probably have to be triggered after the save.
另一种选择是直接在数据库中创建唯一的 SQL 索引。