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
SriniSrini 

Need SOQL Help

Hi Team,

We are facing the error like Unknown parsing query, while we are trying the  query mentioned below,can any one please help us where we made the mistakes.

Select id,name,Opportunity.RecordType.name,(SELECT CreatedById,Id,LastModifiedDate,OwnerId, WhatId FROM Task),(SELECT OpportunityId FROM  OpportunityTeamMember where UserId =' '),
(SELECT Id,IsActive,UseRole.Name FROM User Where IsActive=true and UserRoleId ='' And UseRole.Name like 'Assign-A%')) From Opportunity

Thanks
Martijn SchwarzerMartijn Schwarzer
Hi Srini,

There are several issues with the query:
  1. There is 2 times UseRole.Name used. This should be UserRole.Name
  2. There is an extra ")"-character after 'Assign-A%'. Please remove this.
  3. You cannot query the RecordType.Name directly. You will have to use a separate query for the RecordType record and later match it with the opportunity record you retrieve from the query. (see example code below)
  4. You will need to use the relationship names to query the related records: "From Tasks" in stead of "From Task" and "From OpportunityTeamMembers" in stead of "OpportunityTeamMember".
  5. You cannot query related users to an opportunity, since the User object is not directly linked to the Opportunity object. You will need to create a separate query for this.
So your code will need to look something like this:
//Get the list of users
List<User> users = [SELECT Id,IsActive,UserRole.Name FROM Users Where IsActive=true and UserRoleId ='' And UserRole.Name like 'Assign-A%'];

//Get all active recordtypes for Opportunity object
List<RecordType> recordtypes = [SELECT Id, Name, DeveloperName FROM RecordType WHERE sObjectType = 'Opportunity' AND isActive = 'true'];

//Put recordtypes in Map for easy retrieval later on
Map<Id, RecordType> recordTypeMap = new Map<Id, RecordType>(recordtypes);

//Query the Opportunity records, including related data
List<Opportunity> opps = [Select id, name, RecordTypeId,
                             (SELECT CreatedById,Id,LastModifiedDate,OwnerId, WhatId FROM Tasks),
                             (SELECT OpportunityId FROM OpportunityTeamMembers where UserId =' ') 
                          From Opportunity];

//Map the correct recordtype to opportunity records:
for(Opportunity opp : opps){
    RecordType rt = recordTypeMap.get(opp.RecordTypeId);
    //Now you have access to the RecordType name for this opportunity
}

//I'm not sure how to map the users returned from the query, or why you are querying them. There is no direct relation between User object and Opportunity object.
Hope this helps!

Best regards,
Martijn Schwärzer
SriniSrini
Hi Martijn,

Sorry for the late reply...Thanks for your help.

We have one more issue .who ever having the Team roles "Assign" we need to remove them in the opportunity along with particular Recordtypes.

Record types are : 1.Sale,Desing,Flow.

Can you please help us  that would be great.

Please check my below Apex code: and where we need to include role level.

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 WHERE TeamMemberRole = \'TeamRole\') 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>();
                //set<id> aset=new set<id>();
                // set<id> oset=new set<id>();
        List<OpportunityTeamMember> plist=New List<OpportunityTeamMember>();
        for(Opportunity ot : scope){
            system.debug('@@@@@@OpportunityTeamMembers'+ot.getSObjects('OpportunityTeamMembers'));
        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()));
            system.debug('@@@@@@Subject'+OPA.Subject);
            system.debug('@@@@@@numberDaysDue'+numberDaysDue);
                if(OPA.Subject !=null){  
                   if(numberDaysDue > Integer.valueOf(System.Label.OppTeamDays)){
                        for(OpportunityTeamMember  optm: ot.getSObjects('OpportunityTeamMembers')){
                            if(optm.TeamMemberRole=='Assign'){
            system.debug('@@@@@@TeamMemberRole'+optm.TeamMemberRole);
            plist.add(optm); 
            system.debug('@@@@@@OpportunityTeamMember   '+plist);
            }
        }
    }
    }else{
            for(OpportunityTeamMember  optm: ot.getSObjects('OpportunityTeamMembers')){
                if(optm.TeamMemberRole=='Assign'){
                    system.debug('@@@@@@TeamMemberRole'+optm.TeamMemberRole);
                    plist.add(optm); 
            system.debug('@@@@@@OpportunityTeamMember      '+plist);
               }
            }
        }
 } 
}
        if(Plist.size()>0){
        delete plist;
       }
   }
      //Finish Method
    global void finish(Database.BatchableContext BC){       
    }   
 }

Thanks in Advance