3

i am quite new to salesforce so i am not sure about this, and i wanted to get a clear answer since i haven't found one yet. Can i schedule a Queueable class that basically makes a callout to get and insert Accounts, and then queues another callout to get and insert Contacts? Something like:

global class scheduledClass implements Schedulable {
   global void execute(SchedulableContext SC) {
      System.enqueueJob(new QueueExample());
   }
}

I need to schedule that to run every 4 hours.

2
  • 2
    Yes, you can certainly do this. Do you have a specific question on how to accomplish this? If you're stuck, we can try to get you unstuck, but it looks like you've already got the basics you need to get started.
    – sfdcfox
    Apr 6, 2019 at 0:35
  • 1
    also -- always good to also check to make sure you aren't at the queueable limit and have a fallback strategy before enqueueing
    – cropredy
    Apr 6, 2019 at 0:53

1 Answer 1

3

You can do this. Here are some of the things to keep in mind.

  1. To run every 4 hours, schedule your scheduled job using Cron expression [0]

  2. If you want to your queueable to make callout, make sure you implement Database.AllowsCallouts [1]

  3. In Apex code (be it Queueable, Batch job) once your make changes to db, you cannot make callouts. So make sure you adjust your code accordingly (either to separate into different jobs or do all callouts and then make db changes) [2]

  4. Scheduled job can start 20 other queueable jobs and one queueable job can start one other queueable job. This allows you to chain your jobs. [1]

[0] https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

[1] https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm

[2] You have uncommitted work pending. Please commit or rollback before calling out error

1
  • 2
    also -- in testmethods, you can't execute chained queueables - this requires clever testing techniques
    – cropredy
    Apr 7, 2019 at 5:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .