+ Start a Discussion
Vijay Zutshi 2Vijay Zutshi 2 

Test Class Not covering Account Team Member

My trigger is as follows:-

//Trigger to create the Opportunity with the Probability=20, 
//then the opportunity owner will be automatically added to Account Team 
//of the associated account for that Opportunity.
trigger opportunityAcountTeam on Opportunity (after insert, after update) {
    LIST<Opportunity> listOpp = new LIST<Opportunity>();
    SET<ID> oppIds = new SET<ID>();
    LIST<AccountTeamMember> listAccTeamMember = new LIST<AccountTeamMember>();
    //LIST<AccountShare> listShare = new LIST<AccountShare>();
    if(Trigger.IsAfter && Trigger.IsInsert) {
        for(Opportunity newOpp : Trigger.new) {
            if(newOpp.Probability == 20) { 
                    if(String.isEmpty(newOpp.AccountId)) {
                    newOpp.addError('Account name cannot be blank');
                    AccountTeamMember newAccMember = new AccountTeamMember();
                    newAccMember.AccountAccessLevel = 'Edit';
                    newAccMember.OpportunityAccessLevel = 'Read';
                    newAccMember.TeamMemberRole = 'Account Manager';
                    newAccMember.AccountId = newOpp.AccountId;                 
                    newAccMember.UserId = newOpp.OwnerId;
                    newAccMember.Account = newOpp.Account;
                    //AccountShare newAccShare = new AccountShare();
                     //newAccShare.OpportunityAccessLevel = 'Read Only';
                     //newAccShare.CaseAccessLevel='Read Only';
                    //newAccShare.AccountId = newOpp.AccountId;
                    //newAccShare.UserOrGroupId = newOpp.OwnerId;                    
                    system.debug('team' + listAccTeamMember);
                        if(listAccTeamMember != NULL) {
                        insert listAccTeammember;                        

My test Class is as follows:-

public class oppAccTeamMember {
    static testMethod Void testOppAccTeamMember() {
        LIST<Opportunity> newOpp = new LIST<Opportunity>();
        LIST<AccountTeamMember> newTeamMember = new LIST<AccountTeamMember>();
        LIST<Account> listAcc = new LIST<ACCOUNT>();
        SET<ID> allIds = new SET<ID>();
        Opportunity createOpp = new Opportunity();
        AccountTeamMember createTeam = new AccountTeamMember();
        Account newAcc = new Account();
        newAcc.Name = 'Starting';
        insert newAcc;
        createOpp.Name = 'First';
        createOpp.CloseDate = Date.today();
        createOpp.StageName = 'Prospecting';
        createOpp.AccountId = newAcc.Id;
            if(String.isEmpty(createOpp.AccountId)) {
          createOpp.addError('Account name cannot be blank');
        if(!allIds.isEmpty()) {
                       AccountTeamMember testClassAtm = new AccountTeamMember();
                    testClassAtm.AccountAccessLevel = 'Edit';
                    testClassAtm.OpportunityAccessLevel = 'Read';
                    testClassAtm.TeamMemberRole = 'Account Manager';
                    testClassAtm.AccountId = createOpp.AccountId;                 
                    testClassAtm.UserId = createOpp.OwnerId;
                    testClassAtm.Account = createOpp.Account; 
                    //insert newTeamMember;
       insert newOpp;  
       //insert listAcc; 

I am only getting 28% coverage. The test class is not covering the Account Team Member code in the trigger. Kindly help.

Vijay Zutshi
AnudeepAnudeep (Salesforce Developers) 
Hi Vijay,

Can you please highlight the lines of code that are not covered?
Vijay Zutshi 2Vijay Zutshi 2
Hi Anudeep, Thanks for your response. The lines that are not being covered are as follows:- if(String.isEmpty(createOpp.AccountId)) { createOpp.addError('Account name cannot be blank'); return; } if(!allIds.isEmpty()) { AccountTeamMember testClassAtm = new AccountTeamMember(); testClassAtm.AccountAccessLevel = 'Edit'; testClassAtm.OpportunityAccessLevel = 'Read'; testClassAtm.TeamMemberRole = 'Account Manager'; testClassAtm.AccountId = createOpp.AccountId; testClassAtm.UserId = createOpp.OwnerId; testClassAtm.Account = createOpp.Account; newTeamMember.add(testClassAtm); //insert newTeamMember; Please advise. Thanks Vijay Zutshi