0

I have created a test class in my sandbox org .I have achieved the code coverage uoto 94%

But when I am deploying it in the production org I am getting an error

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATES_DETECTED, You're creating a duplicate record. We recommend you use an existing record instead.: [] Stack Trace: Class.CustomerLeadCtrl.saveCustomerInfo: line 15, column 1 Class.CustomerLeadCtrlTest.saveCustomerInfoTest: line 18, column 1

I am not getting this error in my sandbox org .Please guide me where I went wrong with the code.

I am pasting both the class and the test class code here

global with sharing class CustomerLeadCtrl {

@remoteAction
global static Boolean saveCustomerInfo(String leadJson){
  system.debug('!!!!'+leadJson);
  LeadWrapper LeadRec =  (LeadWrapper)System.JSON.deserialize(leadJson, LeadWrapper.class);

  lead ldRec =  new lead( FirstName = LeadRec.First_Name, 
                             lastName  =  LeadRec.LastName,
                             Email =  LeadRec.Email,
                             Message__c = LeadRec.Message,
                             Company = 'Shipperman',
                             LeadSource = 'Website'
                             );
  insert ldRec;

  if(ldRec.Id != null){
        return true;
  }
  return false;
}

public class LeadWrapper {

public String lcompny;
public String First_Name;
public String LastName;
public String Email;
public String Message;


public LeadWrapper(Lead leadRec){
    this.lcompny = leadRec.Company;
    this.First_Name = leadRec.FirstName;
    this.LastName =  leadRec.LastName; 
    this.Email = leadRec.Email;
    this.Message = leadRec.Message__c;

}
}

}

Test class:

   @isTest(seeAllData=False)
   public class CustomerLeadCtrlTest {

   static testMethod void saveCustomerInfoTest(){

   Lead l = new lead();
       l.FirstName = 'TestFirstNam1e';
       l.LastName = 'TestLastName1';
       l.company = 'TestCompany1';
       l.LeadSource = 'website1';
       l.email = 'test1@gmail.com';
       l.Message__c='Test data1';
   insert l;

   string LeadJson = Json.serialize(l);
   system.debug('@@@@'+LeadJson);
   Test.startTest();
   Boolean leadstatus = CustomerLeadCtrl.saveCustomerInfo(LeadJson);
   Test.stopTest();    
   }

static testmethod void leadwrapperttest(){
 Lead le = new lead();
       le .FirstName = 'TestFirstName2';
       le .LastName = 'TestLastName2';
       le .company = 'TestCompany2';
       le .LeadSource = 'website2';
       le .email = 'test2@gmail.com';
       le .Message__c='Test data2';
   insert le ;
  CustomerLeadCtrl.LeadWrapper c = new CustomerLeadCtrl.LeadWrapper(le );
  System.assert(true,true);        


}
}
  • 2
    Can you be sure, that Duplication rules on your sandbox are activated? Pay attention, that after refreshing sandbox duplication rules are not activated automaticaly, even if it is active on production. – Oleksandr Berehovskyi May 26 '17 at 11:24
1

this kind of message we get because of duplicate rules activated. So, you need to check if you have Lead duplication rule existing in your production environment.

If yes, then it is possible that you will not be deactivating the duplication rules in Production.

So, to come over it, I would suggest removing your insert statement in test class method and then try. Because right now you are inserting the lead once in your test class, and then in your actual class again.

static testMethod void saveCustomerInfoTest(){

   Lead l = new lead();
   l.FirstName = 'TestFirstNam1e';
   l.LastName = 'TestLastName1';
   l.company = 'TestCompany1';
   l.LeadSource = 'website1';
   l.email = 'test1@gmail.com';
   l.Message__c='Test data1';

    //commented the below line
   //insert l;

   string LeadJson = Json.serialize(l);
   system.debug('@@@@'+LeadJson);
   Test.startTest();
   Boolean leadstatus = CustomerLeadCtrl.saveCustomerInfo(LeadJson);
   Test.stopTest();    
  }

Can you please try once. And do it similarly for other methods as well.

Hope this helps :)

  • 1
    actually, it is not a good idea just to remove everything that fails in tests. This code would not work as expected on production, because due to active duplication rule on production, written code will be failed. – Oleksandr Berehovskyi May 26 '17 at 11:56
  • 2
    I told the reason because we only need the string to be passed to the actual class method, then no need to insert lead in the test class, and just pass the serialized string would work. I don't think that would make any impact on this class's behavior. – Nitish Singhal May 26 '17 at 11:58
  • Hii @NitishSinghal ,Thank you for the answer I commented the insert statements in both the methods .And I am able to deploy this code in the production org.While I agree with Alexnder's point also that if I don't insert my lead then how will the record get created .Though my code is still showing the same percentage of code coverage(94%) but still I am a bit confused that without the record getting created how is it able to show that much coverage.My requirement is already complete.I am just asking this question for my personal knowledge. – Saswata Das May 26 '17 at 12:34
  • You just did not insert the lead in test class, but actual class still inserted the lead. that's why :) – Nitish Singhal May 26 '17 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.