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
Malik Butler 5Malik Butler 5 

Issues with ParentRecordType in apex class

I have an issue in the debug section it's saying that the ParentRecordType is an unexpected token for service appointments. 
global class ServiceAppointmentCancel implements Database.Batchable<SObject>, Schedulable
{ 
    global Database.QueryLocator start(Database.BatchableContext BC) 
    { 
        String subject = 'Reload,Pre-Trip report,Post-Trip report'; 
        String status = 'O'; 
        Date SchedEndTime = Date.today();
        String ParentRecordType = 'Work Order';
        String ID = 'WorkOrder.ID';
        String query = 'SELECT Subject,Status,SchedEndTime FROM ServiceAppointment WHERE subject = :subject AND status = :status AND SchedEndTime < :SchedEndTime' + 'SELECT ParentRecordType, FROM ServiceAppointment WHERE ParentRecordType = :ParentRecordType AND ID = ID'; 
        return Database.getQueryLocator(query); 
    } 

    global void execute(Database.BatchableContext BC, List<WorkOrder> serviceAppointments) 
    { 
        // Loop to iterate over the service appointments 
        for(WorkOrder service : serviceAppointments)
        
        { 
            service.status = 'C';
        } 
        for(WorkOrder work : serviceAppointments)
        {
            work.status = 'C';
        }
        // Preforming the DML operation 
        update serviceAppointments;
    } 

    global void finish(Database.BatchableContext BC) 
    { } 

    global void execute(System.SchedulableContext SC) 
    { 
        Database.executeBatch(new ServiceAppointmentCancel(), 200); 
    } 
}
Best Answer chosen by Malik Butler 5
Dhanya NDhanya N
Hi Malik,

In this query, no need to concat 2 soql.
'SELECT Subject,Status,SchedEndTime FROM ServiceAppointment WHERE subject = :subject AND status = :status AND SchedEndTime < :SchedEndTime' + 'SELECT ParentRecordType, FROM ServiceAppointment WHERE ParentRecordType = :ParentRecordType AND ID = ID';

Instead add all the conditions in same soql:
'SELECT Subject,Status, SchedEndTime, ParentRecordType FROM ServiceAppointment WHERE subject = :subject AND status = :status AND SchedEndTime < :SchedEndTime AND ParentRecordType = :ParentRecordType AND ID = ID';


Thanks,
Dhanya 

All Answers

Dhanya NDhanya N
Hi Malik,

In this query, no need to concat 2 soql.
'SELECT Subject,Status,SchedEndTime FROM ServiceAppointment WHERE subject = :subject AND status = :status AND SchedEndTime < :SchedEndTime' + 'SELECT ParentRecordType, FROM ServiceAppointment WHERE ParentRecordType = :ParentRecordType AND ID = ID';

Instead add all the conditions in same soql:
'SELECT Subject,Status, SchedEndTime, ParentRecordType FROM ServiceAppointment WHERE subject = :subject AND status = :status AND SchedEndTime < :SchedEndTime AND ParentRecordType = :ParentRecordType AND ID = ID';


Thanks,
Dhanya 
This was selected as the best answer
Malik Butler 5Malik Butler 5
This worked! Thank you so much for your help!