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
Abhishek Singh 88Abhishek Singh 88 

Stucked in test class coverage .

Hi All,
I have written a small trigger on opportunity.Which is updating field 'opportunity sales Leader' with the user in opportunity team member.Here is trigger.
trigger UpdateOpportunity on Opportunity (after insert) 
{
    Set<Id> setOfOppty = new Set<Id>();
    List<Opportunity> listOfOpptyToUpdate = new List<Opportunity>();
    for(Opportunity opp : Trigger.New){
        if(opp != null && (opp.RecordTypeId == Label.Oppty_Record_Type_ISV || opp.RecordTypeId == Label.Oppty_Record_Type_OEM || opp.RecordTypeId == Label.Oppty_Record_Type_Wholesale || opp.RecordTypeId == Label.Oppty_Record_Type_xSP)){
            setOfOppty.add(opp.Id);
        }
    }
    List<OpportunityTeamMember> listOTM = new List<OpportunityTeamMember>([SELECT Id, UserId, OpportunityId, User.Name FROM OpportunityTeamMember WHERE OpportunityId in :setOfOppty AND TeamMemberRole = :Label.Opportunity_Sales_Leader]);
    for(OpportunityTeamMember otmObj: listOTM){
        
        if(otmObj != null && otmObj.UserId!=null && otmObj.OpportunityId != null){
            Opportunity opp = new Opportunity(Id = otmObj.OpportunityId, Opportunity_Team_Member_Sales_Leader__c = otmObj.UserId);
            listOfOpptyToUpdate.add(opp);
        }
    }   
    if(!listOfOpptyToUpdate.IsEmpty()){
        Database.SaveResult[] saveResultList = Database.update(listOfOpptyToUpdate,false);
        for(Database.SaveResult svResult : saveResultList){
            if(!svResult.isSuccess()){                
                system.debug('Error::'+svResult);
                break;
            }
        }
    }
}

And Here Is Test clas.;
@Istest(SeeAllData=false)
Private class Test_opportunity 
{
     public static testmethod void testOpportunity()
     {   
         //ProId = [SELECT Id FROM Profile WHERE Name = 'Consumer Sales Ops'].Id;
         User user = new User();
        
        user.FirstName = 'Test';
        user.LastName = 'Name';
        user.CompanyName = 'IT Test Company';
        user.MobilePhone = '123-456-7890';
        
        user.Username = 'testUser-' + '@test.com';
        user.Email = 'testUser-' +'@test.com';
  user.Alias = 'test';
        user.CommunityNickname = 'test1';
        user.TimeZoneSidKey = 'America/New_York';
        user.LocaleSidKey = 'en_US';
        user.EmailEncodingKey = 'UTF-8';
        user.ProfileId = '00e1I000000Veel';
        user.LanguageLocaleKey = 'en_US';
        
        user.Street = '123 Test St';
        user.City = 'Testcity';
        user.State = 'va';
        user.PostalCode = '23223';
        user.Country = 'USA';
        
        insert user;
         Date myDate = Date.today();
         Opportunity opp=new opportunity();
         opp.RecordTypeId='0122F0000004ot9';
         opp.name='test opp';
         opp.AccountId='0012F00000EXcyQ';
         opp.CurrencyIsoCode='AED';
         opp.CloseDate= myDate;
         opp.StageName='Identification';
         opp.Opportunity_Team_Member_Sales_Leader__c=user.Id;
         insert opp;
         OpportunityTeamMember opptyteam=new OpportunityTeamMember();
         opptyteam.opportunityId=opp.Id;
         opptyteam.TeamMemberRole='Sales Leader';
         opptyteam.UserId=user.Id;
         insert opptyteam;
         opp.Opportunity_Team_Member_Sales_Leader__c = opptyteam.UserId;
         update opp;
         
     }
}

But its covering only 53 %
if(otmObj != null && otmObj.UserId!=null && otmObj.OpportunityId != null){
            Opportunity opp = new Opportunity(Id = otmObj.OpportunityId, Opportunity_Team_Member_Sales_Leader__c = otmObj.UserId);
            listOfOpptyToUpdate.add(opp);
        }

this if condition and also error statment is not been covered.any suggestion on this would be really help full
Best Answer chosen by Abhishek Singh 88
Amit Chaudhary 8Amit Chaudhary 8
Your trigger should be like below

trigger UpdateOpportunity on Opportunity (after insert ,after update )

As on insert you will not get OpportunityTeamMember.
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Your trigger should be like below

trigger UpdateOpportunity on Opportunity (after insert ,after update )

As on insert you will not get OpportunityTeamMember.
 
This was selected as the best answer
Abhishek Singh 88Abhishek Singh 88
Thanks Amit.
Parag Bhatt 10Parag Bhatt 10
Hi Abhishek,
I think you must run opportunity and opportunity team member as RUNAS from user. i have tried to solve your problem in attested code below :)
Have a Look
 and complete the details of Profile name and opportunity Record type Name in code below and run it !!!
@Istest(SeeAllData=false)
Private class Test_opportunity 
{
     private static testmethod void testOpportunity()
     { 
Profile testProfile = [SELECT Id FROM profile WHERE Name = 'Give The Name You Need In Profile' LIMIT 1];

        User thisUser = new User(LastName = 'test user 1', 
                           Username = 'test.user.1@example.com', 
                           Email = 'test.1@example.com', 
                           Alias = 'testu1', 
                           TimeZoneSidKey = 'GMT', 
                           LocaleSidKey = 'en_GB', 
                           EmailEncodingKey = 'ISO-8859-1', 
                           ProfileId = testProfile.Id, 
                           LanguageLocaleKey = 'en_US'); 
		
		User newUser;
        Contact c;
		System.runAs ( thisUser ) {
		
		Id opprt = Schema.SObjectType.opportunity.getRecordTypeInfosByName().get('REcordType Name of Opportunity').getRecordTypeId();
		
		Account acc = new Account();
		acc.name = 'Test';
		insert acc;
		
		Contact con = new Contact();
		con.LastName = 'TestCon';
		con.AccountId = acc.id;
		insert con;
		
		Opportunity opp=new opportunity();
         opp.RecordTypeId=opprt;
         opp.name='test opp';
         opp.AccountId=acc.id;
         opp.CurrencyIsoCode='AED';
         opp.CloseDate= myDate;
         opp.StageName='Identification';
         opp.Opportunity_Team_Member_Sales_Leader__c=thisUser.Id;
         insert opp;
		 
		 OpportunityTeamMember opptyteam=new OpportunityTeamMember();
         opptyteam.opportunityId=opp.Id;
         opptyteam.TeamMemberRole='Sales Leader';
         opptyteam.UserId=thisUser.Id;
         insert opptyteam;
         opp.Opportunity_Team_Member_Sales_Leader__c = opptyteam.UserId;
         update opp;
		}
		
	 }
}
It will work as expected :)

Please let us know if this will help you.

Thanks,
Parag Bhatt