You need to sign in to do that
Don't have an account?
Dorel Nasso 9
Help with Account Team Member Trigger Test Class
Hi, I need help with a test class for a trigger. The trigger works but I can not get the test class to pass. I need to pull the User ID from the Account Team with the role of Clinical Account Manger.
Here is the Trigger.
trigger CAM on Case (before update, before insert)
{
for (Case myObj : Trigger.new){
try{
Id samID = [select UserId from AccountTeamMember where AccountId = : myObj.Account_ID__c and TeamMemberRole = 'Clinical Account Manager' limit 1].UserId;
//string userName = [select Name from User where Id = :samID].Name;
Id userName = [select Id from User where Id = :samID].Id;
myObj.Clinical_Account_Manager__c = userName;
}
catch(QueryException qEx){
//Do something if no AccountTeamMember; We chose to send an email to the Account Managers to notify them of the missing data
}
}
}
The Test Class is only getting 66% coverage, Lines 8 and 9 of the trigger are not covered. But I am not how to cover it in the test class.
@isTest
private class CAMTest
{
static testMethod void TestCase()
{
Case a = new Case();
a.AccountId = '0010a00001ELjI2';
a.Clinical_Account_Manager__c = 'test';
a.Clinical_Account_Manager__c = 'test';
update a;
a.AccountId = 'Test';
a.Clinical_Account_Manager__c = 'test';
a.Clinical_Account_Manager__c = 'test';
insert a;
}
}
Thank you for anyone suggestions.
Here is the Trigger.
trigger CAM on Case (before update, before insert)
{
for (Case myObj : Trigger.new){
try{
Id samID = [select UserId from AccountTeamMember where AccountId = : myObj.Account_ID__c and TeamMemberRole = 'Clinical Account Manager' limit 1].UserId;
//string userName = [select Name from User where Id = :samID].Name;
Id userName = [select Id from User where Id = :samID].Id;
myObj.Clinical_Account_Manager__c = userName;
}
catch(QueryException qEx){
//Do something if no AccountTeamMember; We chose to send an email to the Account Managers to notify them of the missing data
}
}
}
The Test Class is only getting 66% coverage, Lines 8 and 9 of the trigger are not covered. But I am not how to cover it in the test class.
@isTest
private class CAMTest
{
static testMethod void TestCase()
{
Case a = new Case();
a.AccountId = '0010a00001ELjI2';
a.Clinical_Account_Manager__c = 'test';
a.Clinical_Account_Manager__c = 'test';
update a;
a.AccountId = 'Test';
a.Clinical_Account_Manager__c = 'test';
a.Clinical_Account_Manager__c = 'test';
insert a;
}
}
Thank you for anyone suggestions.
hello Dorel Nasso,
I don't know whether you are familiar or not that trigger needs only >1% code coverage for deployment. how much coverage do you want?.
You need to create test data first before inserting the case. The test classes in Salesforce does not have access to existing records so you cannot pass the hardcoded Id's like how you have provided. In order to cover your required lines of code, you will need to follow below steps:-
- Create a user record
- Create an account record
- Add user to to the account team as account team member with role of 'Clinical Account Manager'.
- create a case with the created account id
- Do the required Assertions.
Sample code below:- Note:- I have not added assertions. Please write test cases for all required scenarios with the required assertions as the motive of writing test classes is not just to have code coverage but to fully test your code for all possible scenarios (positive/negative/regression).Hope this helps. Please mark this as the correct answer if it suits you.
Regards,
Maulik Desai