You need to sign in to do that
Don't have an account?
Muhammad.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
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
Create a new checkbox for "merge" and add condition to byepass error when Merge checkbox is checked.
Thanks,
Gaurav
Skype: gaurav62990
Thanks,
Gaurav
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
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