In this article you will learn about how to programatically generate high numbers of forms.
In order to accommodate the need to pre-create forms on a very large scale we’ve created a process that can be called programmatically (ex. a Flow or Apex). This will allow the mass-creation of forms in bulk. More importantly, this invocable action defers the creation of answer records to an asynchronous queueable job. This allows you to reduce the amount of SOQL query limits / CPU time out issues you may be having while creating large quantities of Forms records.
There are four important concepts to understand when deferring Answer creation.
1. Skip Form Trigger
Before creating your Form(s), you should tell the system to skip the Youreka form trigger. This can be done programmatically by using the disco.Util_SkipFormTrigger.skipFormTrigger() helper class, or you can call this class from within a Salesforce Flow. An example of this class being used can be seen in the code snippet and screenshots below.
2. Indicate That Answers Need Creating
A checkbox field exists on the Form object called Needs Answers Created (disco__Needs_Answers_Created__c). When creating your Form (either via apex, or in a Salesforce Flow), be sure to mark this checkbox to TRUE.
3. Invoke the Answer Creation Job
Call upon disco.Invoke_AnswerCreationJob, which is a queueable job that identifies the Forms in the system which needs Answers created, and then creates those Answers.
4. Set Your Batch Size
Using the Youreka Custom Settings in Salesforce, you have the ability to control how many form records are processed by the queueable job at one time. Increasing the batch size typically decreases the amount of time it takes to process all of the forms that need answers. But by setting the batch process too high, you run the risk of approaching Salesforce's DML limits (as of this writing, it is 10,000 records per transaction). Consider using the following formula to best calculate what your batch size should be...
(Number of Answers Per Form) x (Number of Forms Per Batch) x 2 < 10,000
Out of the box, Youreka's Answer Creation Batch Size is 10, but you can adjust this number to optimize your system, based on the formula above.
Below is an example of the process, demonstrated in the developer console.
In the Developer Console/Execute Anonymous, run this code:
Id templateId='a07f200000PoXhP'; //(ID OF A GIVEN TEMPLATE)
disco.Util_SkipFormTrigger.skipFormTrigger(); // skip answer generator and other form validations
insert new disco__Form__c(disco__Form_Template__c=templateId,disco__Needs_Answers_Created__c=true); // generate a new form that needs answers
This will generate a form with no answers, the checkbox for "Needs Answers Created" will be checked.
Next run this code in the Developer Console/Execute Anonymous:
Wait a few seconds for the job to complete. Then navigate to the form created above, and all the questions/answers should be loading correctly.
Once you are in a place where your automation is set up and you are ready to test, run the automation and check to see if Forms are being created properly. Here are some queries you can run.
View Forms in Salesforce where Needs Answers Created = TRUE
select ID, disco__Form_Template__r.Name, disco__Needs_Answers_Created__c from disco__Form__c WHERE disco__Needs_Answers_Created__c = true
Immediately after your automation is run, this query should return some results, and slowly count down to zero as the queueable job runs.
View Queueable Jobs In Salesforce
Visit Setup -> Jobs -> Apex Jobs to verify that the Answer creation queueable job ran properly.