+ Start a Discussion
Vijay Zutshi 2Vijay Zutshi 2 

Test Class Failing with 0% Test Coverage

My Trigger is as follows:- 

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>();
        Profile pld= [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
        User u= new User();
        u.LastName = 'ttt';
        u.ProfileId = Pld.Id;        
        insert u;                   
        
        Account newAcc = new Account();
        newAcc.Name = 'Starting';
        newAcc.OwnerId = u.Id;        
        listAcc.add(newAcc);        
        insert listAcc;        
                
        Opportunity createOpp = new Opportunity();       
        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; 
        }       
       newOpp.add(createOpp);       
       insert newOpp;       
        
        AccountTeamMember createTeam = new AccountTeamMember();
        createTeam.accountId = createOpp.AccountId;
        createTeam.AccountAccessLevel = 'Read';
        createTeam.UserId = createOpp.OwnerId;
        newTeamMember.add(createTeam);        
        insert newTeamMember;       
    }
}

My test class is failing and it is giving me System.DMLException Insert failed

Please provide assistance as I am stuck with for many days and I do not just want to forget about it because by doing that I am not going to learn for the next time.

Thanks
Vijay
Best Answer chosen by Vijay Zutshi 2
veer soniveer soni
hi vijay,
try following apex test class with 90% code coverage.
@isTest
public class oppAccTeamMemberTest {
    static testMethod Void testOppAccTeamMember() {
      
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='dummyStandarduser@testorg.com');
        insert u;

        Account newAcc = new Account();
        newAcc.Name = 'Starting';
        newAcc.OwnerId = u.Id;        
        insert newAcc;        
                
        Opportunity createOpp = new Opportunity();       
        createOpp.Name = 'First';
        createOpp.CloseDate = Date.today();
        createOpp.StageName = 'Prospecting';
        createOpp.AccountId = newAcc.Id;
        createOpp.Probability=20;
          
       insert createOpp;       
       
    }
}

I hope this time it will help you.
let me know if it helps you and marking it as best answer.
Thank you

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Vijay, 

You are inserting many items in the test class and based on the error message, I am unsure which insert is failing. Can you please provide the stack trace/ line number? 

 
Vijay Zutshi 2Vijay Zutshi 2
Hi Anudeep, 
Thanks for your reply. Just to let you know all Insert statements are giving that error in the test class.
Thanks
Vijay
 
Vijay Zutshi 2Vijay Zutshi 2
Hi Anudeep,
Following are inserts that are giving System.DMLException Insert failed
 insert u;  
 insert listAcc; 
 insert newOpp;

 insert newTeamMember;

Thanks
Vijay
Vijay Zutshi 2Vijay Zutshi 2
Hi Anudeep, Thanks for your email. Following are inserts that are giving System.DMLException Insert failed insert u; insert listAcc; insert newOpp; insert newTeamMember; Thanks Vijay
veer soniveer soni
hi vijay,
try following apex test class with 90% code coverage.
@isTest
public class oppAccTeamMemberTest {
    static testMethod Void testOppAccTeamMember() {
      
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='dummyStandarduser@testorg.com');
        insert u;

        Account newAcc = new Account();
        newAcc.Name = 'Starting';
        newAcc.OwnerId = u.Id;        
        insert newAcc;        
                
        Opportunity createOpp = new Opportunity();       
        createOpp.Name = 'First';
        createOpp.CloseDate = Date.today();
        createOpp.StageName = 'Prospecting';
        createOpp.AccountId = newAcc.Id;
        createOpp.Probability=20;
          
       insert createOpp;       
       
    }
}

I hope this time it will help you.
let me know if it helps you and marking it as best answer.
Thank you
This was selected as the best answer
Vijay Zutshi 2Vijay Zutshi 2
Hi Veer,

Thanks for your help. Yes it is working with modifications that you suggested.

Thanks again
Vijay