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
Projjwal LahiriProjjwal Lahiri 

Custom Code is not working in production

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
 
Santosh Patro 2Santosh Patro 2
Hey  Projjwal Lahir,

Ther reason y are getting this error is you have Duplicate rules enabled in your production org. 
The record you are trying to create might be already present in the production org with the same name. Please check the Matching rules in your production org to identify what is the criteria for duplicate check. 

The reason y this is not happening iun your sandbox is you might not have the same duplicate account in sandbox or you may have disabled the duplicate rules. 

Let me know if this was helpful.