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
Erica Nemeth VeitErica Nemeth Veit 

Apex trigger in sandbox to add Case Creator as a Team Member into Cases is working fine but displays an error when bring to production with Change Set. I need help...

I don't know how to write code in Salesforce but after a lot of digging and searching, I found a possible way of adding the Case Creator as a Case Team Member every time a new Case was created. Thus, I copied and paste one in my Sandbox and made the adjustments necessary to make it work for my ORG.
It is working fine in Sandbox, but while trying to deploy it from my sandbox using Change Sets. The trigger is failing with the following error:

"The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage."

As I don't do coding I don't know what I can do in order to be able to move this successfully to my production environment. Any help is really appreciated. 

Thank you,
Erica
Best Answer chosen by Erica Nemeth Veit
Deepali KulshresthaDeepali Kulshrestha
Hi Erica,

Firstly create a 'CaseTeamRole' in your ORG and gave Name = 'Creator' of CaseTeamRole.
And then try the following test class, it covers 82% code coverage of your code in my ORG.
@IsTest
public class AddCaseCreatorasaTeamMember_Test {
    @IsTest
    public static void methodTest1(){
        Id RecordTypeIdCase = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Customer Support').getRecordTypeId();
        Case caseInst=new Case();
        caseInst.Origin='Phone';
        caseInst.Status='New';
        caseInst.recordtypeid=RecordTypeIdCase;
        insert caseInst;
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi Erica,

At least 75% of your Apex code must be covered by unit tests, and all of those tests must complete successfully. Note the following:
1. When deploying to a production organization, every unit test in your organization name-space is executed.
2. Calls to System.debug are not counted as part of Apex code coverage.
3. Test methods and test classes are not counted as part of Apex code coverage.
4. While only 75% of your Apex code must be covered by tests, your focus shouldn't be on the percentage of code that is covered. Instead, you should make sure that every use case of your application is covered, including positive and negative cases, as well as bulk and single records. This should lead to 75% or more of your code being covered by unit tests.

Every trigger must have 75% test coverage to When deploying to a production organization.
Please have a look the below links:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_deploying_ant_deploy.htm

            
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Erica Nemeth VeitErica Nemeth Veit
Hello,
Thank you for your reply. Is there any way that you can help me create the test class for my Apex trigger? Here we go:

trigger AddCaseCreatorasaTeamMember on Case (after insert) {
/**
* Original: Erica Nemeth
* Date:    04/12/2019

* Comments:
* To give visiblity into the Case records that were created by a User, we
* will add them to the CaseTeamMember list when a particular record is
* updated. The user should only be added to the CaseTeamMember table once.
   */
   Map<Id, CaseTeamMember> membersToAdd = new Map<Id, CaseTeamMember>();
    List<Case> cases = [Select Id,OwnerId, CreatedById, RecordTypeId,RecordType.Name
                  from Case where id IN :Trigger.newMap.keySet()];
    Set<id> caseCreatorIDSet = new set<id>();
    for (Case c : cases) {
   if (c.RecordType.Name == 'Customer Support'||
       c.RecordType.Name == 'Salesforce System Support'||
       c.RecordType.Name == 'AR Support') {
    caseCreatorIDSet.add(c.CreatedById);
    membersToAdd.put(c.Id, new CaseTeamMember(            
            ParentId = c.Id,                          
            MemberId = c.CreatedById
          )
        );
      }
    }
    if (!membersToAdd.isEmpty()) {
      try {
        CaseTeamRole caseTeamRole = [SELECT Id FROM CaseTeamRole WHERE Name = 'Creator' LIMIT 1];
        for (CaseTeamMember ctm : membersToAdd.values()) {
          ctm.TeamRoleId = caseTeamRole.Id;
        }
     
    for (CaseTeamMember ctm : [SELECT Id, MemberId, ParentId
                       FROM CaseTeamMember
                       WHERE ParentId IN :membersToAdd.keySet()
                       AND MemberId IN: caseCreatorIDSet
                       ORDER BY ParentId]) {
                        
        System.debug('*******************************************');              
          if (membersToAdd.containsKey(ctm.ParentId)) {
            membersToAdd.remove(ctm.ParentId);
          }
        }
     
    if (!membersToAdd.isEmpty()) {
          insert membersToAdd.values();
        }
         
        } catch (System.QueryException qe) {}
    }
}
 
Deepali KulshresthaDeepali Kulshrestha
Hi Erica,

Firstly create a 'CaseTeamRole' in your ORG and gave Name = 'Creator' of CaseTeamRole.
And then try the following test class, it covers 82% code coverage of your code in my ORG.
@IsTest
public class AddCaseCreatorasaTeamMember_Test {
    @IsTest
    public static void methodTest1(){
        Id RecordTypeIdCase = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Customer Support').getRecordTypeId();
        Case caseInst=new Case();
        caseInst.Origin='Phone';
        caseInst.Status='New';
        caseInst.recordtypeid=RecordTypeIdCase;
        insert caseInst;
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
This was selected as the best answer
Erica Nemeth VeitErica Nemeth Veit
Hi Deepali,

Thank you so much. It worked really well.

Erica