0

I have a trigger on duplicateRecordItem object, which is working fine. I have also written a test class for the same, which is passing, but test coverage is showing as 0%. Can anybody help me out.

Here is my code.

Apex trigger:

//This trigger sends the email to the owner of the duplicate record.
trigger sendEmailToRecordOwner on DuplicateRecordItem (after insert) {

    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    Set<Id> AccRecordIds = new Set<Id>();
    //Lets iterate over all the records being created, and find out the record ids of 
    for(DuplicateRecordItem dup : trigger.new){
        if(String.valueOf(dup.RecordId).startsWith('001')){
             AccRecordIds.add(dup.RecordId);       
        }
    }

    Map<Id, Account> accMap = new Map<id, Account>();
    Id templateId = [select id from EmailTemplate where developerName = 'Duplicate_Record_Created' Limit 1].id;

    //Fetch the owner Ids of all the account records, which will be used to send the email to them. 
    if(AccRecordIds.size() > 0){
        accMap = new Map<Id, Account>([select id, ownerId, owner.Email from Account where Id IN: AccRecordIds AND owner.Profile_Name__c != 'Partner Agency User']);

    }

    for(DuplicateRecordItem dup : trigger.new){
        if(String.valueOf(dup.RecordId).startsWith('001') && accMap.containsKey(dup.RecordId)){
            //prepare the email object. 
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setToAddresses(new String[]{accMap.get(dup.RecordId).owner.Email});
            mail.setSubject('Duplicate Record Created');
            String messageBody = '<html><body>A duplicate record was just added into Salesforce. Please visit<br>' + URL.getSalesforceBaseUrl() + '/' + dup.DuplicateRecordSetId +'</body></html>';
            mail.setHtmlBody(messageBody);
            mails.add(mail);

        }
    }

    //send the final email.
    if(mails.size() > 0){
        system.debug('Emails to Send         ' + mails);
        Messaging.sendEmail(mails);

    }
}

Test class code:

@isTest
public class TestsendEmailToRecordOwnerTrigger{

    @testsetup
    static void createOneAccountRecord(){
        User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
        System.runAs (thisUser) {

            Account acc = new Account(name = 'TestAccount', phone='9898787878');
            insert acc;

            EmailTemplate e = new EmailTemplate (developerName = 'Duplicate_Record_Created', FolderId = UserInfo.getUserId(), TemplateType= 'text', Name = 'Duplicate Record Created'); // plus any other fields that you want to set
            insert e;
        }

    }

    public static testMethod void testFirstSituation(){

        Test.startTest();
        Account acc = new Account(name = 'TestAccount', phone='9898787878');
        Database.DMLOptions insertDML = new Database.DMLOptions(); 
        insertDML.DuplicateRuleHeader.AllowSave = true; 
        Database.SaveResult sr = Database.insert(acc, insertDML);

        Test.stopTest();

    }
}
5
  • 1
    You're not inserting record for this DuplicateRecordItem in your test class. Try inserting it. Commented Apr 12, 2017 at 14:15
  • Thanks Rohit !! I don't want to insert a record of DuplicateRecordItem, because for that I will have to create a record of DuplicateRecordSet, as well as a record of DuplicateRule, which will become a bit complex. I am expecting the record to be created automatically. Commented Apr 12, 2017 at 14:35
  • Trigger will only be invoked after the insert operation(DuplicateRecordItem ). You have to insert DuplicateRecordItem for code coverage. Commented Apr 12, 2017 at 14:37
  • 1
    @NitishSinghal Since you've written Trigger after insert of DuplicateRecordItem, you will not be able to do that. However as a standard practice you should write helper class for your trigger and move that chunk of code in helper class. This way you can cover that helper class but still I doubt, it won't cross 75% unless you create setUp data for DuplicateRecordItem. Commented Apr 12, 2017 at 14:38
  • Thanks Rohit, I added the code to insert the DuplicateRecordItem, and now it worked. But I was expecting that duplicate rules will run automatically and insert these records as well. But anyway, thanks again. Commented Apr 12, 2017 at 15:42

1 Answer 1

3

Thanks everyone !!

Here is my final code of test class.

@isTest
public class TestsendEmailToRecordOwnerTrigger{

    @testsetup
    static void createOneAccountRecord(){
        User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
        System.runAs (thisUser) {

            Account acc = new Account(name = 'TestAccount', phone='9898787878');
            insert acc;

            EmailTemplate e = new EmailTemplate (developerName = 'Duplicate_Record_Created', FolderId = UserInfo.getUserId(), TemplateType= 'text', Name = 'Duplicate Record Created'); // plus any other fields that you want to set
            insert e;
        }

    }

    public static testMethod void testFirstSituation(){


        DuplicateRule dR = [select id from DuplicateRule where DeveloperName = 'Dup_Accounts_2' LIMIT 1];
        DuplicateRecordSet dupRS = new DuplicateRecordSet(DuplicateRuleId = dR.id);
        insert dupRS;

        Test.startTest();
        Account acc = new Account(name = 'TestAccount', phone='9898787878');
        Database.DMLOptions insertDML = new Database.DMLOptions(); 
        insertDML.DuplicateRuleHeader.AllowSave = true; 
        Database.SaveResult sr = Database.insert(acc, insertDML);
        DuplicateRecordItem dup = new DuplicateRecordItem(DuplicateRecordSetId = dupRS.id, RecordId=acc.id);
        insert dup;
        Test.stopTest();

    }
}

We don't even need seealldata=true, to query the duplicateRule from org. So, we can query the DuplicateRule from org, then create a duplicateRecordSet, and then duplicateRecordItem record to test the trigger.

Thanks again!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .