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
force shahidforce shahid 

How to write test class for this campaign member trigger

Hi Friends,

Good Morning. I need to write a test class for the below trigger. I wrote a test class for insert , updation of the Contact / Lead records. In this trigger i need to check this particular record act as a campain member or not ? If Yes , I need to update . No, I  have to create . I didn't understand how to write test class for this scenario .

Trigger :
trigger Dataimport on New_Lead__c(before insert, before update) {
    List <Contact> upcon = new List <Contact> ();
    List <Contact> upcon1 = new List <Contact> ();
    List <Contact> upcon2 = new List <Contact> ();
    List <Contact> incon = new List <Contact> ();
    Integer count;
List <Campaign> campCrt = new List <Campaign> ();
    List <Campaign> camprecds = [select name, (select Name, Status, ContactId,LeadId,Phone, MobilePhone, 
                                     CompanyOrAccount from CampaignMembers ) from Campaign];
    List <CampaignMember> campMemUpd = new List <CampaignMember>();
List <CampaignMember> campMemcre = new List <CampaignMember>();
    List <Account> accs = [select Email_Domain__c, (select LastName,Email, otherphone, phone, Contact_Status__c,Title,
                                  MailingStreet,MailingCity,MailingState,MailingPostalCode
                                                    from Contacts) from Account];
List <CampaignMember> campMem = [select Name, Status, ContactId,LeadId,Phone, MobilePhone, 
                                     CompanyOrAccount from CampaignMember];
for (New_Lead__c customObjRecord: Trigger.New) { // Iterate over the custom object records
           if(customObjRecord.Email_Address__c!=null && customObjRecord.Email_Address__c !=''){   
                custObjEmailDomain = ((customObjRecord.Email_Address__c).split('@')[1]).split( '\\.')[0];
           }
        System.debug(customObjRecord);
        Boolean accCrtFlag = true;
        for (Account a: accs) { // Iterate over each account object
            if ( a.Email_Domain__c != null && custObjEmailDomain == a.Email_Domain__c &&
                (custObjEmailDomain != '' || custObjEmailDomain != null)) {
                accCrtFlag = false;  
                Boolean inconFlag = true;
                for (Contact c: a.Contacts) {
                    System.debug(c);
                    if ((customObjRecord.Email_Address__c != null && c.email != null && customObjRecord.Email_Address__c ==
                            c.email && customObjRecord.Email_Address__c != '') && customObjRecord.Office_Phone__c != c.phone ) // If Email ids are same
                    {
                        System.debug('Email Matched');
                        inconFlag = false;
                        contact c1 = c;
                        if (customObjRecord.name != null && customObjRecord.name != '')
                            c1.LastName = customObjRecord.Name;
                        if (customObjRecord.First_Name__c != null && customObjRecord.First_Name__c != '')
                            c1.FirstName = customObjRecord.First_Name__c;
                        if (customObjRecord.Office_Phone__c != '' && customObjRecord.Office_Phone__c != null)
                            c1.OtherPhone = customObjRecord.Office_Phone__c; // Update new phone Field value into other phone Field
                        if (customObjRecord.Business_Card_Title__c != null && customObjRecord.Business_Card_Title__c !='')
                            c1.Title=customObjRecord.Business_Card_Title__c;
                        if (customObjRecord.Primary_Address_Street__c != null && customObjRecord.Primary_Address_Street__c !='')
                            c1.MailingStreet=customObjRecord.Primary_Address_Street__c;
                        if (customObjRecord.Primary_Address_City__c != null && customObjRecord.Primary_Address_City__c !='')
                            c1.MailingCity=customObjRecord.Primary_Address_City__c;
                        if (customObjRecord.Primary_Address_State__c != null && customObjRecord.Primary_Address_State__c !='')
                           c1.MailingState=customObjRecord.Primary_Address_State__c;
                        if (customObjRecord.Primary_Address_Country__c != null && customObjRecord.Primary_Address_Country__c !='')
                           c1.MailingCountry=customObjRecord.Primary_Address_Country__c;
                        if (customObjRecord.Primary_Address_Postal_Code__c != null && customObjRecord.Primary_Address_Postal_Code__c !='')
                           c1.MailingPostalCode=customObjRecord.Primary_Address_Postal_Code__c;
                        
                                                
                        Boolean campCrtFlag = true; 
                        
                        for (Campaign camp: camprecds){
                            if(camp.Name==customObjRecord.Campaign__c){
                                for(CampaignMember cm: camp.CampaignMembers){
                                    if(cm.ContactId==c1.Id){
                                        campCrtFlag=false;
                                    }
                                }
                                if(campCrtFlag){
                                    CampaignMember cm = new CampaignMember(CampaignId = camp.Id, ContactId = c1.Id,
                                                                           status = customObjRecord.Member_Status__c);
                                    campMemcre.add(cm);
                                }
                                
                            }
                        }
                        upcon.add(c1); // Update the existing record in Contacts
                    }
                    else if (customObjRecord.Email_Address__c != c.email && (customObjRecord.Office_Phone__c == c.phone &&
                            customObjRecord.Office_Phone__c != '' && customObjRecord.Office_Phone__c != null)) // If phone numbers are same
                    {
                        System.debug('Phone Matched');
                        inconFlag = false;
                        contact c2 = c;
                        if (customObjRecord.Name != null && customObjRecord.Name != '') // Update LastName field when new Name field value is not empty .
                            c2.LastName = customObjRecord.Name;
                        if (customObjRecord.First_Name__c != null && customObjRecord.First_Name__c != '')
                            c2.FirstName = customObjRecord.First_Name__c;
                        if (customObjRecord.Other_Phone__c != null && customObjRecord.Other_Phone__c != '')
                            c2.OtherPhone = customObjRecord.Other_Phone__c;
                        if (customObjRecord.Email_Address__c != null && customObjRecord.Email_Address__c != '')
                            c2.Email = customObjRecord.Email_Address__c;
                        if (customObjRecord.Business_Card_Title__c != null && customObjRecord.Business_Card_Title__c !='')
                            c2.Title=customObjRecord.Business_Card_Title__c;
                        if (customObjRecord.Primary_Address_Street__c != null && customObjRecord.Primary_Address_Street__c !='')
                            c2.MailingStreet=customObjRecord.Primary_Address_Street__c;
                        if (customObjRecord.Primary_Address_City__c != null && customObjRecord.Primary_Address_City__c !='')
                            c2.MailingCity=customObjRecord.Primary_Address_City__c;
                        if (customObjRecord.Primary_Address_State__c != null && customObjRecord.Primary_Address_State__c !='')
                           c2.MailingState=customObjRecord.Primary_Address_State__c;
                        if (customObjRecord.Primary_Address_Country__c != null && customObjRecord.Primary_Address_Country__c !='')
                           c2.MailingCountry=customObjRecord.Primary_Address_Country__c;
                        if (customObjRecord.Primary_Address_Postal_Code__c != null && customObjRecord.Primary_Address_Postal_Code__c !='')
                           c2.MailingPostalCode=customObjRecord.Primary_Address_Postal_Code__c;
                        
                        Boolean campCrtFlag = true;
                        for (Campaign camp: camprecds){
                            if(camp.Name==customObjRecord.Campaign__c){
                                for(CampaignMember cm: camp.CampaignMembers){
                                    if(cm.ContactId==c2.Id){
                                        campCrtFlag=false;
                                    }
                                }
                                if(campCrtFlag){
                                    CampaignMember cm = new CampaignMember(CampaignId = camp.Id, ContactId = c2.Id,
                                                                           status = customObjRecord.Member_Status__c);
                                    campMemcre.add(cm);
                                }
                            }
                        }
                      
                        upcon.add(c2); // Update the existing record in Contacts
                    }
                    else if ((customObjRecord.Email_Address__c == c.email && customObjRecord.Email_Address__c != '' &&
                            customObjRecord.Email_Address__c != null) && (customObjRecord.Office_Phone__c == c.phone &&
                            customObjRecord.Office_Phone__c != '' && customObjRecord.Office_Phone__c != null)) // If phone numbers are same
                    {
                        System.debug('Both Matched');
                        inconFlag = false;
                        contact c3 = c;
                        if (customObjRecord.Name != null && customObjRecord.Name != '') // Update LastName field when new Name field value is not empty .
                            c3.LastName = customObjRecord.Name;
                        if (customObjRecord.First_Name__c != null && customObjRecord.First_Name__c != '')
                            c3.FirstName = customObjRecord.First_Name__c;
                        if (customObjRecord.Business_Card_Title__c != null && customObjRecord.Business_Card_Title__c !='')
                            c3.Title=customObjRecord.Business_Card_Title__c;
                        if (customObjRecord.Primary_Address_Street__c != null && customObjRecord.Primary_Address_Street__c !='')
                            c3.MailingStreet=customObjRecord.Primary_Address_Street__c;
                        if (customObjRecord.Primary_Address_City__c != null && customObjRecord.Primary_Address_City__c !='')
                            c3.MailingCity=customObjRecord.Primary_Address_City__c;
                        if (customObjRecord.Primary_Address_State__c != null && customObjRecord.Primary_Address_State__c !='')
                           c3.MailingState=customObjRecord.Primary_Address_State__c;
                        if (customObjRecord.Primary_Address_Country__c != null && customObjRecord.Primary_Address_Country__c !='')
                           c3.MailingCountry=customObjRecord.Primary_Address_Country__c;
                        if (customObjRecord.Primary_Address_Postal_Code__c != null && customObjRecord.Primary_Address_Postal_Code__c !='')
                           c3.MailingPostalCode=customObjRecord.Primary_Address_Postal_Code__c;
                        
                        Boolean campCrtFlag = true;
                        for (Campaign camp: camprecds){
                            if(camp.Name==customObjRecord.Campaign__c){
                                for(CampaignMember cm: camp.CampaignMembers){
                                    if(cm.ContactId==c3.Id)
                                         campCrtFlag=false;
                                }
                                if(campCrtFlag){
                                    CampaignMember cm = new CampaignMember(CampaignId = camp.Id, ContactId = c3.Id,
                                                                           status = customObjRecord.Member_Status__c);
                                    campMemcre.add(cm);
                                }
                            }
                        }
                        upcon.add(c3); // Update the existing record in Contacts
                    }
                }
                   if (inconFlag) // Email & Phone both are different
                    {
                        System.debug('None Matched');
                        count=01;
                        contact c4 = new contact();
                        c4.AccountId = a.Id;
                        c4.LastName = customObjRecord.Name;
                        c4.FirstName = customobjrecord.First_Name__c;
                        c4.OtherPhone = customObjRecord.Other_Phone__c;
                        c4.Email = customObjRecord.Email_Address__c;
                        c4.Phone = customObjRecord.Office_Phone__c;
                        c4.Title=customObjRecord.Business_Card_Title__c;
                        c4.MailingStreet=customObjRecord.Primary_Address_Street__c;
                        c4.MailingCity=customObjRecord.Primary_Address_City__c;
                        c4.MailingState=customObjRecord.Primary_Address_State__c;
                        c4.MailingCountry=customObjRecord.Primary_Address_Country__c;
                        c4.MailingPostalCode=customObjRecord.Primary_Address_Postal_Code__c;
                        c4.External_Con_Id__c='Ext'+count+customObjRecord.Office_Phone__c;
                        System.debug('Ext'+count+customObjRecord.Office_Phone__c);
                        incon.add(c4); // Create new contact
                        for (Campaign camp: camprecds){
                            if(camp.Name==customObjRecord.Campaign__c){
                                    String extname='Ext'+count+customObjRecord.Office_Phone__c;
                                    Contact cnew=new Contact(External_Con_Id__c=extname);
                                       System.debug(extname);
                                    CampaignMember cm = new CampaignMember(CampaignId = camp.Id, Contact = cnew,
                                                                           Status=customobjrecord.Member_Status__c);
                                    campMemcre.add(cm);
                                
                            }
                        }
                        count++;
                    }
            }
        }
 if (incon.size() > 0){
        Database.saveResult[] rslt=Database.insert(incon, false);
        for(Database.saveResult r: rslt){
            if(r.isSuccess()){
                System.debug(r.getId());
            }
            else{
                System.debug(r.getErrors());
            }
        }
    }
if (campMemcre.size() > 0){
        Database.saveResult[] rslt=Database.insert(campmemCre, false);
        for(Database.saveResult r: rslt){
            if(r.isSuccess()){
                System.debug(r.getId());
            }
            else{
                System.debug(r.getErrors());
            }
        }
    }
        System.debug('New Camp Members'+campMemCre);
    if (uplead.size() > 0){
        Database.saveResult[] rslt=Database.update(uplead, false);
        for(Database.saveResult r: rslt){
            if(r.isSuccess()){
                System.debug(r.getId());
            }
            else{
                System.debug(r.getErrors());
            }
        }
    }
    if (campMemUpd.size() > 0){
        Database.saveResult[] rslt=Database.update(campMemUpd, false);
        for(Database.saveResult r: rslt){
            if(r.isSuccess()){
                System.debug(r.getId());
            }
            else{
                System.debug(r.getErrors());
            }
        }
    }
    
}
Sitarama MurthySitarama Murthy
Hi force shahid,

Create the Test Class with 2 test Methods in it. 
  1. first test method will cover the 'YES' condition based on your requirement. 
  2. second test method will cover the 'NO' condition based on your requirement.
Hope this helps.

Thanks,
Ram
force shahidforce shahid
Hi Sitarama Murthy,

I wrote test class based on yes condition. All conditions are satisfied but the campaign Member scenarios are not satified. If i add one contact as a campaignmember it doesn't give the proper code coverage.

Before adding campain member the code coverage is around 60 - 70.
After adding one updated contact as a campaign member the code coverage area is 21 %.

That's y I m asking how to write a test class for this trigger.
 Unfortunatly i delete the Test class.
Can you provide the test class code.

Thanks,
Shahid.
 
Sitarama MurthySitarama Murthy
Hey,

Use the same Test class given above  and create one more test method give the campaign member scenarios in the newly created test method and try.

Thanks,