Preventing Duplicate Data being created by Update or Insert

cbegl
Kilo Explorer

Hi Guys

Im currently working on a config issue with duplicate ci's, to prevent this issue we are trying to create a business rule so that before updates or inserts we check the name hasnt been updated (current.name.changes()), and then that the same CI name doesnt exist within the same class with something like in the attachment.

If the CI does exist or renaming would create a ci with a identical name then we would abort the individual update, current.setAbortAction(true).

But implementation is a problem, has anyone had a similar Config issue and managed to resolve it?

Any help would be hugely appreciated.

3 REPLIES 3

Jim Coyne
Kilo Patron

What you could do is create a new field called "Name2" (u_name2) on the appropriate table(s) and select the Unique check box to enforce uniqueness on that field. Then, create a Business Rule that copies the Name field to the new field and the system will automatically enforce the uniqueness:

Name: Sync names
Table: Server [cmdb_ci_server]
Order: 10000000
When: before
Insert: Checked
Update: Checked
Script:

(function(){
  current.u_name2 = current.name;
})();

 

Remove the field from the form as you will not need to see or populate it. It does not display the most intuitive message:
java.sql.BatchUpdateException: Duplicate entry 'test' for key 'cmdb_ci_server_U1'
Invalid update

...but you do not have to do any scripting to look for dups, the database takes care of it for you. And it enforces uniqueness on a class per class basis, well, depending on where you create the field and Business Rule. For instance, adding the field and BR on the Server table enforces uniqueness on it and all it's sub-classes (Windows Server, Linux Server, etc...).

Take a look at demo007 and try to create 2 servers with the same name.

 

Jim Coyne
Kilo Patron

I'm just curious if the idea worked for you or not? You've marked the question as answered, but I see you have essentially the same question posted here, Business Rules for Duplicate CI's, and it is marked as answered as well, but with a different idea to solve your issue.


Jim,



The unique dictionary checkbox worked out well on Istanbul.       I was not able to check the box until I deduplicated the table.     Note:   by default,   the checkbox is not visible on the form.



Mike