Unique field combination

Manoj Meena asked on September 19, 2019 14:44

we have created a custom module and a class under it which has 10 fields, we need to validate if value of 3 of the fields are unique while inserting. if its a duplicate then it should throw a message that data already exist.

we have created user interface page in store configuration for admin to insert data and we are trying to validate that admin doesn''t insert same data multiple times by validating on 3 fields out of 10.

example: if address, city and state are the 3 fields and if admin is trying to do insert where record for these fields is already there with same combination then it should not allow.

Recent Answers


Peter Mogilnitski answered on September 19, 2019 16:27 (last edited on September 19, 2019 16:32)

I'd say do it at the SQL server level. Although it technically it is possible to do it in Kentico, (it is not straight forward) I find it easier do it in SQL.

ALTER TABLE CustomModuleTable
ADD CONSTRAINT UC_address_city_state UNIQUE (address,city,state);

If you want to do in Kentico way - here is the article how to make check for unique fields

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on September 19, 2019 16:43

While doing it in the database is an easier option it is also very difficult to see/find this in your business logic. I'd highly recommend creating a global event handler to perform this validation check. In the event handler you can perform your check and return errors and write event logs if needed.

1 votesVote for this answer Mark as a Correct answer

Eric Dugre answered on September 20, 2019 22:25

This can be accomplished using a General condition in the Validation rules of the fields you want to validate. Custom module objects are stored in the GlobalObjects macro collection, so you can compare the field value to the objects in this list.

For example, if the module is called Games with a class called Game, you can ensure that no Game has the same GameName with this rule:

!GameName.Value.EqualsAny(GlobalObjects.Games.ItemsAsFields.GameName)

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.