• Heshan Weerasekera 5
  • NEWBIE
  • 10 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Hi All- with the help of this group, I wrote the below trigger and test class. The trigger is run on the camp members object. it calculates the unique members whose accounts are tagged as SQA. For example a campaign can have 50 members for 10 accounts. 5 of these accounts are tagged as SQA and I want that count of 5 shown on the Campaign. The test class passed in SB and I got a code coverage of 76%. when I tried to push to prod I get the error below

System.AssertException: Assertion Failed: Expected: 1, Actual: null 
Stack Trace: Class.UniqueSQAsTest.testCampaignWithSQA: line 54, column 1

my trigger and class below. Any help how to fix will be much appreciated 

Trigger
 
trigger UniqueSQAs on CampaignMember (after Insert, after Update, after Delete, after UnDelete) {
    List<Id> campaignIds = new List<Id>();
 
    if(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete) {
        for(CampaignMember member : Trigger.New) {
            if(member.CampaignId != null && member.ContactId != null && member.Account_ID__c != null && member.SQA_Org__c  == true) {
                campaignIds.add(member.CampaignId);
            }
        }
    }
    
    if(Trigger.IsDelete) {
        for(CampaignMember member : Trigger.Old){
            If(member.CampaignId != null && member.ContactId != null && member.Account_ID__c != null && member.SQA_Org__c  == true) {
                campaignIds.add(member.CampaignId);
            }
        }
    }
 
    // Get a list of Campaigns
    Map<ID,Campaign> campaignsOriginalMap = new Map<ID, Campaign>([SELECT ID, Sum_of_unique_SQAs__c FROM Campaign WHERE Id = :campaignIds]);
    
    // Get a list of CampaignMembers
    List<CampaignMember> campaignMembersLst = [SELECT Id, Account_ID__c, ContactId,CampaignId FROM CampaignMember WHERE CampaignId = :campaignIds AND SQA_Org__c  = true];
    
    // Loop round and remove elements that don't has a ContactId or Account_Id__c populated
    for(Integer i=0; i < campaignMembersLst.size(); i++){
         if(campaignMembersLst.get(i).ContactId == null || campaignMembersLst.get(i).Account_ID__c == null) {
             campaignMembersLst.remove(i);
         }
     }
    
    // Duplicate Account removal...
    Map<Id,CampaignMember> accountIdToCampMemberMap = new Map<Id,CampaignMember>();
    for(CampaignMember memRec : campaignMembersLst){
        if(!accountIdToCampMemberMap.containsKey(memRec.Account_ID__c)){
            accountIdToCampMemberMap.put(memRec.Account_ID__c,memRec);
        }
    }
    
    //Create a map of campaigns to be updated
    Map<Id,Campaign> campaignsToBeUpdatedMap = new Map<Id,Campaign>();
   
    //Iterate through the unique campaign members and add up the unique_SQAs to corresponding campaign
    for(CampaignMember membRec: accountIdToCampMemberMap.values()){
        if(campaignsToBeUpdatedMap.containsKey(membRec.CampaignId)){
            Campaign campaignRec = campaignsToBeUpdatedMap.get(membRec.CampaignId);
            campaignRec.Sum_of_unique_SQAs__c = campaignRec.Sum_of_unique_SQAs__c + 1;
        }else{
            Campaign campaignRec = campaignsOriginalMap.get(membRec.CampaignId);
            campaignRec.Sum_of_unique_SQAs__c = 1;
            campaignsToBeUpdatedMap.put(membRec.CampaignId,campaignRec);
        }
       
    }
   
    try {
        if(campaignsToBeUpdatedMap.size() > 0) {
            update campaignsToBeUpdatedMap.values();
        }
    }
    catch(Exception e) {
        system.debug('Thrown Exception for TotalDifferentCompaniesAttached Is: ' + e.getMessage());
    }
}
 
 
Test
 
@isTest
private class UniqueSQAsTest {
   private static testMethod void testCampaignWithSQA() {
    system.debug('Started Member Insert Test Class');
 
    //Start: Test Data Creation
    //Is there any common test data creation class??
   
    //Insert Account
    Account acct = new Account (Name = 'Acme, Inc.',
                                Website ='www.test.com',
                                Type = 'Other',
                                SQA_Categorisation__c='SQA',
                                Region__c = 'APAC',
                                Sub_Region__c ='India');
    insert acct;
    system.debug('Inserted Account, ID: ' + acct.id);
   
    //Insert Contact
    Contact con = new Contact(
                      FirstName = 'Robin',
                      LastName = 'Koehler',
                      AccountId = acct.Id,
                      Title ='COO',
                      Email ='robin@hotmail.com',
                      Phone ='5544789',
                      LeadSource ='Event',
                      contact_type__c='CCM');
    insert con;  
    system.debug('Inserted Contact, ID: ' + con.id);
 
    //Insert Campaign  
    Campaign camp = new Campaign(
                        Name = 'Test',
                        IsActive = TRUE
                        );           
    insert camp;
    system.debug('Inserted Campaign, ID: ' + camp.id);
   
    //Insert CampaignMember  
    CampaignMember member = new CampaignMember(
        ContactId = con.Id,
        Status = 'Sent',
        CampaignId = camp.Id
        );
    insert member;
   
    system.debug('Inserted CampaignMember, ID: ' + member.Id);
   
    Test.startTest();
   
 List<Campaign> campaigns = [SELECT Sum_of_unique_SQAs__c FROM Campaign WHERE Id = :camp.Id];
 
  system.assertEquals(1,campaigns.get(0).Sum_of_unique_SQAs__c);
 
    Test.stopTest();
    //End: Test Data Creation
 
 
   }
}
 
Hi - I am very new to dev ( like 1 week :) )... i need to try and write a trigger calculating the unique number of campaign member companies attached to a campaign. For example a campaign can have 50 members representing 10 companies. I need that 10. Would anyone have a similar trigger they can share which I will try and edit ? many many thanks 
Hi - I am very new to dev ( like 1 week :) )... i need to try and write a trigger calculating the unique number of campaign member companies attached to a campaign. For example a campaign can have 50 members representing 10 companies. I need that 10. Would anyone have a similar trigger they can share which I will try and edit ? many many thanks