I am trying to prevent duplicate users while creating the record in custom object which is having look-up relationship. The code is executing fine in before insert, But when updating same record with different field values it is showing the error, I want to show the error when the user modifies non-existing user to existing user only.
Can i achive with the following trigger or can i go for another concept?
trigger PreventDuplicateUser on object__c(before insert,before update) {
for(object__c o:trigger.new)
{
list<object__c> obj=[select id,user__c from object__c where user__c=:o.user__c];
if(obj.size()>0)
{
r.addError('Duplicate users not allowed');
}
}
}