Question: **Screenshot**

[![enter image description here](https://i.stack.imgur.com/NT0Ny.jpg)](https://i.stack.imgur.com/NT0Ny.jpg)

**Trigger:**

```
trigger Triggertovalidate on GEN_Parameter__c (BEFORE INSERT,BEFORE UPDATE)
{
    LIST<GEN_Parameter__c> li = [select id,Country__c from GEN_Parameter__c where id != null]; 
    for(GEN_Parameter__c c :trigger.new)
    {
     if(c.Country__c != null){
      for(GEN_Parameter__c existrecord :li)
      {
          if(existrecord.Country__c == c.Country__c ){
          c.Country__c.addError('Country is already used please select another one.');
          }
      }
     }
    }

}

```

**Problem:**
I am checking unique value for Country Pick list, while inserting and updating the record.

This is working fine while inserting a record.
When i am updating the record , I am unable to update the record for the same country. Its showing Trigger error country already exist.

My Goal is not to create duplicate record for country while insert or update.But I am not able to update my original(unique) record itself.

Please help to solve this issue. Thanks in advance.

Answer: 
@Salesforcesmarty 's answer is almost correct. You simply need to change the condition line into: 

```
if(trigger.isInsert || (trigger.isUpdate && existrecord.Id != c.Id))

```

And this will work for you.
Your code will throw error, even though you are not updating any country field on update.

Update your code with this:

```
trigger Triggertovalidate on GEN_Parameter__c (BEFORE INSERT,BEFORE UPDATE)
{
    Set<String> countries = new Set<String>();
    for(GEN_Parameter__c country : [select id,Country__c from GEN_Parameter__c where id != null]);
        countries.add(country.country__c);

    for(GEN_Parameter__c c :trigger.new){
        if(c.Country__c != null){
            if(trigger.isInsert || (trigger.isUpdate && trigger.OldMap.get(c.id).Country__c != c.Country__c)){
                if(countries.contains(c.Country__c)){
                    c.Country__c.addError('Country is already used please select another one.');
                }
            }
        }
    }
}

```