I have a scheduled apex class that calls a queueable. This runs daily to retrieve data from an external API, transforms the data, and upserts sObjects. Some of that data transformation relies on Custom Metadata that maps the API value to the Salesforce value (e.g. a status value "1" → "New").
Unfortunately, if I add a new Custom Metadata record, the scheduled job does not see the new Custom Metadata until after I reschedule the job. Why is this happening?
For example, I added a new mapping record to map the API value "9" to Salesforce value "New Status". Manually executing the queueable (i.e. System.enqueueJob()) will result in correct execution with the updated mapping. However, the scheduled job will fail and throw an error because the value "9" isn't mapped. If I simply delete the job and reschedule it, it will run correctly.
execute(). Schedulables are serialized upon starting so CMDT data at the time of initial execution is all that is known for subsequent executions. Related Q&A