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 

Best way to execute code

I'm trying to execute an apex class. I keep running this:
String q = 'SELECT Reload,Pre-Trip report,Post-Trip report,ParentRecordType,ID, O, Date.today()';
Database.executeBatch(new ServiceAppointmentCancel(), 200);
But it is not executing what it is supposed to in the code. 
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, ParentRecordType FROM ServiceAppointment WHERE subject = :subject AND status = :status AND SchedEndTime < :SchedEndTime AND 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); 
    } 
}

 
Ajay K DubediAjay K Dubedi
Hi Malik,

I have gone through you code and found some mistakes. Please correct them and try with new codes. When you are going to use string literals, You must be concatenate them by using + sign.You can also check your output by system.debug to be assure at your side that its working fine or not.

Replace all of your Query string with this which is written below. I have tested it on my Org and it is working same as your wish.

        String query = 'SELECT Subject, Status, SchedEndTime, ParentRecordType FROM ServiceAppointment WHERE subject = :' + subject + ' AND status = :'+ status + ' AND SchedEndTime < :'+ SchedEndTime + ' AND ParentRecordType = :'+ ParentRecordType +' AND ID = :'+ID;   
    
    system.debug(Query);
    return Database.getQueryLocator(query);

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Malik Butler 5Malik Butler 5
This doen't seem to be workong when I run the debug through execute anonymous window there there is an issue where it says this
"Line: 1, Column: 117
Variable does not exist: subject"
Ajay K DubediAjay K Dubedi
Hi Malik,

If you want to execute the code from an Anonymous window, you should write the whole code from assignment variables to query string, Otherwise variable not exist an error will occur.
Here is the perfect solution, try this in an anonymous window-
 
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, ParentRecordType FROM ServiceAppointment WHERE subject = :' + subject + ' AND status = :'+ status + ' AND SchedEndTime < :'+ SchedEndTime + ' AND ParentRecordType = :'+ ParentRecordType +' AND ID = :'+ID;   
    system.debug(Query);

Put the whole above code in an Anonymous window and check to debug log to see the outcome and this time, it will definitely work.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com