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
AkshuAkshu 

I want Batch class for below example

Create 3 objects Airplanes, Schedules, and Flights. Airplane object has 2 fields Name of airplane, number of seats. Schedules object has 4 fields, start date, End date, Post Date (formula field Start Date minus 30- days) and lookup to Airplane object. An Airplane can have multiple schedules. Flight object has lookup to schedules as well as Airplane and 2 additional fields as Flight start date and flight end date.

Write a code that will run every day on Schedule and when the Post Date is less than or equal to today, create a record of flight object. 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Akshu,


The developer community recommends posting what you have tried so far and where exactly you are stuck.

Thanks,
 
AkshuAkshu
/*Use above created trailhead org for the below example. Create 3 objects Airplanes, Schedules, and Flights. 
* Airplane object has 2 fields Name of airplane, number of seats.chedules object has 4 fields, 
* start date, End date, Post Date (formula field Start Date minus 30- days) and lookup to Airplane object. 
* An Airplane can have multiple schedules. Flight object has lookup to schedules as well as Airplane and 2 
* additional fields as Flight start date and flight end date. 

* Write a code that will run every day on Schedule and when the Post Date is less than or equal to today,
*  create a record of flight object. 
*/
global class BatchForAirplanesch implements Database.batchable<sObject>{
    global  Database.QueryLocator  start(Database.BatchableContext bc)
    {
        return Database.getQueryLocator([select id,name,FlightStartdate__c,FlightEnddate__c from  Flight__c where ScheduleForFlights__r.PostDate__c <= today]);
    }
    global void execute(Database.BatchableContext bc,List<Flight__c> Flightlist)
        
    {
        list<Flight__c> tasklist=new list<Flight__c>();
            

        for(Flight__c a:Flightlist)
        {
            Flight__c f=new flight__c();
            //if(f.ScheduleForFlights__r.PostDate__c <= system.today())
            {
                Airplane__c ap=new Airplane__c();
                ap.name='air1';
                ap.NumberOf_Seats__c=100;
                insert ap;
            

                schedule__c s=new schedule__c();
                s.name='sch1';
                s.EndDate__c=date.parse('3/4/2023');
                s.StartDate__c=date.parse('3/4/2022');
                s.AirplaneForSchedules__c=ap.id;
                insert s;
                
                
                f.Name='Flight100';
                f.FlightEnddate__c=date.parse('02/03/2022');
                f.FlightStartdate__c=date.parse('02/02/2022');
                f.AirplaneForFlights__c=ap.id;
                f.ScheduleForFlights__c= s.id;
                
             
                tasklist.add(f);}
            
        }
        
        insert tasklist;
      
    }
    global void finish(Database.BatchableContext bc)
    {
        
    } 
}

//Any record not inserted
 
AkshuAkshu
zero record inserted unable to find out root cause;plz tell me whats is my mistake
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Akshu,

I dont see any issue with the code. Can you check if the query is returning any rows by excuting the query in developer console.
 
select id,name,FlightStartdate__c,FlightEnddate__c from  Flight__c where ScheduleForFlights__r.PostDate__c <= today

If that is not returning any record then it is expected that the records wont be created. 

So first create a data which satisfies that condition and run the batch class in anonomous window. It should work and is working for me as expected.

Thanks,