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
sachin joshisachin joshi 

Apex Schedular

Hi,
Is it possible to create a new detail record everyday of master record using Apex Schedular, as I am new to Apex please suggest me how can i do that?
Best Answer chosen by sachin joshi
SIVASASNKARSIVASASNKAR
Hi Sachin,

You can use the follow the below steps. i'm giving you small code for inserting the Account record.
 
  1. global class scheduledBatchable implements Schedulable {
       global void execute(SchedulableContext sc) {
         Account createAcc = new Account(Name='Test');//fill all required fields in it
         Database.SaveResult sr = Database.insert(createAcc, false);
         
        if (sr.isSuccess()) {
            // Operation was successful, so get the ID of the record that was processed
            System.debug('Successfully inserted account. Account ID: ' + sr.getId());
        }
        else {
            // Operation failed, so get all errors                
            for(Database.Error err : sr.getErrors()) {
                System.debug('The following error has occurred.');                    
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('Account fields that affected this error: ' + err.getFields());
            }
     
       }
    }
 
Run this code in developer console:

schedulable sc = new schedulable ();
        String sch = '0 0 9 * * ?';//it will run every day 9 am
        system.schedule('EveryDay', sch, sc);

OR
User-added image


Thanks,
Sivasankar

All Answers

sandeep sankhlasandeep sankhla
Hi,

Yes , you can create a batch which will run daily and then that will find all master object first and then that will create detail record for that master record...

Thanks
siddarth rajsiddarth raj
Hi Sachin,

Yes you can create. Batch is not must to impliment in this scenario.. You could just extend the Apex class to Schedulable and override execute() with the logic to identify details record is there or not for the day...and do accordingly

Finally schedule that to run on business demand.

Thanks
Sid
SIVASASNKARSIVASASNKAR
Hi Sachin,

You can use the follow the below steps. i'm giving you small code for inserting the Account record.
 
  1. global class scheduledBatchable implements Schedulable {
       global void execute(SchedulableContext sc) {
         Account createAcc = new Account(Name='Test');//fill all required fields in it
         Database.SaveResult sr = Database.insert(createAcc, false);
         
        if (sr.isSuccess()) {
            // Operation was successful, so get the ID of the record that was processed
            System.debug('Successfully inserted account. Account ID: ' + sr.getId());
        }
        else {
            // Operation failed, so get all errors                
            for(Database.Error err : sr.getErrors()) {
                System.debug('The following error has occurred.');                    
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('Account fields that affected this error: ' + err.getFields());
            }
     
       }
    }
 
Run this code in developer console:

schedulable sc = new schedulable ();
        String sch = '0 0 9 * * ?';//it will run every day 9 am
        system.schedule('EveryDay', sch, sc);

OR
User-added image


Thanks,
Sivasankar
This was selected as the best answer