Exam is an object. It contains the following fields - exam_name(String), exam_state(picklist), exam_date(date), expiration_date(date)

'exam_name' is an AutoNumber(string) field (starts from 1 and increments depending on the number of exams registered)

Below i have provided a pseudocode kinda.

Loop through the exam_name 1-100000. check exam_state, check expiration_date. if(and( exam_state is "active", and expiration_date > exam_date + 30)). set exam_state to "expired").

In theory, I want my code to find the exam_name field which is the primary_key, loop through all of it(eg. 1-100000), check the 'state' of each exam_name, set all the exams that have exceeded the 30 days from the day they were set-up, to change from "Active" state to "Expired" State. I want this to run daily.

How can i execute this? I'm new to salesforce working with a sandbox, i tried out work flows, process automation etc., but wasn't able to figure this out.

2 Answers 2


I don't know your experience level with Apex. But this requirement can be easily fulfilled with Batchable and Schedulable Apex.

Batchable because you want to make it future proof if you have really alot of records. Schedulable so you can schedule it to run every day during the night for example.

Edit: To filter your exams of the last 30 days you can use something like this: SELECT Id FROM Exam__c WHERE exam_date__c = LAST_N_DAYS:30



  • Hi, thank you for the links. I'm actually new to Apex started working on it for a couple of weeks now, but i have worked a bit with Python and JAVA. The links you have provided seem really complicated to understand. Any other ways I can approach this, an easier way? Aug 30, 2018 at 11:16
  • It can look quite complicated, but once you dive in it should be ok. Especially if you already have some experience with Java. You indeed try to do it with a time based workflow rule. But I think a scheduled job during the night is better performance wise.
    – Bram
    Aug 30, 2018 at 12:51

Just create a time-based workflow. And run it after (exam_date + 30 days) and in the field Action set the field status to Expired.

You can run this workflow in first criteria Whenever a record is created and in the condition you can check exam_date is not null. And if you are updating exam_date later then choose the third option.

  • @NivethKumar Did you face any challenge or error? Aug 30, 2018 at 8:37
  • this works when updating realtime(editing or creating), but it won't work for the exams that have already been created. I tried using a schedule to update it 1hour after the expiration date. Aug 30, 2018 at 8:38
  • @NivethKumar for old data you can exxecute one time script from developer console. That wont take much time. Aug 30, 2018 at 8:39
  • I created an entry, which was supposed to expire tomorrow, but i checked it the next day and it hasn't expired. I used process automation, and also tried work flow. salesforce checks the dates and values when im creating/editing a new entry, and when i create an expired entry, salesforce recognizes it and updates it to "expired". But when it comes to a delayed update something that needs to update tomorrow or so, it won't work. can you be kind enough to guide me on how i can build this one time script if you have the time. thanks Aug 30, 2018 at 8:45
  • @NivethKumar Share the exqm date data on your record and WF screen in your question. Aug 30, 2018 at 8:46

You must log in to answer this question.

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