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
Ivan WinzerIvan Winzer 

Duplicate accounts are being created

So over the weekend im not sure what happend but for some reason i have a hand full of accounts that are duplicating on the second. I just did a mass delete of over 10k of the same account and just as soon as i refresh the report the name poped back up and i had another 119 of the same account but not tied to an contact. Here is a trigger an older developer wrote (before my time) and im wondering if something is triggering this to keep creating accounts even though they are not tied to any contacts. can anyone help this is casusing a huge problem as these duplicates are being pushed into other systems with our API calls and integrations.
 
trigger MainContactTrigger on Contact (before update, before insert, after update, after insert) {
    DuplicateEmailFilter def = new DuplicateEmailFilter();
    Map<id,Contact> oldMap = trigger.oldMap;
    CreateAccountFromContact accountCreation = New CreateAccountFromContact();
    
    if(oldMap == null){
        oldMap = new Map<Id,Contact>();
    }
    if(Trigger.isBefore){
    // 
    // For the before creation we check if the contact is getting created by a real user then we use 
    // the Duplicate Email Filter to generate an error preventing the contact from being created.
    //
        if(!IntegrationUtils.isIntegrationUser()){
            def.processContacts(trigger.new, oldMap);
        }
        if( Trigger.isInsert && !IntegrationUtils.ignoreForAutoAccountUser() ) {
            accountCreation.ProcessBatchContactsWEmail(trigger.new,trigger.oldMap);
        }
        else if( Trigger.isInsert || Trigger.isUpdate ) {
            accountCreation.ProcessBatchContacts(trigger.new,trigger.oldMap);
        }
    }
    else{
    //
    // This is the After trigger - we just need to check if the email address is already
    // in use in the duplicate email filter. 
    // if it is being created by the integration user then in the DEF we flag the contct as having 
    // a duplicate. Leaving it up to the Salesforce Admin to clean it up.
    //
        if(IntegrationUtils.isIntegrationUser()){
            def.processContacts(trigger.new, oldMap);
        }
    }
    
}

 
William TranWilliam Tran
it looks like you have an object - CreateAccountFromContact - that creates account.

if( Trigger.isInsert && !IntegrationUtils.ignoreForAutoAccountUser() ) {

18             accountCreation.ProcessBatchContactsWEmail(trigger.new,trigger.oldMap);

19         }

20         else if( Trigger.isInsert || Trigger.isUpdate ) {

21             accountCreation.ProcessBatchContacts(trigger.new,trigger.oldMap);

22         }

You need to determine you logic and proceed as needed (remove them? leave them but tweak it ?, etc.)

thx
Ivan WinzerIvan Winzer
So William here is the class that is being called in the trigger. What do you think
 
public class CreateAccountFromContact{ 

    private RecordType arec = 
        [SELECT ID 
            FROM RecordType 
            WHERE 
                SobjectType='Account'
                AND 
                Name='Household' limit 1];

    public Account CreateAccountFromContactWEmail( Contact c) {
    
        Account b = (Account) c.Account;
        Account a = new Account( name = c.firstName + ' ' + c.lastName, email__c = c.email);
        return a;
    }
    
    public Account CreateAccountFromContact( Contact c) {
    
        Account b = (Account) c.Account;
        Account a = new Account( name = c.firstName + ' ' + c.lastName, RecordTypeID = arec.Id);
        return a;
    }
    
    public void ProcessBatchContactsWEmail ( List <Contact> clist, Map<id,contact> oldValues) {
        
        List<Contact> contacts = new List<Contact>();
        List<Account> accounts = new List<Account>();
        for ( Contact c : clist ) { 
            if ( c.is_household__c || c.AccountId == null) {
                contacts.add(c);
                accounts.add(createAccountFromContactWEmail( c ));
            }
        }
    
        insert accounts;
        for ( integer i = 0; i<contacts.size(); i++){
            contacts[i].AccountId = accounts[i].id;
        }
    
    }

    
    public void ProcessBatchContacts ( List <Contact> clist, Map<id,contact> oldValues) {
        
        List<Contact> contacts = new List<Contact>();
        List<Account> accounts = new List<Account>();
        for ( Contact c : clist ) { 
            if ( c.AccountId == null ) {
                contacts.add(c);
                accounts.add(createAccountFromContactWEmail( c ));
            }
        }
    
        insert accounts;
        for ( integer i = 0; i<contacts.size(); i++){
            contacts[i].AccountId = accounts[i].id;
        }
    
    }
}