You need to sign in to do that
Don't have an account?
Sainath Venkat
trigger on specific record type for contact duplication
Can anyone help me to fire the below trigger on a specific record type only.
I have record type of "Business Organization (Mastered)" and below trigger should fire only on the mentioned record type.
trigger PreventDuplicateContact on Contact (before insert,before update) {
if(trigger.isInsert || trigger.isUpdate)
{
Map<Id,List<Contact>> accMap = new Map<Id,List<Contact>>();
List<Contact> contactList;
for(Contact c: [Select Id,Email,accountId from Contact]){
if(accMap.containsKey(c.accountId)){
accMap.get(c.accountId).add(c);
}
else{
contactList = new List<Contact>();
contactList.add(c);
accMap.put(c.accountId,contactList);
}
}
for (Contact c : Trigger.new)
{
if(accMap.containsKey(c.accountId)){
for(Contact con : accMap.get(c.accountId)){
if(c.Email == con.Email){
c.Email.addError('Contact with this email address already exists');
}
}
}
}
}
}
I have record type of "Business Organization (Mastered)" and below trigger should fire only on the mentioned record type.
trigger PreventDuplicateContact on Contact (before insert,before update) {
if(trigger.isInsert || trigger.isUpdate)
{
Map<Id,List<Contact>> accMap = new Map<Id,List<Contact>>();
List<Contact> contactList;
for(Contact c: [Select Id,Email,accountId from Contact]){
if(accMap.containsKey(c.accountId)){
accMap.get(c.accountId).add(c);
}
else{
contactList = new List<Contact>();
contactList.add(c);
accMap.put(c.accountId,contactList);
}
}
for (Contact c : Trigger.new)
{
if(accMap.containsKey(c.accountId)){
for(Contact con : accMap.get(c.accountId)){
if(c.Email == con.Email){
c.Email.addError('Contact with this email address already exists');
}
}
}
}
}
}
All Answers
OR
Actually, the record type that I have mentioned is present on ACCOUNT object but not on CONTACT object
Your trigger worked for me, I have written a test class previously, can you please help me with the modifications for the below test class
@isTest
private class PreventDuplicateContactTest {
@isTest static void TestTrigger(){
Account testAcct1 = new Account(Name='Test Account');
insert testAcct1;
Account testAcct2 = new Account(Name='Test Account');
insert testAcct2;
Contact cont = new Contact(FirstName = 'Test', LastName = 'LastName', Email='test@tst.com', accountid = testAcct2.id);
insert cont;
Contact cont2 = new Contact(FirstName = 'Test2', LastName = 'LastName', Email='testt@tst.com', accountid = testAcct1.id);
insert cont2;
List<Contact> contacts = new List<Contact>();
for (Integer i = 1; i < 4; i++){
contacts.add(new Contact(FirstName = 'Test'+i, LastName = 'LastName'+i, Email='test@tst.com', accountid = testAcct2.id));
}
try {
insert contacts;
} catch (Exception e) {
System.assert(e.getMessage().contains('Contact with this email address already exists'));
}
}
}
I am considering record type of "Business Organization (Mastered)" on Contact object.
Try Below code:
I have used to get contact record type info:
Map<ID, Schema.RecordTypeInfo> rtMap = Schema.SObjectType.Contact.getRecordTypeInfosById();
To validate record type name used below code:
String type = rtMap.get(c.RecordTypeId).getName();
if(type.equals('Business_Organization_Mastered')){
Mark it as a best answer if it helps