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
Belen Ares Paredes 13Belen Ares Paredes 13 

Help with test code -

Hi!
I am an admin, not a developer and I am trying to overcome the fact that I cannot access the Name of a team member declaratively. 

I found a trigger that does what I need but I am unable to write the test code for the deployment to pass. Can someone help, please??
So frustrating!

Thanks,

Belen
trigger TRG_OpptyTeamMemberName on OpportunityTeamMember (before insert, before update)
{
	Set<Id> setTeamMemberID = new Set<Id>();
	
	for (OpportunityTeamMember otm: Trigger.new)
	{
		setTeamMemberID.add(otm.UserId);
	}

	Map<Id, User> usrMap = new Map<Id, User>([SELECT Id, Name, LastName FROM User WHERE Id IN :setTeamMemberId]);
	
	for (OpportunityTeamMember otm: Trigger.new)
	{
		User usr = usrMap.get(otm.UserId);

		otm.Team_Member_Name__c = usr.Name;
	}
}

 
Vanisha_GuptaVanisha_Gupta
Hello,

You can use test class like below:
@isTest public class TRG_OpptyTeamMemberNameTest {

    @isTest static void testing(){
        User usr=[Select Id from User where Name='Vanisha gupta'];
        OpportunityTeamMember otm=new OpportunityTeamMember(OpportunityId='0060K00000bKAt5QAG', UserId=usr.Id);
		insert otm;
        
        OpportunityTeamMember query=[Select Id,Team_Member_Name__c from OpportunityTeamMember where OpportunityId='0060K00000bKAt5QAG'];
        
        System.assertEquals(query.Team_Member_Name__c, 'Vanisha gupta');
    }
}


Here, we are inserting OpportunityTeamMember by passing OpportunityId and UserId
This will cause trigger to start.

Then put an assert on Team_Member_Name__c to check if trigger is working .
It will give 100% code coverage.
Maharajan CMaharajan C
Hi Belen,

Try the below code:

Don't hardcode any thing in the test class.
@isTest
public class TRG_OpptyTeamMemberNameTest {
    static testmethod void testOpptyTeamMemName(){
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
        User u = new User(Alias = 'attach', Email='testattach123@testorg.com', 
                          EmailEncodingKey='UTF-8', LastName='Testingtestuser', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='testattach123@testorg.com');
        
        // Add below if there any other mandatory fields needed to create opportunity
        Opportunity opp = new Opportunity(Name='Test Opp',CloseDate=system.today(),StageName='Prospecting');
        insert opp;
        system.runAs(u)
        {
            OpportunityTeamMember opm = new OpportunityTeamMember(OpportunityId=opp.id,UserId=u.id);
        	insert opm;
            
            OpportunityTeamMember optm=[Select Id,Team_Member_Name__c from OpportunityTeamMember where Id=:opm.Id limit 1];
            system.assertEquals('Testingtestuser', optm.Team_Member_Name__c);
        }
        
    }
}

Thanks,
Maharajan.C