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

Trigger - Help
Hi,
I have written a Trigger where in i am Preventing duplicate Contact on a Child Account when Same contact appears on the Parent account.
trigger Duplicate on Contact (before insert , before update) {
set<Id> accIds = new set<Id>();
set<Id> parentIds = new set<Id>();
for(Contact p :trigger.new){
accIds.add(p.accountId);
}
map<Id,account> parentAccmap = new map<Id,account>([select Id,parentId,recordtypeId from account where Id IN :accIds and parentId!=NULL and recordtypeId='012600000005J5J']); //add Site record type Id here.
for(account a:parentAccmap.values()){
parentIds.add(a.parentId);
}
map<Id,Contact> parentPrMap = new map<Id,Contact>();
list<Contact> prParentlist = [select ID,Email,AccountId, Account.recordtypeId from Contact where AccountId IN:parentIds and Account.recordtypeId='012600000005J5I']; //add Customer record type Id here.
for(Contact pr:prParentlist){
parentPrMap.put(pr.AccountId, pr);
}
for(Contact prr:trigger.new){
if(prr.email!=null && parentAccmap.containsKey(prr.Accountid) && parentPrMap.containsKey(parentAccmap.get(prr.Accountid).ParentId)){
if(prr.email == parentPrMap.get(parentAccmap.get(prr.Accountid).ParentId).email){
String baseUrl = URL.getSalesforceBaseUrl().toExternalForm()+'/02Z/e?parentId=';
string errorMsg = '<a style=\'color:1B2BE8\'href="'+baseUrl+ prr.accountid +'"> Please Create a Contact Role </a>';
prr.addError('This Contact already exists.'+ errorMsg,false);
}
}
}
}
Can some one help me with logic or modification to the above trigger so that if that contact is present on any of the Child Accounts of the Parent account, error should be thrown.
I have written a Trigger where in i am Preventing duplicate Contact on a Child Account when Same contact appears on the Parent account.
trigger Duplicate on Contact (before insert , before update) {
set<Id> accIds = new set<Id>();
set<Id> parentIds = new set<Id>();
for(Contact p :trigger.new){
accIds.add(p.accountId);
}
map<Id,account> parentAccmap = new map<Id,account>([select Id,parentId,recordtypeId from account where Id IN :accIds and parentId!=NULL and recordtypeId='012600000005J5J']); //add Site record type Id here.
for(account a:parentAccmap.values()){
parentIds.add(a.parentId);
}
map<Id,Contact> parentPrMap = new map<Id,Contact>();
list<Contact> prParentlist = [select ID,Email,AccountId, Account.recordtypeId from Contact where AccountId IN:parentIds and Account.recordtypeId='012600000005J5I']; //add Customer record type Id here.
for(Contact pr:prParentlist){
parentPrMap.put(pr.AccountId, pr);
}
for(Contact prr:trigger.new){
if(prr.email!=null && parentAccmap.containsKey(prr.Accountid) && parentPrMap.containsKey(parentAccmap.get(prr.Accountid).ParentId)){
if(prr.email == parentPrMap.get(parentAccmap.get(prr.Accountid).ParentId).email){
String baseUrl = URL.getSalesforceBaseUrl().toExternalForm()+'/02Z/e?parentId=';
string errorMsg = '<a style=\'color:1B2BE8\'href="'+baseUrl+ prr.accountid +'"> Please Create a Contact Role </a>';
prr.addError('This Contact already exists.'+ errorMsg,false);
}
}
}
}
Can some one help me with logic or modification to the above trigger so that if that contact is present on any of the Child Accounts of the Parent account, error should be thrown.
I assume that
012600000005J5J = Customer and
012600000005J5I = Site
In the 3rd request, I search in the Sites, but the other condition is on parentId (so the Customer) : try to change the record type id in the 3rd request
from : to :
Hope this helps
Gil
Question Solved ? Please mark as the best answer to help other users
All Answers
By using this, you get only the last contact on a parent account : the accountid key is the same for all contacts on the same account.
Your test of uniqueness is done only on email ? if so, try use a Map<AccountId, set<email>> and test if a contact email is in the email set of the current account key.
Hope this helps
Gil
Question Solved ? Please mark as the best answer to help other users !
thank you for the reply.
But did not get you completely as to where do i need to make change.
Sorry to bother...m naive in triggers
I did not test it in a real case but, you can try this one : Let me know if it do the job, or if you have any error.
Hope this helps
Gil
Question Solved ? Please mark as the best answer to help other users !
The trigger that you gave is not working at all. It's not at all preventing duplicates.
Hope this helps
Gil
Question Solved ? Please mark as the best answer to help other users !
Working but only for Parent. i.e. it is checking duplicates based on Parent Record only , as my initial trigger did.
If a dupliate is present another child account of the same parent account, in that case this trigger does not prevent duplicates
change the line : to : so the query will search in parent, or in all accounts witch have the same parentId = brothers
That would do the trick.
Hope this helps
Gil
Question Solved ? Please mark as the best answer to help other users !
Hope this helps
Gil
Question Solved ? Please mark as the best answer to help other users !
no success yet gill
- parent account : email_parent@email.com
- child account : email_child@email.com
- on child 2 (child of parent account) I try to create email_child@email.com -> error
did you try with the correct record type ? (the request takes it into account)
Hope this helps
Gil
Question Solved ? Please mark as the best answer to help other users !
yes i am doing the same thing but in case of child records it is allowing duplicates
child account : email_child@email.com
child 2 ( chil of parent account) email: email_child@email.com --- no error , but it should be throwing error
Are we doing the same thing ?
When I try to insert the contact on child 2 account, I get an error.
And if I try to insert a contact on child 2 with the email : Email_parent@email.com, I also get an error.
did you try with the correct record type ids ?
are you sure that all the accounts are of the correct recordtype id ?
Hope this helps
Gil
Question Solved ? Please mark as the best answer to help other users
yup i am trying the same thing.
If a contact is on parent and if i try to duplicate it on a child account, the trigger works and prevents the duplicate.
But if a contact exists on the child and i try to duplicate this contact on another child of the same parent, then the trigger does ot work and does not throw the error. it should be throwing error even then.
i checked the record type ids, they are correct. The Customer is the Parent Account and the Site is the child account
I assume that
012600000005J5J = Customer and
012600000005J5I = Site
In the 3rd request, I search in the Sites, but the other condition is on parentId (so the Customer) : try to change the record type id in the 3rd request
from : to :
Hope this helps
Gil
Question Solved ? Please mark as the best answer to help other users
Did you solved your problem ?
It did.
Just figuring out a case where the duplicates will not only be checked on Email but Phone, Name as well