title: Trigger to prevent duplicate record names in salesforce
body: <p>I need to prevent duplicate names from been entered. This restriction needs to be imposed using a trigger. I want to know how i can restrict the DML operations from happening. Not sure of the usage of .addError in bulkified code.</p>
  
  <pre><code> Set&lt;string&gt; Seta= new Set&lt;string&gt;();
   for(oj__c o:trigger.new)
   {
     Seta.add(c.name);
   }
  
   List&lt;oj__c&gt; listoj= new List&lt;oj__c&gt;();
   listoj=[select id from oj__c where name in :Seta]
   if listoj.size()&gt;0
   trigger.new.adderror('Cannot have duplicate name');// i know this line is wrong. How can i stop the DML statement from excuting?
  </code></pre>