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
MaddyConnectMaddyConnect 

Test class failed when I activate trigger to test

Dear Friends,

 

                 I am trying to test triggers in the Sandbox, I deployed Test Class but when I activate trigger to be tested by that Test Class, my test class fails. It is trowing error as per mentioned in trigger. 

E.g. My trigger checks wheather e-mail entered in Lead is already exist in the Lead or contact object. So that my trigger works fine but when I run test class, my test class failed.

 

What will be the possible cause of such failure of trigger?

 

Thanks & Regards,

Maddy

HaagenHaagen

Hi Maddy,

 

can you please post the error message and possible some code for us to look at?

 

Cheers!

 

Martin

MaddyConnectMaddyConnect

Thanks Martin for reply. Following is code snippet of my trigger, This trigger basically checks whether entered e-mail in Lead exist in Lead's and Contact's Primary and Secondary Email filed respectively.

 

 

trigger CheckEmailForDuplication on Lead (before insert, before update) 
{
    Map<String, Lead> LeadMap_Insert = new Map<String, Lead>();
    Map<String, Lead> LeadMap_Update = new Map<String, Lead>();
    for(Lead led: System.Trigger.new)
    {
        if(Lead.RecordTypeId != null)
        {
            if(System.Trigger.isinsert || System.Trigger.isupdate)
            {
                if(Trigger.isInsert)
                {
                    LeadMap_Insert.put(led.Email, led);
                }
                if(Trigger.isUpdate)
                {
                    if(System.Trigger.oldMap!=null && (led.Email != System.Trigger.oldMap.get(led.Id).Email))
                    {
                        if(led.Email != null || led.Email != '')
                        {
                            LeadMap_Update.put(led.Email, led);
                        }
                    }
                }
            }
        }
    }
    if(LeadMap_Insert.size() > 0)
    {
        for(Lead L : [SELECT PRG_Email__c, Name FROM Lead WHERE PRG_Email__c IN :LeadMap_Insert.KeySet() limit 1000]) 
        {
            Lead newLead = LeadMap_Insert.get(L.PRG_Email__c);
            if(L.PRG_Email__c != null)
            {
                newLead.Email.addError('Email already exist for Lead: <b><a href="/' + L.Id + '">' + L.Name + '</a></b>');
                break;
            }
        }
        for(Contact C : [SELECT PRG_Primary_Email__c, Name FROM Contact WHERE PRG_Primary_Email__c IN :LeadMap_Insert.KeySet() limit 1000]) 
        {
            Lead newLead = LeadMap_Insert.get(C.PRG_Primary_Email__c);
            if(C.PRG_Primary_Email__c != null)
            {
                newLead.Email.addError('Email already exist for Contact: <b><a href="/' + C.Id + '">' + C.Name + '</a></b>');
                break;
            }
        }
        for(Contact C : [SELECT GI_Personal_Email__c, Name FROM Contact WHERE GI_Personal_Email__c IN :LeadMap_Insert.KeySet() limit 1000]) 
        {
            Lead newLead = LeadMap_Insert.get(C.GI_Personal_Email__c);
            if(C.GI_Personal_Email__c != null)
            {
                newLead.Email.addError('Email already exist for Contact: <b><a href="/' + C.Id + '">' + C.Name + '</a></b>');
                break;
            }
        }
    }
    if(LeadMap_Update.size() > 0)
    {
        for(Lead L : [SELECT PRG_Email__c, Name FROM Lead WHERE PRG_Email__c IN :LeadMap_Update.KeySet() limit 1000]) 
        {
            Lead newLead = LeadMap_Update.get(L.PRG_Email__c);
            {
                newLead.Email.addError('Email already exist for Lead: <b><a href="/' + L.Id + '">' + L.Name + '</a></b>');
                break;
            }
        }
        for(Contact C : [SELECT PRG_Primary_Email__c, Name FROM Contact WHERE PRG_Primary_Email__c IN :LeadMap_Update.KeySet() limit 1000]) 
        {
            Lead newLead = LeadMap_Update.get(C.PRG_Primary_Email__c);
            if(C.PRG_Primary_Email__c != null)
            {
                newLead.Email.addError('Email already exist for Contact: <b><a href="/' + C.Id + '">' + C.Name + '</a></b>');
                break;
            }
        }
        for(Contact C : [SELECT GI_Personal_Email__c, Name FROM Contact WHERE GI_Personal_Email__c IN :LeadMap_Update.KeySet() limit 1000]) 
        {
            Lead newLead = LeadMap_Update.get(C.GI_Personal_Email__c);
            if(C.GI_Personal_Email__c != null)
            {
                newLead.Email.addError('Email already exist for Contact: <b><a href="/' + C.Id + '">' + C.Name + '</a></b>');
                break;
            }
        }
    }
}

 

 

I write test class for the same. In that class, I created two lead with same email address to cover above code. So I played with combination of Lead's Primary exist in Lead's secondary and vice vesa.... etc.

But when I run Test  Class it failed and gives following Error:

 

System.DmlException: InsertFailed First Exception on Row 0, First Error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Email already exist for contact: <b><a href="/003T000000rxcDMTAY">Test Name</a></b>: [Email]

Class.TestCkeckOnLead.insertLead3: External Entry Pont

 

Thanks & Regards,

Maddy.

 

MaddyConnectMaddyConnect

Can anybody help me?

 

Thanks & Regards,

Maddy

MaddyConnectMaddyConnect

My trigger contain 'Custome Error Validation'. So whenever I execute my test class to cover code line of 'Custome Error Validation', it throws error of 'FIELD_CUSTOM_VALIDATION_EXCEPTION' for TEST CLASS. How can I get rid from this error message?