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
iaestemaniaesteman 

Create bunch of records with apex for a specific month and time to be updated from 6am to 6pm every 5 minutes.

Hello,
I am trying to test the time that will be needed to be created more than 90000 records in salesforce with apex code. I need this so i can prove my boss that this is not a best way to run things.
Why we need this?
We basically want to create available time frames for every course so that people can book a time and play on that course.
For example for one day the available time is from 6am to 6pm and every 5 minutes i need to create records to create those time frames. 
More into details:
1 day has 12 hours(6am to 6pm) in 1 hour there are 12 records so for 1 day i will need to create 144 records with this apex code and i need to do that for a specific month(lets say June 2019).  Total: 144 x 31 = 4464 records.

This is my code that I did so far and run into developer console and i defintiely need improvments if anyone can help i will be eternaly grateful!
 
List<Courses__c> course =[select id from courses__c]; 
List<Available_Time__c> cList = new List<Available_Time__c>(); 
for(Courses__c i: course){ 
for(Integer j=0;i<2;i++){ 
Available_Time__c cust = new Available_Time__c(); 
cust.Course__c = 'a0C0D000000Q7qbUAC'; 
} 
}
insert cList;

Thanks,
Darko
Edgar SantillanaEdgar Santillana
Hello,

To create a bunch of records with a specific time you probably need to use Schedulable class, so here the code.

Class (Here only left filter the SOQL with the time to not create more course with the other times)
global with sharing class SomeClass implements schedulable{

    global void execute(schedulableContext ctx){
        
        Datetime dt = System.now();
        Datetime dtfiveminutesbefore = System.now().addMinutes(-5);
        Integer numAvailableTime = 2;
        List<Courses__c> course = new List<Courses__c>(); 
        List<Available_Time__c> cList = new List<Available_Time__c>(); 

        course = [select id from courses__c Where createdDate >= :dtfiveminutesbefore and createdDate <= :dt Limit 10000];

        if(!course.isEmpty() && course.size() > 0){
            for(Courses__c i: course){ 
                for(Integer j = 0; j < numAvailableTime; j++){
                    Available_Time__c cust = new Available_Time__c(); 
                    cust.Name = 'Available Time' + j;
                    cust.Course__c = i.Id;
                    cList.add(cust); 
                }  
            }
            insert cList; 
        }
 
    }
}
And the Test Class
@isTest
private class SomeTest {

    public static String CRON_EXP = '0 0 0 15 3 ? 2022';

    static testmethod void testScheduledJob() {
        List<Courses__c> coursesList = new List<Courses__c>();

        for(Integer i = 0; i < 12; i++){
        System.System.debug('List ' + i);
            Courses__c course = new Courses__c();
            course.Name = 'Course ' + i;
            coursesList.add(course);
        }
        System.System.debug('List ' + coursesList);
        Insert coursesList;

        Test.StartTest();
            SomeClass some = new SomeClass();
            // Seconds Minutes Hours Day_of_month Month Day_of_week optional_year
            String sch = '0 5 * * * ?';// Or '0 0 1 * 6 ? 2019'
            String jobID = System.schedule('Create Available Time records', sch, some);
            List<Available_Time__c> recordsExist = [SELECT Id FROM Available_Time__c];
            System.assertEquals(0, recordsExist.size(), 'Available Times exist before job has run');
        Test.StopTest();
            
        recordsExist = [SELECT Id FROM Available_Time__c];
        System.assertEquals(24, recordsExist.size(), 'Available Times exist before job has run');
    }
}

If you need more about Schedule Class see this trailhead (https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_scheduled#). And other option you could use Workflows or Process Builders to avoid use code.
Regards!