You need to sign in to do that
Don't have an account?
Erica 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
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
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. 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
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
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) {}
}
}
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. 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
Thank you so much. It worked really well.
Erica