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 ='';
                update upteamlist;

        //Finish Method    
        global void finish(Database.BatchableContext BC){

  • Batch class query should be fetching all Opportunities matching some criteria. You might consider externalizing the where condition to a custom object/setting - don't use hard-coded IDs
    – cropredy
    Jul 15, 2016 at 1:39

1 Answer 1



You need to escape quotes in a query or use merge syntax.

You have:

Public String query = 'SELECT ... From Opportunity where id = '0061C000011S0ZAVQ0' ';

To compile you must escape single quotes:

public String query = 'SELECT ... From Opportunity where id = \'0061C000011S0ZAVQ0\'';

You also need to escape single quotes in your subquery on team members.

You should avoid hard-coded ids at all costs and find a different way to get the data you need. Also if you only want to process one record, a batch is overkill.


You need to cast the scope before you can loop through it like you want, but you have to actually use the type of sObject the query returns.

List<OpportunityTeamMember> allTeamMembers = new List<OpportunityTeamMember>();
for (Opportunity record : (List<Opportunity>)scope)
    for (OpportunityTeamMember teamMember : record.OpportunityTeamMembers)
        if (/*condition*/)
            teamMember.TeamMemberRole = null;
update allTeamMembers;
  • Hi @Adrian Larson, Yes, correct whatever you mentioned above.But testing purpose we are given the opportunity id's,whenever we are trying to execute in anonymous class we are getting error like "Loop variable must be an SObject or list of Opportunity".In anonymous class we are using same query.Can you please help us that error. Thanks
    – Devi
    Jul 14, 2016 at 12:53
  • Okay, if you include the error message in your original post it really makes it way easier to help you. Otherwise you're just making us guess...
    – Adrian Larson
    Jul 14, 2016 at 12:55
  • Please check my Edit bit of code which you are provided in the For loop.But we are getting errors "Invalid field TeamMemberRole for SObject Opportunity".Please correct me entire code based on My requirement we are full to confusing to implement the logic.That would be great help for us. Thanks
    – Devi
    Jul 14, 2016 at 13:59
  • 1
    You need to change the code in the loop to use teamMember, not record... Anywhere you had ot use teamMember. @Devi
    – Adrian Larson
    Jul 14, 2016 at 15:00
  • @ Adrian Larson We have tried same code but not removing the Roles from Opty teammebers don't know where we made the mistakes
    – Devi
    Jul 14, 2016 at 16:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .