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
Jim McCabeJim McCabe 

contactDuplicatePreventer trigger...can you exclude a specific record type?


We use the following trigger to prevent adding contacts from being created when another contact exists based upon exact email.  We just created a second account record type that a very small profile group has access to.  There is a use case where they will need to create a contact that already exists on our original record type, so I don't want them to be prevented from adding the contact.  I have zero experience writting triggers, so my question is can we exclude this trigger from running where account record type = 01260000000JKZ8
Thank you for any suggestions you can provide.
contactDuplicatePreventer trigger
bob_buzzardbob_buzzard
This should be straightforward - I wouldn't use the id of the record type, as that can change between sandbox and production when you deploy it.  Instead pull the record type back based on its name.  In the code below, the name is assumed to be 'My Account RT' - change this with the name of your account record type:

before line 7:

// get the record type 
RecordType rt=[select id from RecordType where Name='My Account RT' and sobjectype='Account'];

// build a set (no dupes) of the account ids associated with the contacts
Set<id> accIds=new Set<accIds>();
for (Contact cont : trigger.new)
{
  accIds.add(cont.accountId);
}

// pull back all accounts with the record type that allows dupes
List<Account> accs=[select id from Account where RecordTypeId=:rt.id and id in :accIds];

// build a map of account that allows dupes by id
Map<Id, Account> accsById=new Map<Id, Account>();
accsById.putAll(accs);

Then wrap lines 9 - 17 in the following if statement - this will ignore any contacts that are created for accounts with the special record type.

if (null==(accsById.get(contact.AccountId))
{
}

Finally in line 23, change the query to exclude contacts from the special record type when retrieving duplicates:

for (Contact contact : [select Email from Contact where Email in :contactMap.keySet() and AccountId not in :accsById.keySet()]