You need to sign in to do that
Don't have an account?

Hi all! Iwant to write a trigger to account. I creating an account with Email field to be unique for each account, If the Email is not unique to issue an error and do not secure data - that`s work
trigger AccountEmail on Account (before insert) { Map<String, Account> accMap = new Map <String, Account>(); Map<String, Account> exMap = new Map <String, Account>(); Map<String, Contact> conMap = new Map <String, Contact>(); for(Account account: Trigger.new){ accMap.put(account.Email__c, account); // } for(Account acc : [SELECT Name, Email__c, Secondary_Email__c FROM Account WHERE Email__c in: accMap.keySet()]) { exMap.put(acc.Email__c, acc); } for(Contact con : [SELECT Email, Secondary_Email__c FROM Contact WHERE Secondary_Email__c in : conMap.keySet()]){ conMap.put(con.Secondary_Email__c, con); } //eq for(Account acc : Trigger.new) { if(exMap.containsKey(acc.Email__c)) { acc.Email__c.addError('Existed'); } } }
But then I create a custom field on the Account - Secondary Email object and the same field on the Contact object, For this field I need make a check - If the entered Secondary Email is contained in the Contact object, then issue an error. In the trigger, consider processing not a single Account, Foresee that there can be several accounts (Process List <Account>) - doesn`t work, related list is empty. What I need to do? Thanks!