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
shobana shobana 1shobana shobana 1 

code coverage for duplicate management rule in apex class

Hi  i have replicated the duplicate management rule in Apex class for  Account Object. But am getting only 64% code coverage. Can anyone help me to increase the code coverage.
User-added image
Best Answer chosen by shobana shobana 1
Manish BhatiManish Bhati
The problem is I don't want to change the existing code in your test class.
Although this test class can be written in much lesser lines.

Just try this last time:-
@Istest 
public class testforCustomSaveofAccount{
public static Account duplicateRecords;
    static void init(){
 duplicateRecords= new  Account();
    }
public static TestMethod void SaveofAccount(){
RecordType r = [select Id,DeveloperName from RecordType where SObjectType =: 'Account' AND DeveloperName =: 'Corporate_Office'];
    Account acc = new Account(Name = 'Test',BillingStreet = 'Test',
                              BillingCity = 'Test',BillingPostalCode = '343443',BillingCountry = 'Afghanistan',
                              RecordTypeId = r.Id,OwnerId = UserInfo.getUserId());
    insert acc;
    Contact contact1 = new Contact(FirstName= 't1',LastName='Test1', AccountId=acc.Id,MailingStreet = 'Test',
                              MailingCity = 'Test',MailingPostalCode = '343443',MailingCountry = 'Afghanistan',
                              Email='test1@duptest.com' , CurrencyIsoCode='USD');
    insert contact1; 
    ApexPages.StandardController stdcontroller = new ApexPages.Standardcontroller(acc);
            ApexPages.CurrentPage().getparameters().put('id',acc.Id);
            CustomControllerSaveofAccount  cls= new CustomControllerSaveofAccount(stdcontroller);
            acc.BillingStreet='test1';
            acc.BillingCity = 'Test1';
            acc.BillingCountryCode = 'ANT';
            acc.BillingPostalCode = '33443';
            acc.BillingCountry = 'Netherlands Antilles'; 
            acc.BillingState='Netherlands Antilles';
            acc.BillingStateCode='ANT';  
            acc.BillingStreet='345';             
           Database.update(acc,false);
     
     
            Account a1 = new Account(Name = 'Test1',BillingStreet = 'Test1',
                              BillingCity = 'Test1',BillingPostalCode = '34343',BillingCountry = 'Afghanistan',
                              RecordTypeId = r.Id,OwnerId = UserInfo.getUserId());
             Database.insert(a1,false);  
             cls.newAccount=a1;
             cls.hasDuplicateResult = false;
      cls.CustomSaveNew();
      cls.CustomSave();
      cls.newAccount.put('Name','');      
      cls.IgnoreAndSave();
      cls.IgnoreAndSaveNew();            
      cls.FetchAccountRecord(acc.id); 
      a1.Name = 'Test';
      Database.SaveResult saveResult = Database.update(a1, false);
      cls.newAccount=a1;
      cls.hasDuplicateResult = true;
      cls.CustomSave();
      cls.newAccount.put('Name','');      
      cls.IgnoreAndSave();
      cls.IgnoreAndSaveNew();  
     Account a2 = a1;
    
    try{
    Database.insert(a2,false);  
    Account a3 = a2;
    Database.insert(a3,false);
      cls.newAccount=a2;
      cls.hasDuplicateResult = true;
      cls.CustomSaveNew();
      cls.CustomSave();    
    }
    catch(Exception e)
{
  ApexPages.Message errorMessage = new ApexPages.Message(
                            ApexPages.Severity.ERROR, 'Duplicate Error: You may be creating a duplicate record. If so, we recommend you use the existing record instead. ' );
} 
      cls.newAccount.put('Name','');
      cls.IgnoreAndSave();
      cls.IgnoreAndSaveNew();    
      a1.Name = 'Test';
      Database.update(a2,false);  
      cls.newAccount=a2;
      duplicateRecords = a2;
      cls.hasDuplicateResult = true;
      cls.CustomSaveNew();
      cls.CustomSave();
      cls.newAccount.put('Name','');
      cls.IgnoreAndSave();
      cls.IgnoreAndSaveNew(); 
      
              
  }          
}

If this doesn't work then put SeeAllData=true in the starting.

All Answers

Manish BhatiManish Bhati
Just create a case where you try to input a duplicate record and the error is invoked.
The flow will then go to if statement and your code coverage will increase.
shobana shobana 1shobana shobana 1
Hi Manish Bhati

Thank you for your reply. Create a case means, i want to insert the sample record? would you please share the sample code for this.
 
Manish BhatiManish Bhati
yes, try inserting the sample record twice so that second time it becomes duplicate record.
Or try inserting record which is already available for the object (but for that you have to write Seealldata=true)

If you want code then just give your test class, I will help with your code.
shobana shobana 1shobana shobana 1
Hi Manish
@Istest 
public class testforCustomSaveofAccount{
public static Account duplicateRecords;
    static void init(){
 duplicateRecords= new  Account();
    }
public static TestMethod void SaveofAccount(){
RecordType r = [select Id,DeveloperName from RecordType where SObjectType =: 'Account' AND DeveloperName =: 'Corporate_Office'];
    Account acc = new Account(Name = 'Test',BillingStreet = 'Test',
                              BillingCity = 'Test',BillingPostalCode = '343443',BillingCountry = 'Afghanistan',
                              RecordTypeId = r.Id,OwnerId = UserInfo.getUserId());
    insert acc;
    Contact contact1 = new Contact(FirstName= 't1',LastName='Test1', AccountId=acc.Id,MailingStreet = 'Test',
                              MailingCity = 'Test',MailingPostalCode = '343443',MailingCountry = 'Afghanistan',
                              Email='test1@duptest.com' , CurrencyIsoCode='USD');
    insert contact1; 
    ApexPages.StandardController stdcontroller = new ApexPages.Standardcontroller(acc);
            ApexPages.CurrentPage().getparameters().put('id',acc.Id);
            CustomControllerSaveofAccount  cls= new CustomControllerSaveofAccount(stdcontroller);
            acc.BillingStreet='test1';
            acc.BillingCity = 'Test1';
            acc.BillingCountryCode = 'ANT';
            acc.BillingPostalCode = '33443';
            acc.BillingCountry = 'Netherlands Antilles'; 
            acc.BillingState='Netherlands Antilles';
            acc.BillingStateCode='ANT';  
            acc.BillingStreet='345';             
           Database.update(acc,false);
     
     
            Account a1 = new Account(Name = 'Test1',BillingStreet = 'Test1',
                              BillingCity = 'Test1',BillingPostalCode = '34343',BillingCountry = 'Afghanistan',
                              RecordTypeId = r.Id,OwnerId = UserInfo.getUserId());
             Database.insert(a1,false);  
             cls.newAccount=a1;
             cls.hasDuplicateResult = false;
      cls.CustomSaveNew();
      cls.CustomSave();
      cls.newAccount.put('Name','');      
      cls.IgnoreAndSave();
      cls.IgnoreAndSaveNew();            
      cls.FetchAccountRecord(acc.id); 
      a1.Name = 'Test';
      Database.SaveResult saveResult = Database.update(a1, false);
      cls.newAccount=a1;
      cls.hasDuplicateResult = true;
      cls.CustomSave();
      cls.newAccount.put('Name','');      
      cls.IgnoreAndSave();
      cls.IgnoreAndSaveNew();  
     Account a2 = new Account(Name = 'Test',BillingStreet = 'Test1',
                              BillingCity = 'Test1',BillingPostalCode = '34343',BillingCountry = 'Afghanistan',
                              RecordTypeId = r.Id,OwnerId = UserInfo.getUserId());
    
    try{
    Database.insert(a2,false);  
      cls.newAccount=a2;
      cls.hasDuplicateResult = true;
      cls.CustomSaveNew();
      cls.CustomSave();    
    }
    catch(Exception e)
{
  ApexPages.Message errorMessage = new ApexPages.Message(
                            ApexPages.Severity.ERROR, 'Duplicate Error: You may be creating a duplicate record. If so, we recommend you use the existing record instead. ' );
} 
      cls.newAccount.put('Name','');
      cls.IgnoreAndSave();
      cls.IgnoreAndSaveNew();    
      a1.Name = 'Test';
      Database.update(a2,false);  
      cls.newAccount=a2;
      duplicateRecords = a2;
      cls.hasDuplicateResult = true;
      cls.CustomSaveNew();
      cls.CustomSave();
      cls.newAccount.put('Name','');
      cls.IgnoreAndSave();
      cls.IgnoreAndSaveNew(); 
      
              
  }          
}

 
Manish BhatiManish Bhati
@Istest 
public class testforCustomSaveofAccount{
public static Account duplicateRecords;
    static void init(){
 duplicateRecords= new  Account();
    }
public static TestMethod void SaveofAccount(){
RecordType r = [select Id,DeveloperName from RecordType where SObjectType =: 'Account' AND DeveloperName =: 'Corporate_Office'];
    Account acc = new Account(Name = 'Test',BillingStreet = 'Test',
                              BillingCity = 'Test',BillingPostalCode = '343443',BillingCountry = 'Afghanistan',
                              RecordTypeId = r.Id,OwnerId = UserInfo.getUserId());
    insert acc;
    Contact contact1 = new Contact(FirstName= 't1',LastName='Test1', AccountId=acc.Id,MailingStreet = 'Test',
                              MailingCity = 'Test',MailingPostalCode = '343443',MailingCountry = 'Afghanistan',
                              Email='test1@duptest.com' , CurrencyIsoCode='USD');
    insert contact1; 
    ApexPages.StandardController stdcontroller = new ApexPages.Standardcontroller(acc);
            ApexPages.CurrentPage().getparameters().put('id',acc.Id);
            CustomControllerSaveofAccount  cls= new CustomControllerSaveofAccount(stdcontroller);
            acc.BillingStreet='test1';
            acc.BillingCity = 'Test1';
            acc.BillingCountryCode = 'ANT';
            acc.BillingPostalCode = '33443';
            acc.BillingCountry = 'Netherlands Antilles'; 
            acc.BillingState='Netherlands Antilles';
            acc.BillingStateCode='ANT';  
            acc.BillingStreet='345';             
           Database.update(acc,false);
     
     
            Account a1 = new Account(Name = 'Test1',BillingStreet = 'Test1',
                              BillingCity = 'Test1',BillingPostalCode = '34343',BillingCountry = 'Afghanistan',
                              RecordTypeId = r.Id,OwnerId = UserInfo.getUserId());
             Database.insert(a1,false);  
             cls.newAccount=a1;
             cls.hasDuplicateResult = false;
      cls.CustomSaveNew();
      cls.CustomSave();
      cls.newAccount.put('Name','');      
      cls.IgnoreAndSave();
      cls.IgnoreAndSaveNew();            
      cls.FetchAccountRecord(acc.id); 
      a1.Name = 'Test';
      Database.SaveResult saveResult = Database.update(a1, false);
      cls.newAccount=a1;
      cls.hasDuplicateResult = true;
      cls.CustomSave();
      cls.newAccount.put('Name','');      
      cls.IgnoreAndSave();
      cls.IgnoreAndSaveNew();  
     Account a2 = a1;
    
    try{
    Database.insert(a2,false);  
      cls.newAccount=a2;
      cls.hasDuplicateResult = true;
      cls.CustomSaveNew();
      cls.CustomSave();    
    }
    catch(Exception e)
{
  ApexPages.Message errorMessage = new ApexPages.Message(
                            ApexPages.Severity.ERROR, 'Duplicate Error: You may be creating a duplicate record. If so, we recommend you use the existing record instead. ' );
} 
      cls.newAccount.put('Name','');
      cls.IgnoreAndSave();
      cls.IgnoreAndSaveNew();    
      a1.Name = 'Test';
      Database.update(a2,false);  
      cls.newAccount=a2;
      duplicateRecords = a2;
      cls.hasDuplicateResult = true;
      cls.CustomSaveNew();
      cls.CustomSave();
      cls.newAccount.put('Name','');
      cls.IgnoreAndSave();
      cls.IgnoreAndSaveNew(); 
      
              
  }          
}

try this if it works.
Manish BhatiManish Bhati
I only changed line number 51, as my prime motive is to make Account a2 same as Account a1.

but you can put system.debug(a1);
before line 51 and system.debug(a2);
after line 51 to know whether both are same.
shobana shobana 1shobana shobana 1
Hi Manish Bhati
still 64% only.
Manish BhatiManish Bhati
Are a1 and a2 same in debug logs?
shobana shobana 1shobana shobana 1

Hi Manish

Both are same only

Manish BhatiManish Bhati
The problem is I don't want to change the existing code in your test class.
Although this test class can be written in much lesser lines.

Just try this last time:-
@Istest 
public class testforCustomSaveofAccount{
public static Account duplicateRecords;
    static void init(){
 duplicateRecords= new  Account();
    }
public static TestMethod void SaveofAccount(){
RecordType r = [select Id,DeveloperName from RecordType where SObjectType =: 'Account' AND DeveloperName =: 'Corporate_Office'];
    Account acc = new Account(Name = 'Test',BillingStreet = 'Test',
                              BillingCity = 'Test',BillingPostalCode = '343443',BillingCountry = 'Afghanistan',
                              RecordTypeId = r.Id,OwnerId = UserInfo.getUserId());
    insert acc;
    Contact contact1 = new Contact(FirstName= 't1',LastName='Test1', AccountId=acc.Id,MailingStreet = 'Test',
                              MailingCity = 'Test',MailingPostalCode = '343443',MailingCountry = 'Afghanistan',
                              Email='test1@duptest.com' , CurrencyIsoCode='USD');
    insert contact1; 
    ApexPages.StandardController stdcontroller = new ApexPages.Standardcontroller(acc);
            ApexPages.CurrentPage().getparameters().put('id',acc.Id);
            CustomControllerSaveofAccount  cls= new CustomControllerSaveofAccount(stdcontroller);
            acc.BillingStreet='test1';
            acc.BillingCity = 'Test1';
            acc.BillingCountryCode = 'ANT';
            acc.BillingPostalCode = '33443';
            acc.BillingCountry = 'Netherlands Antilles'; 
            acc.BillingState='Netherlands Antilles';
            acc.BillingStateCode='ANT';  
            acc.BillingStreet='345';             
           Database.update(acc,false);
     
     
            Account a1 = new Account(Name = 'Test1',BillingStreet = 'Test1',
                              BillingCity = 'Test1',BillingPostalCode = '34343',BillingCountry = 'Afghanistan',
                              RecordTypeId = r.Id,OwnerId = UserInfo.getUserId());
             Database.insert(a1,false);  
             cls.newAccount=a1;
             cls.hasDuplicateResult = false;
      cls.CustomSaveNew();
      cls.CustomSave();
      cls.newAccount.put('Name','');      
      cls.IgnoreAndSave();
      cls.IgnoreAndSaveNew();            
      cls.FetchAccountRecord(acc.id); 
      a1.Name = 'Test';
      Database.SaveResult saveResult = Database.update(a1, false);
      cls.newAccount=a1;
      cls.hasDuplicateResult = true;
      cls.CustomSave();
      cls.newAccount.put('Name','');      
      cls.IgnoreAndSave();
      cls.IgnoreAndSaveNew();  
     Account a2 = a1;
    
    try{
    Database.insert(a2,false);  
    Account a3 = a2;
    Database.insert(a3,false);
      cls.newAccount=a2;
      cls.hasDuplicateResult = true;
      cls.CustomSaveNew();
      cls.CustomSave();    
    }
    catch(Exception e)
{
  ApexPages.Message errorMessage = new ApexPages.Message(
                            ApexPages.Severity.ERROR, 'Duplicate Error: You may be creating a duplicate record. If so, we recommend you use the existing record instead. ' );
} 
      cls.newAccount.put('Name','');
      cls.IgnoreAndSave();
      cls.IgnoreAndSaveNew();    
      a1.Name = 'Test';
      Database.update(a2,false);  
      cls.newAccount=a2;
      duplicateRecords = a2;
      cls.hasDuplicateResult = true;
      cls.CustomSaveNew();
      cls.CustomSave();
      cls.newAccount.put('Name','');
      cls.IgnoreAndSave();
      cls.IgnoreAndSaveNew(); 
      
              
  }          
}

If this doesn't work then put SeeAllData=true in the starting.
This was selected as the best answer
shobana shobana 1shobana shobana 1
Hi Manish
Thank you for you help