Reputation: 597
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?
Upvotes: 4
Views: 2822
Reputation: 2087
Another option is to create unique SQL index directly in database.
Upvotes: -1
Reputation: 10344
You have to create a plugin for this requirement which handles the Pre-Create/Pre-Update step for this entity. In this plugin you have to check whether the passed value is unique or not. If it's not, you throw an exception which cancels the operation and displays a dialog to the user (if the plugin runs synchronously).
throw new InvalidPluginExecutionException("Value passed for 'attribute' is not unique.");
Upvotes: 8
Reputation: 966
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.
Upvotes: 1