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
Stephanie AkinStephanie Akin 

Can someone help build a test class?

Hi, I have an apex trigger that I pieced together and tested out and it works. But now I need a test class in order to move it to production. Any help is appreciated. 
Here is my trigger:

trigger InsertOppTeam on OpportunityTeamMember (after insert, after update) {
//Using set to be able to work with setOpp and setOTM SETS
set<Id> setOpp = new set<Id>();
set<Id> setOTM = new set<Id>();


for (OpportunityTeamMember oppTeam : trigger.new) {
setOpp.add(oppTeam.OpportunityId);
setOTM.add(oppTeam.id);
}
//Loop through opportunity team members and grab Users who have these roles 'OL Assignment' or 'Sage Buddy' or 'Mentor'.

list<OpportunityTeamMember> listOTM = new list<OpportunityTeamMember>
([SELECT Id, UserId, OpportunityId, TeamMemberRole, User.Name FROM OpportunityTeamMember WHERE Id in :setOTM AND (TeamMemberRole = 'OL Assignment' OR

TeamMemberRole = 'Sage Buddy' OR TeamMemberRole = 'Mentor') ]);


// Create a map that grabs the Opportunity Id being worked with
Map<Id,Opportunity> mapOpps = new map<Id, Opportunity>([SELECT Id FROM Opportunity Where Id = :setOpp ]) ;

Opportunity tempOpp;


//Load the ID's
for(OpportunityTeamMember otm : listOTM ){
tempOpp = mapOpps.get(otm.OpportunityId);

if(otm.TeamMemberRole == 'OL Assignment') {
tempOpp.OL_Assignment1__c = otm.User.Name;
}
else if(otm.TeamMemberRole == 'Mentor') {
tempOpp.Mentor__c = otm.User.Name;
}
else if(otm.TeamMemberRole == 'Sage Buddy') {
tempOpp.Sage_Buddy__c = otm.User.Name;
}

}

// Load values

update mapOpps.values();
}

Thank you!
Best Answer chosen by Stephanie Akin
Arunkumar RArunkumar R
Try the below code, I have not tested this code. If any issue let me know, i will help you..! Put assertion in the below class

@isTest
public class OpportunityTeamMemberTest
{
static testMethod void OpportunityTeamOLAssignment()
{

    Opportunity oppRec=new Opportunity(Name='Test Opportunity 1',StageName='Prospecting',Amount=100000,CloseDate=System.today());
         insert oppRec;
         
         Opportunity oppRec1=[select Id,OwnerId,StageName from Opportunity where Id=:oppRec.Id];
         
         System.assertEquals(oppRec1.OwnerID,UserInfo.getUserId());
         
       
         OpportunityTeamMember opmemRec=new OpportunityTeamMember(OpportunityId=oppRec.id,UserId=UserInfo.getUserId(),TeamMemberRole='OL Assignment');
         insert opmemRec;
         
}

static testMethod void OpportunityTeamSageBuddy()
{

    Opportunity oppRec=new Opportunity(Name='Test Opportunity 2',StageName='Prospecting',Amount=100000,CloseDate=System.today());
         insert oppRec;
         
         Opportunity oppRec1=[select Id,OwnerId,StageName from Opportunity where Id=:oppRec.Id];
         
         System.assertEquals(oppRec1.OwnerID,UserInfo.getUserId());
         
       
         OpportunityTeamMember opmemRec=new OpportunityTeamMember(OpportunityId=oppRec.id,UserId=UserInfo.getUserId(),TeamMemberRole='Sage Buddy');
         insert opmemRec;
         
}

static testMethod void OpportunityTeamMentor()
{

    Opportunity oppRec=new Opportunity(Name='Test Opportunity 3',StageName='Prospecting',Amount=100000,CloseDate=System.today());
         insert oppRec;
         
         Opportunity oppRec1=[select Id,OwnerId,StageName from Opportunity where Id=:oppRec.Id];
         
         System.assertEquals(oppRec1.OwnerID,UserInfo.getUserId());
         
       
         OpportunityTeamMember opmemRec=new OpportunityTeamMember(OpportunityId=oppRec.id,UserId=UserInfo.getUserId(),TeamMemberRole='Mentor');
         insert opmemRec;
         
}

}


All Answers

Vinit_KumarVinit_Kumar
Have you written the Test class , If yes what is the issue you are facing ?

If not I would suggest you to go through the below links to know how you can create test class for Triggers :-

https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

http://teachmesalesforce.wordpress.com/2011/05/07/how-to-write-a-trigger-test/

http://techman97.wordpress.com/2012/07/18/how-to-write-a-basic-apex-trigger/

If this helps,please mark this as best answer to help others :)
Arunkumar RArunkumar R
Try the below code, I have not tested this code. If any issue let me know, i will help you..! Put assertion in the below class

@isTest
public class OpportunityTeamMemberTest
{
static testMethod void OpportunityTeamOLAssignment()
{

    Opportunity oppRec=new Opportunity(Name='Test Opportunity 1',StageName='Prospecting',Amount=100000,CloseDate=System.today());
         insert oppRec;
         
         Opportunity oppRec1=[select Id,OwnerId,StageName from Opportunity where Id=:oppRec.Id];
         
         System.assertEquals(oppRec1.OwnerID,UserInfo.getUserId());
         
       
         OpportunityTeamMember opmemRec=new OpportunityTeamMember(OpportunityId=oppRec.id,UserId=UserInfo.getUserId(),TeamMemberRole='OL Assignment');
         insert opmemRec;
         
}

static testMethod void OpportunityTeamSageBuddy()
{

    Opportunity oppRec=new Opportunity(Name='Test Opportunity 2',StageName='Prospecting',Amount=100000,CloseDate=System.today());
         insert oppRec;
         
         Opportunity oppRec1=[select Id,OwnerId,StageName from Opportunity where Id=:oppRec.Id];
         
         System.assertEquals(oppRec1.OwnerID,UserInfo.getUserId());
         
       
         OpportunityTeamMember opmemRec=new OpportunityTeamMember(OpportunityId=oppRec.id,UserId=UserInfo.getUserId(),TeamMemberRole='Sage Buddy');
         insert opmemRec;
         
}

static testMethod void OpportunityTeamMentor()
{

    Opportunity oppRec=new Opportunity(Name='Test Opportunity 3',StageName='Prospecting',Amount=100000,CloseDate=System.today());
         insert oppRec;
         
         Opportunity oppRec1=[select Id,OwnerId,StageName from Opportunity where Id=:oppRec.Id];
         
         System.assertEquals(oppRec1.OwnerID,UserInfo.getUserId());
         
       
         OpportunityTeamMember opmemRec=new OpportunityTeamMember(OpportunityId=oppRec.id,UserId=UserInfo.getUserId(),TeamMemberRole='Mentor');
         insert opmemRec;
         
}

}


This was selected as the best answer
Ramu_SFDCRamu_SFDC
Follow the guidelines as explained in the below article

https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
Stephanie AkinStephanie Akin
Thank you, Arunkumar! It worked and passed the test! 100% code coverage. Thank you so much!!