• Santosh Patro 2
  • NEWBIE
  • 5 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi,
I have  written one trigger and two classes which is working fine in sandbox but in Production I am getting the below error:

Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Account: execution of AfterInsert caused by: System.DmlException: Upsert failed. First exception on row 0; first error: DUPLICATES_DETECTED, You're creating a duplicate record. We recommend you use an existing record instead.: [] Class.AccountServices.associateContacts: line 36, column 1 Class.AccountTriggerHandler.afterInsert: line 12, column 1 Trigger.Account: line 6, column 1: []
Error is in expression '{!createopportunity}' in component <apex:page> in page lead_convert: Class.Leadconvert.createopportunity: line 31, column 1

 
<Account Services>

public with sharing class AccountServices {
    public static List<Account> hasEmailChanged
        (List<Account> newRecords, Map<Id, Account> oldMap)
    {
        List<Account> changed = new List<Account>();
        for (Account account : newRecords)
        {
            if (account.Primary_Contact_Email__c != oldMap.get(account.Id).Primary_Contact_Email__c)
                changed.add(account);
        }
        return changed;
    }

    public static void associateContacts(List<Account> accounts)
    {
        Set<String> addresses = new Set<String>();
        for (Account account : accounts) addresses.add(account.Primary_Contact_Email__c);
        // now you know all the email addresses
        // you can use this collection to get every Contact in one query

        Map<String, Contact> emailToContact = new Map<String, Contact>();
        for (Contact contact : [
            SELECT Email FROM Contact WHERE Email IN :addresses
        ])
        {
            emailToContact.put(contact.Email, contact);
        }
        //now you can find a Contact by its email address

        for (Account account : accounts)
        {
            String address = account.Primary_Contact_Email__c;
            Contact contact = buildContact(account, emailToContact.get(address));
            emailToContact.put(address, contact);
        }
        upsert emailToContact.values();
        // you should really wrap this in a try/catch
        // I'll leave that code up to you
    }
    static Contact buildContact(Account account, Contact existing)
    {
        Contact contact = new Contact(
            AccountId = account.Id,
            Email = account.Primary_Contact_Email__c,
            LastName=account.Name,
            FirstName='',
            Salutation='',
            MobilePhone=account.Phone,
            PB_Contact_Type__c='Primary'
            
        );
        if (existing != null) contact.Id = existing.Id;
        return contact;
    }
}


<Account TriggerHandler>

public with sharing class AccountTriggerHandler {
    final List<Account> newRecords;
    final Map<Id, Account> oldMap;
    public AccountTriggerHandler(List<Account> newRecords, Map<Id, Account> oldMap)
    {
        this.newRecords = newRecords;
        this.oldMap = oldMap;
    }

    public void afterInsert()
    {
        AccountServices.associateContacts(newRecords);
    }

    public void afterUpdate()
    {
        AccountServices.associateContacts(AccountServices.hasEmailChanged(newRecords, oldMap));
    }

}


<Trigger>

trigger Account on Account (after insert, after update)
{
    AccountTriggerHandler handle = new AccountTriggerHandler(trigger.new, trigger.oldMap);
    if (trigger.isAfter)
    {
        if (trigger.isInsert) handle.afterInsert();
        if (trigger.isUpdate) handle.afterUpdate();
    }
}

Please suggest any solution