You need to sign in to do that
Don't have an account?
andresperez
One of the things that concern me is that triggers are not always fired.
How do I prevent duplicates based on more than one field?
Hi,
I need to have an sObject where the name field is unique for each user. A user can not have two or more records with the same "name", but it is possible for different users to have records with the same name. Let me explain....
Let's suppose I have an sObject with this data:
Owner Name
user1 name1
user1 name2
If "user1" tries to add a row named "name1", the system should prevent him from doing so because "user1" already has a row with "name1". On the other hand if "user2" tries to add a row named "name1" the system should allow this, because "user2" does not have a row named "name1".
So far, I have come to this solution... But I would like to know if there is a better any of doing it.
Code:
trigger PreventDuplicateNameForUser on TPName__c (before insert, before update) { for (TPName__c TPName : Trigger.new) { List<TPName__c> listFound = [SELECT name
FROM TPName__c
WHERE ownerid = :UserInfo.getUserID()
AND name = :TPName.name]; if (listFound.size() > 0) {
Trigger.new[0].addError('You already defined a Parameter Name called ['
+ TPName.name +']. Please use another name and try again');
}
} }
One of the things that concern me is that triggers are not always fired.
Is there a better way of doing this... I mean is there something to configure in a sObject to make it behave like this?
Thanks
I was reading the document "APEX Language Reference", and I found this: