+ Start a Discussion
SolidLucasSolidLucas 

test class

Someone could help me to create a class test for my trigger? i'm kinda newbie on salesforce :p

.here is my trigger.
trigger MembroOportunidade_aiu on OpportunityTeamMember (after insert, after update) {
	
	for(OpportunityTeamMember om : Trigger.New){
		// verificar se papel do membro do time é igual a representante de vendas 	
		if(om.TeamMemberRole == 'Representante de Vendas'){
			   Opportunity opp = [Select o.Vendedor__c, o.Id From Opportunity o 
			   					  where Id = :om.OpportunityId];
			   opp.Vendedor__c = om.UserId;
		// se papel correto, alterar vendedor da oportunidade		
		 update opp;		   
		}
	}		
}

 
Anoop yadavAnoop yadav
Hi,

Try Like below.
@isTest(SeeAllData = true)
public class TestTriggerClass {
	
	public static testMethod Void testMethod1(){
		
		Opportunity oppn = new Opportunity();
		oppn.Name = 'Test oppn';
		oppn.StageName = 'Stage'; // Put your Stage Name
		oppn.CloseDate = System.Today();
		insert oppn;
		
		OpportunityTeamMember member = new OpportunityTeamMember();  
		member.OpportunityId = oppn.Id;  
		member.UserId = userInfo.getUserId();  
		member.TeamMemberRole = 'Representante de Vendas';
		insert member;
		update member;
	}
}

 
Vatsal KothariVatsal Kothari
Hi,
You can refer below code:
@istest
public class MembroOportunidade_aiuTest{

	public static testmethod void testOpportunityTeamMember()
	{
		Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
		
		User u = new User(Alias = 'standt', Email='standarduser123@testorg.com', 
		EmailEncodingKey='UTF-8', LastName='Testing123', LanguageLocaleKey='en_US', 
		LocaleSidKey='en_US', ProfileId = p.Id, 
		TimeZoneSidKey='America/Los_Angeles', UserName='standarduser123@testorg123.com');
		
		Account acc = new Account();
		acc.Name = 'test account';
		insert acc;
		
		Opportunity opp = new Opportunity();
		opp.Name = 'test Opportunity';
		opp.AccountId = acc.Id;		
		Opp.StageName = 'Closed Won';
		Opp.CloseDate = System.Today();
		insert opp;
		
		OpportunityTeamMember otm = new OpportunityTeamMember();
        otm.UserId = u.Id;
        otm.TeamMemberRole = 'Account Manager';
        otm.OpportunityId = opp.ID;
		insert otm;
		
		otm.TeamMemberRole = 'Representante de Vendas';
		update otm;
	}
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal