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
Tess Sta RomanaTess Sta Romana 

Apex trigger works in Sandbox but fails in production

Auto lead converter works in sandbox but it fails when deployed in production.

ERROR MESSAGE:
---------------------------
Class Name     Method Name     Error Message
unsubscribetestUnsubscribeSystem.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, You're creating a duplicate record. We recommend you use an existing record instead. 
Stack Trace: Class.unsubscribe.testUnsubscribe: line 110, column 1

unsubscribetestUnsubscribe2System.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, You're creating a duplicate record. We recommend you use an existing record instead. 
Stack Trace: Class.unsubscribe.testUnsubscribe2: line 141, column 1

CODE:
---------
trigger AutoConverter on Lead (after update) {
LeadStatus convertStatus = [
          select MasterLabel
          from LeadStatus
          where IsConverted = true
          limit 1
     ];
     List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();

     for (Lead lead: Trigger.new) {
          if (!lead.isConverted && lead.status == 'Sales Qualified') {
               Database.LeadConvert lc = new Database.LeadConvert();
               String oppName = lead.Name;
               
               lc.setLeadId(lead.Id);
               lc.setOpportunityName(oppName);
               lc.setConvertedStatus(convertStatus.MasterLabel);
               
               leadConverts.add(lc);
          }
     }

     if (!leadConverts.isEmpty()) {
          List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
     }
}
Raj VakatiRaj Vakati
Hi Tess ,

Your Deployment is failing due to "unsubscribe" Test Class . In you org seems to be duplicate rules are active.

Here are my recommended solutions. 
Option 1: - Fix the unsubscribe Class and deploy 
Option 2: - Not Recommended ( Deactivate the duplication rule and Deploy and After deploying activate it )

Options 3: - Norecommendeded ( Only Run the Test Class for AutoConverter while Deploying )