I have the following CaseTeam Trigger and CaseTeam Trigger Test Class... it runs but it is only providing 63% code coverage... can you please show me how I can increase my code coverage for this Trigger? Thank you in advance... I look forward to adding to your reputation!


//My Trigger is as follows  
trigger AffectedSalesForceUserCaseTeamUpDate on Case (before update) 
    List<CaseTeamMember> ctm = new List<CaseTeamMember>();
    List<CaseTeamMember> ctmdelete = new List<CaseTeamMember>();
    List<CaseTeamRole>ctrole = [SELECT Name, Id FROM CaseTeamRole];
    Map<String,String>caseteamr = new Map<String,String>{};
        for(CaseTeamRole ct:ctrole){
            caseteamr.put(ct.Name, ct.Id);}

        // For each case
    for(case c: trigger.new){
        // Get the id of the old case
        Case oldCase = Trigger.oldMap.get(c.Id);
        //Delete old case team member from case team
           oldCase.Affected_Salesforce_User__c == null ||
           c.Affected_Salesforce_User__c != oldCase.Affected_Salesforce_User__c){
            for(CaseTeamMember ctmrem : [SELECT Id FROM CaseTeamMember WHERE MemberID =: c.Affected_Salesforce_User__c OR MemberID =: oldCase.Affected_Salesforce_User__c]){

        // If the old case team member doesn't equal the new case team member
        if(oldCase.Affected_Salesforce_User__c != c.Affected_Salesforce_User__c && c.Affected_Salesforce_User__c != null){
            // Create a list of the members in the case team
            // For each of the case team members in ctmlist
            CaseTeamMember ctmadd = new CaseTeamMember();
            ctmadd.ParentId = c.id;
            ctmadd.MemberId = c.Affected_Salesforce_User__c;
            ctmadd.TeamRoleId = caseteamr.get('Affected User');
            // Add the case team member to the list ctm
    delete ctmdelete;
    if(ctm.isEmpty() == false){
        insert ctm;}}


//My TestCode is as follows  
    public class AffectedSalesForceUserCaseTeamUpDateTest
    static testMethod void AffectedSalesForceUserCaseTeamUpDate()

CaseTeamMember[] newmembers = new CaseTeamMember[]{};
integer  i;
 Case c= new Case(Affected_Salesforce_User__c = '00550000004fTqi');
 insert c;
 case u1=[select id,ownerid from case where id=:c.id];
 id uid=u1.ownerid;

 CaseTeamMember ctm= new CaseTeamMember();
 ctm.ParentId = c.id;
 ctm.MemberId = c.Affected_Salesforce_User__c;
 ctm.TeamRoleId = ctm.get('Affected User');
 insert ctm;
 update c;

//This asserts that prior to adding the CaseTeamMember, there are only the correct caseteammembers in the system
 list<CaseTeamMember> ctms = [Select Id, MemberId From CaseTeamMember];
 system.assertEquals(967, ctms.size());

//Shows that after your trigger fires, the correct team members are in the system
 ctms = [Select Id, MemberId From CaseTeamMember];
 system.assertEquals(967, ctms.size());

  • What lines are getting coverage?
    – EricSSH
    Commented Feb 3, 2016 at 22:10
  • Line 12, 13, 14, 15, 16, 17, 20, 22, 24, 25, 26, 32, 43, and 44 are ALL getting Coverage... 27, 28, 35, 36, 37, 38, 40, 45 are NOT getting Coverage Commented Feb 3, 2016 at 22:19
  • This testmethod looks scary -- you are asserting that 967 caseteamMembers exist because you are using seeAllData=true. This testmethod is likely to break very soon. You should be mocking case, User, and caseTeamMembers
    – cropredy
    Commented Feb 3, 2016 at 22:54
  • @crop1645, could you possibly please provide an example of how I could do this... I am still learning how to do this whole coding thing? Commented Feb 3, 2016 at 23:03
  • you are actually pretty close -- remove seeAllData=true, mock a User and then use that User's id as the value for affected_salesforce_user__c, then your asserts will be looking for the absence of the mocked CTM and the presence of the new CTM
    – cropredy
    Commented Feb 3, 2016 at 23:09

1 Answer 1


For those of yall who see this in the future... Nathan Pepper of the Los Angeles SalesForce Developer Group, @MayTheSForceBeWithYou, was able to assist me in creating the following... thank you N8!

private class AffectedSalesforceUserCaseTeamTest {
private static void testPopulateAffectedSalesforceUser() {
private static void testClearAffectedSalesforceUser() {
private static void testSwitchAffectedSalesforceUser() {
    Case c = new Case(Status = 'New', Type = 'Question', Affected_Salesforce_User__c = '00550000004PBGc', Application__c = 'Salesforce', Priority = 'P3');
    insert c;
    c.Affected_Salesforce_User__c = UserInfo.getUserId();
    update c;

    User otherUser = [SELECT Id FROM User WHERE IsActive = true AND Id != :UserInfo.getUserId() LIMIT 1][0];
    c.Affected_Salesforce_User__c = otherUser.Id;


    update c;


    List<CaseTeamMember> oldUserCtms = [SELECT Id, MemberId, ParentId FROM CaseTeamMember WHERE MemberId = :UserInfo.getUserId() AND ParentId = :c.Id],
                        newUserCtms = [SELECT Id, MemberId, ParentId FROM CaseTeamMember WHERE MemberId = :otherUser.Id AND ParentId = :c.Id];

    System.assertEquals(0, oldUserCtms.size(), 'Old user CaseTeamMembers should be deleted.');
    System.assertEquals(1, newUserCtms.size(), 'New user should have 1 CaseTeamMember.');

You must log in to answer this question.

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