We have an Opportunity name called as "Test Opty" and Opportunity owner name as some "ABCD". In that opportunity we have two related lists called as "Opportunity Team" and "Open activities". In the Opportunity Team related list,Team members name is showing like as "XYZ" and specific Role name is mentioned as "AssignLeader" . But here in the Open activities related list there is no activities with the name "XYZ",so at that time we need to remove the team role name (Assign Leader) from that opportunity based on last modified date less than 30 days.
For that we have created a batch as mentioned below. But in the query part is not working. Find the Below Apex class and let us know where we are made the mistakes please correct us .
Global class UpdateTeams implements Database.Batchable<Sobject>{
String Team_Role = 'AssignLeader';
Public String query = 'SELECT id,name,Opportunity.RecordType.name,(SELECT Id,Ownerid,ActivityDate FROM OpenActivities Order By LastModifiedDate ),(SELECT LastModifiedDate,TeamMemberRole FROM OpportunityTeamMembers WHERE TeamMemberRole = 'Team_Role')) From Opportunity where id = '0061C000011S0ZAVQ0' ';
//Start Method
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
//Excute Method
global void execute(Database.BatchableContext BC,List<OpportunityTeamMember> scope){
//List<OpportunityTeamMember> upteamlist=new List<OpportunityTeamMember>();
List<OpportunityTeamMember> upteamlist=new List<OpportunityTeamMember>();
//Set<ID> oppid=new Set<ID>();
for(Opportunity record :(List<Opportunity>)scope){
for(OpportunityTeamMember teamMember : record.OpportunityTeamMembers){
DateTime dT = record.LastModifiedDate;
Date myDate = date.newinstance(dT.year(),dT.month(),dT.day());
Integer numberDaysDue = (system.today().daysBetween(myDate))*(-1);
System.debug('numberDaysDue :'+ numberDaysDue);
if(numberDaysDue > Integer.valueOf(System.Label.OppTeamDays)){
ot.TeamMemberRole ='';
}
}
if(upteamlist.size()>0){
update upteamlist;
}
}
//Finish Method
global void finish(Database.BatchableContext BC){
}
}