You need to sign in to do that
Don't have an account?
Matt Czugala
Trigger for Contact works on before Insert, but not before Update
I am trying to create a trigger so that an error is given when a contact has a duplicate email address AND is not a partticular selection in a pick-list. When the picklist value = User, Manager, or Exectuive. If the Picklist value = None, we don't care.
Currently I have it working so that a new record cannot be created (before Insert), however am having trouble getting it to work so that if someone updates a contact an error is given. Also when trying to correct a contact back to None, I receive the error, bascially not allowing me to fix the incorrect data.
Any suggestions?
Currently I have it working so that a new record cannot be created (before Insert), however am having trouble getting it to work so that if someone updates a contact an error is given. Also when trying to correct a contact back to None, I receive the error, bascially not allowing me to fix the incorrect data.
Any suggestions?
trigger CheckDuplicateEmail on Contact (before insert, before update) { for (Contact c : Trigger.new){ Contact[] contacts= [select id from Contact where Email = :c.Email AND elink_Contact_Type__c != 'None' ]; if (contacts.size() > 1 ) { c.Email.addError('A Duplicate contact be given a Login - use a different e-mail address or change their contact type'); } } }
I'm also duty bound to point out that your trigger isn't bulkified, as you have a SOQL query nested in the for loop, so if more than 100 records are loaded via the dataloader, for example, you will breach governor limits. You can learn more about bulkification at:
https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code
All Answers
I'm also duty bound to point out that your trigger isn't bulkified, as you have a SOQL query nested in the for loop, so if more than 100 records are loaded via the dataloader, for example, you will breach governor limits. You can learn more about bulkification at:
https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code
About the problem you are facing I think you may want to exclude the current record you are editing from the query ? something like
I think your if statement I have added has gotten me closer, but there's something still off.
-
If I try to create a duplicate contact with login, I receive the error.
-
If I try to update an existing contact to have a login, I don't receive the error. (it's like it doesn't really find the existing duplicates)
-
If I try to update an existing contact that has a duplicated login, I receive the error and have to change the picklist back to none to save.
once I have this resolved I will look into the bulkification.Point one sounds correct to me (assuming login doesn't mean something)
Points two and three seem conflcting - if you upate an existing contact you don't receive the error, and if you upate an existing contact you do receive an error?
"This post is the first time you've mentioned login - is that something significant?"
We define "login" by what value is in the picklist elink_Contact_Type__c and if they have an e-mail address. If that picklist is anything other than None, they are generated a login for our website. Our website generates the login to be the same as their e-mail address. The options for that picklist are (None, User, Manager, Executive). User/Manager/Executive options all generate a login, it's just they have different access levels. If our website creates a duplicate login because of a duplicated e-mail address & both contacts are not set to NONE, neither accounts are able to login anymore. I want to create this trigger so that our end users are not inadvertanly creating duplicate logins to our website.
Here is my testing process:
I have 4 contacts in my sandbox. Test None, Test User, Test Manager, Test Executive. All of them have the elink_Contact_Type__c field set to None. Each all have the same e-mail address (which is ok since all of them are set to "None").
Modify Test User, set elink_Contact_Type__c field to "User". Save. No Error (GOOD!)
Add new contact, set e-mail as duplicate. Set elink_Contact_Type__C field to "User". Save. Error. (GOOD!)
Modify Test Manager, set elink_Contact_Type__c field to "Manager". Save. (BAD!) No Error. I should receive an error since Test User has already set their field to User.
Apex script unhandled trigger exception by user/organization: 00530000000cqnd/00D300000000L0G
CheckDuplicateEmail: System.LimitException: Too many SOQL queries: 101