+ Start a Discussion
SriniSrini 

Need to write a test class

Hi Everyone,
Can any one please help us writting a test class for below batch apex class.That would be great help.

Global class BatchApex_UpdateOpp implements Database.Batchable<Sobject>{
    String TeamRole = 'Assign';
    Public String query = 'SELECT id,name,Opportunity.RecordType.name,(SELECT ID,Subject,LastModifiedDate FROM ActivityHistories order by LastModifiedDate DESC LIMIT 1 ),(SELECT id,LastModifiedDate,TeamMemberRole FROM OpportunityTeamMembers) From Opportunity ;                  
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }   
        //Execute Method 

        global void execute(Database.BatchableContext BC,List<Opportunity> scope){
        List<OpenActivity> alist=New List<OpenActivity>();
                
        List<OpportunityTeamMember> plist=New List<OpportunityTeamMember>();
        for(Opportunity ot : scope){
          
        for(ActivityHistory OPA : ot.getSObjects('ActivityHistories')){
            DateTime dT = OPA.LastModifiedDate;  
          //DateTime dT = ot.ActivityDate.LastModifiedDate;          
            Date myDate = date.newinstance(dT.year(),dT.month(),dT.day());            
            Integer numberDaysDue = (myDate.daysBetween(system.today()));
          
                if(OPA.Subject !=null){  
                   if(numberDaysDue > Integer.valueOf(System.Label.GE_OppTeamDays)){
                        for(OpportunityTeamMember  optm: ot.getSObjects('OpportunityTeamMembers')){
                            if(optm.TeamMemberRole=='Assign'){          
            plist.add(optm); 
        
            }
        }
    }
    }else{
            for(OpportunityTeamMember  optm: ot.getSObjects('OpportunityTeamMembers')){
                if(optm.TeamMemberRole=='Assign'){
                 
                    plist.add(optm); 
          
               }
            }
        }
 } 
}
        if(Plist.size()>0){
        delete plist;
       }
   }
      //Finish Method
    global void finish(Database.BatchableContext BC){       
    }   
 }    

Thanks in Advance
Sfdc CloudSfdc Cloud
Hi Srini,

First create test data for Opportunity,OpportunityTeam Member and OpportunityHistory object then called below code
BatchApex_UpdateOpp Obj= new BatchApex_UpdateOpp();
                Database.executeBatch(Obj,200);


Thanks,
Nitin