+ Start a Discussion
Dorel Nasso 9Dorel 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){
            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.
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.  
Rounak SharmaRounak Sharma

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?.

Maulik Desai 5Maulik Desai 5
Hi Dorel Nasso,

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:-
  1. Create a user record
  2. Create an account record
  3. Add user to to the account team as account team member with role of 'Clinical Account Manager'.
  4. create a case with the created account id
  5. Do the required Assertions.
Sample code below:-
//first create a user
UserRole r = new UserRole(DeveloperName = 'MyCustomRole', Name = 'My Role');
insert r;
User u = new User(
     ProfileId = [SELECT Id FROM Profile WHERE Name = 'YOUR PROFILE'].Id,
     LastName = 'last',
     Email = 'testemail@test.com',
     Username = 'testemail@test.com' + System.currentTimeMillis(),
     CompanyName = 'TEST',
     Title = 'title',
     Alias = 'alias',
     TimeZoneSidKey = 'America/Los_Angeles',
     EmailEncodingKey = 'UTF-8',
     LanguageLocaleKey = 'en_US',
     LocaleSidKey = 'en_US',
     UserRoleId = r.Id
insert u;

//create an account
Account act = new Account(
	name = 'Test account';
insert act;

//add user to the account team as accountteammember with role of 'Clinical Account Manager'
String teamMemberRole = 'Clinical Account Manager';
List<AccountTeamMember> members = new list<AccountTeamMember>();
members.add(New AccountTeamMember(AccountId = act.id, TeamMemberRole = teamMemberRole, UserId= u.Id));
insert members;

//create a case with the created account id
 Case cs = new Case();
 cs.AccountId = act.Id;
 insert cs;
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.

Maulik Desai