+ Start a Discussion
cambart14cambart14 

OpportunityTeam Test Class

We want to be able to send a simple email notificaiton when ever a member is added to an Opportunity Team, that gives a link.  Below is the working trigger we have in the sandbox, but I need help writing a APEX Test Class that will cover atleast 75% of the code before I can deploy it.  The trigger works perfectly, but any help on how to write a test class would be appreciated.

 

trigger EmailTeamMember on OpportunityTeamMember (after insert) {

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

    for (OpportunityTeamMember o : trigger.new) { 
    String[] showID = new String[] {o.UserID};
    List<User> u = [SELECT Id, Name, Email, BID_Billing_Burden__c FROM User WHERE Id in :showID];
    String[] toAddresses = new String[] {u[0].email};
    String[] oppID = new String[] {o.OpportunityID};
    
    if (o.User_Office__c=='LOG')
    {
    mail.setToAddresses(toAddresses);
    mail.setReplyTo ('noreply@salesforce.com');
    mail.setSenderDisplayname('Salesforce Support');
    mail.setSubject('Added to an Opportunity Team ');
    mail.setUseSignature(false);
    mail.setPlainTextBody('You have been added to an Opportunity Team ');
    mail.setHtmlBody('You have been added to an Opportunity Team:<b><p><p/> '+ 
     'To view that opportunity <a href=https://na3.salesforce.com/'+o.OpportunityID+'>click here.</a>');
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    }
    }
}

 Thanks again!

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Hi Cambart,

 

try below :-

 

@IsTest(SeeAllData=true)
publci class testEmailTeamMember {

static testmethod void MyUnitTest(){

User u = [Select Id, Name, Email from User where name='Vinit Kumar']; // Vinit Kumar is a user in my org
Opportunity opp = new Opportunity(Name='ABC',CloseDate=system.today(),StageName='Prospecting');
insert opp;

OpportunityTeamMember opm = new OpportunityTeamMember(User_Office__c = LOG,OpportunityId=opp.id,UserId=u.id);
insert opm;

}

}