[SalesForce] How to create over 10,000 records with information from Visualforce

I have an inventory input page that I am working on and I am running into an issue where I need to create more than 10k records. The problem here is that all the information needed is not stored in an object but captured on the web page. Inventory numbers are entered and difference calculations are done to determine how many records to create. A record is created for each number in between 2 numbers the user inputs. The case I just tried and hit the limitation on attempted to create 27,500 records.

I assumed that I could use Batch processing but I can only find information related to querying for records and the Start method needs to return a Database.QueryLocator object.

Another option I am considering is breaking the lists into chunks less than 10k per list and calling a series of future methods – this would allow me to insert 100k records. The information could be passed in as a JSON string and broken down on the otherside.

Is anyone aware of something else that I can use to insert this many records with information collected during one execution context?

Best Answer

You don't need to use a QueryLocator. For example, you could write it like this:

public class MakeRecords implements Database.Batchable<SObject> {
    SObject[] queue;
    public MakeRecords(SObject[] records) {
        queue = records;
    }
    public static SObject[] start(Database.BatchableContext context) {
        return queue;
    }
    public static void execute(Database.BatchableContext context, SObject[] records) {
        insert records;
    }
    public void finish(Database.BatchableContext context) {
    }
}

You could also have some wrapper class and iterate through those items, if that's easier to code. You could even have an array of JSON strings, and deserialize the strings in the execute method.

The only requirement is that your start method must finish in 10 minutes. Normally, this isn't a problem, but do keep in mind that non-QueryLocator implementations will be rather severely limited (5 million or so values instead of 50,000,000 records).

You could even perform the "calculations" you mentioned in the start method, and return the results from start.

Your Visualforce page can periodically poll the AsyncApexJob table by job ID to show the user a progress bar.

Related Topic