function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Muhammad.BilalMuhammad.Bilal 

Avoid duplicate contacts on a composite field via trigger

Hi,
We want to create a composite unique value comprises two fields
1. Email
2. data_access(true/false)
The system should allow entry if one of the value in these 2 fields is unique but we cannot allow both values together to have a duplicate record.

The trigger below works absolutely fine when inserting/updating new record but when I try to merge 2 records, the system generates an error and won't allow merging unless I choose the data_access value to be false always.


trigger DuplicateEmailsInContacts on Contact (before insert, before update) {
   Integer EmailCount = 0;
    FOR(Contact c: Trigger.New){
        IF(c.Email !='' && c.Email != NULL && c.Data__c==True){
            EmailCount = [SELECT count() FROM Contact WHERE EMAIL =:c.Email AND Data_Access__c=:True];
            IF(EmailCount > 0){
                c.Email.addError('A user with the same email already exits for data access, please either use a differnet email or uncheck Data Access');
            }
        }
    }
}

Any help is highly appreciated

Thanks,

Muhammad
GauravGargGauravGarg
Hi Muhammad,

Create  a new checkbox for "merge" and add condition to byepass error when Merge checkbox is checked. 

Thanks,
Gaurav
Skype: gaurav62990
Muhammad.BilalMuhammad.Bilal
Thanks Gaurav, but unfortunately we cannot do this because with the checkbox, the users will be able to create duplicate which will break our integrated system completely. Is it possible to check within the trigger if the user is merging the record and ignore the condition?
GauravGargGauravGarg
Yes, if we can select particular user or profile. Then we can check for the User / Profile in trigger and bypass condition. 

Thanks,
Gaurav
Muhammad.BilalMuhammad.Bilal
Sorry can you please elaborate a little more or can make changes to my trigger so it won't run if the user is merging the contacts 
GauravGargGauravGarg
Hi Muhammad,

You are integrating data with some particular User / profile or it can be updated on behalf of any one. 

If yes, you are using some user / profile, then check for bypass condition in code. 

Thanks,
Gaurav
Muhammad.BilalMuhammad.Bilal
No, this should be organization-wide so we don't want any user to override it. I am surprised that no one else has this issue because theoretically, this shouldn't happen in the first place because merging the records isn't violating the rule of a unique value but for some strange reason, it's not letting us merge the records.
GauravGargGauravGarg
Hi Muhammad,

In our project, we have a separate user for this task. And we have byepass the error condition for the user when he tries to merge records. 
Since, in your project do not have such User, we need to go by some customize way. 
  • Checkbox value. 
  • User value check. 
  • Or bypass Trigger when merge process is going on. 
Hope this helps!!!

Thanks,
Gaurav