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
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');
                    return;    
                    }
                listOpp.add(newOpp);
                oppIds.add(newOpp.AccountId);               
                    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.AccountAccessLevel='Read';
                     //newAccShare.OpportunityAccessLevel = 'Read Only';
                     //newAccShare.CaseAccessLevel='Read Only';
                    //newAccShare.AccountId = newOpp.AccountId;
                    //newAccShare.UserOrGroupId = newOpp.OwnerId;                    
                    listAccTeamMember.add(newAccMember);
                    system.debug('team' + listAccTeamMember);
                    //listShare.add(newAccShare);        
                        if(listAccTeamMember != NULL) {
                        insert listAccTeammember;                        
                        }
           }
        }       
    }
}

My test Class is as follows:-

@isTest
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;
        allIds.add(createOpp.AccountId);
        
            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;
        }    
       //listAcc.add(newAcc);
       newOpp.add(createOpp);
       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.

Thanks
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