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
Tyler McCartyTyler McCarty 

Dynamic scheduler class

So the requirement is to build a way for users to be able to schedule emails to go out at certain times. So what I have done is built a custom object called EmailScheduler__c and gave it fields such as Recipients__c, Time__c, Subject__c, Body__c, FileName__c, ClassName__c, etc. I have then made a trigger that will run after inserting a new record of EmailScheduler__c and it will grab the information for that new object. 

Where I'm stuck at is how do I setup the scheduler class from the trigger. I know I can setup a dynamic scheduler and a dynamic emailGenerator class but how do I set that up to run, and when the user deletes a record would it delete the schedule on the backend?
Christian Schwabe (x)Christian Schwabe (x)
Hi Tyler,

my suggestion is to write an apex class that uses the schedulable-interface (trailhead: https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_scheduled). You can schedule the job by defining a cronjobs syntax in that batch job.
To start this job the first time use the developer console and anonymous code (debug > open execute anonymous window)
Feel free to ask in more detail about my suggested solution and if you need more information.

Regards,
Chris
Tyler McCartyTyler McCarty
Scheduling Emails using a Custom Object. I figured it out. I set up a trigger on the custom object, for two reasons. First it will call the scheduler class and set the cron time with what the user entered in on the new record. And the scheduler will call a schedulable class that will take the record as a parameter and set the email subject, body, and recipients. Then the trigger will also run After Deletion of a record to abort the job that was scheduled.
Christian Schwabe (x)Christian Schwabe (x)
Hi Tyler,

unfortunately it looks like some answers from you were changed or are deleted in comparison to the email notifications I've received.
I saw a snapshot for apex code which triggers a batch class in an trigger. In summary: It is not a good practice to call batches from a trigger: https://salesforce.stackexchange.com/questions/31000/is-it-advisable-to-call-batch-from-trigger (think about limitations and governour limits).
I'm afraid I don't understand the implementation. It sound a bit unusual.

Why do you have two scheduler classes? One suggestion / approach is to handover all processed record ids and the schedulable class should do the logic.

Regards,
Chris
Tyler McCartyTyler McCarty
Sorry, I would ask a question then figure out the answer so I deleted a few. And I ended up not using a batch class. And I only have one Scheduler class that calls a Schedulable class. I basically set it up the way you said, passing record id's from the trigger to the scheduler to the schedulable class. The implementation is to let Admins schedule Visualforce Pages that contain Reports to go out via email at certain times. So if a department wants a report to go out by email once a month/week/day etc then an admin can set it up without having to code a scheduler and class. Thanks for your feedback on this, greatly appreciated Christian.
Christian Schwabe (x)Christian Schwabe (x)
I'm happy that I could help.
Perhaps it is also a solution to subscribe to an standard-report.

Please mark one answer as best answer if I fulfill your requirement.

Regards,
Christian