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
mohan.smmohan.sm 

Test Case for Case Contact Creation Trigger

Hi All,

Can some one help me with the test case for this trigger?
When an email to case is created it will search for contact based on email id and stamp the contact if found if not it will create a contact and stamp the value on case.


trigger TriggertoCreateContactformCase on Case (before insert) {
    List<String> UseremailAddresses = new List<String>();
    //First exclude any cases where the contact is set
    for (Case c:Trigger.new) {
        if (c.ContactId==null &&
            c.SuppliedEmail!=''|| c.SuppliedEmail==null)
        {
            UseremailAddresses.add(c.SuppliedEmail);
        }
    }

    //Now we have a nice list of all the email addresses.  Let's query on it and see how many contacts already exist.
    List<Contact> listofallContacts = [Select Id,Email From Contact Where Email in:UseremailAddresses];
    Set<String> ExstingEmails = new Set<String>();
    for (Contact c:listofallContacts) {
        ExstingEmails.add(c.Email);
    }
    
    Map<String,Contact> emailToContactMap = new Map<String,Contact>();
    List<Case> casesToUpdate = new List<Case>();

    for (Case c:Trigger.new) {
        if (c.ContactId==null &&
            c.SuppliedName!=null &&
            c.SuppliedEmail!=null &&
            c.SuppliedName!='' &&
           !c.SuppliedName.contains('@') &&
            c.SuppliedEmail!='' &&
           !ExstingEmails.contains(c.SuppliedEmail))
        {
            //The case was created with a null contact
            //Let's make a contact for it
            String[] Emailheader = c.SuppliedName.split(' ',2);
            if (Emailheader.size() == 2)
            {
                Contact conts = new Contact(FirstName=Emailheader[0],
                                            LastName=Emailheader[1],
                                            Email=c.SuppliedEmail
                                            );
                emailToContactMap.put(c.SuppliedEmail,conts);
                casesToUpdate.add(c);
            }
        }
    }
    
    List<Contact> newContacts = emailToContactMap.values();
    insert newContacts;
    
    for (Case c:casesToUpdate) {
        Contact newContact = emailToContactMap.get(c.SuppliedEmail);
        
        c.ContactId = newContact.Id;
    }
}


 
Amit Singh 1Amit Singh 1
Ok, use below code.
 
@isTest
private class TriggertoCreateContactformCase_Test{
    static testMethod void testMethod_Test(){
        Contact c = new Contact(LastName='Test', Email='test@test.com');
        insert c;
        List<Case> caseList = new List<Case>();
        for(Integer i=0;i<200;i++){
            Case c1 = new Case(SuppliedEmail='test@test1.com', SuppliedName='TestSuplied Name',SuppliedPhone='734946');
            caseList.add(c1);
        }
        if(caseList!=null && caseList.size()>0){
            insert caseList;
        }
    }
}

Let me know if this helps :)

Thanks,
Amit Singh
NavRamNavRam
Thanks Amit. Code give 96%.
I have tweeked in your code to get 100% Coverage.
 
@isTest
private class TriggertoCreateContactformCase_Test{
    static testMethod void testMethod_Test(){
        Contact c = new Contact(LastName='Test', Email='test@test.com');
        insert c;
        List<Case> caseList = new List<Case>();
        for(Integer i=0;i<3;i++){
            Case c1 = new Case(SuppliedEmail='test@test.com', SuppliedName='TestSuplied Name',SuppliedPhone='734946');
            caseList.add(c1);
            Case c2 = new Case(SuppliedEmail='test@test1.com', SuppliedName='TestSuplied NoName',SuppliedPhone='734946');
            caseList.add(c2);
        }
        if(caseList!=null && caseList.size()>0){
            insert caseList;
        }
    }
}