I have a bunch of contacts whose details have been licensed from other companies. The only caveat to being able to treat them as normal contacts in our Salesforce database is that they must be deleted when our license for these clients expire. We have different arrangements with different clients so we simply have an "expiry date" field on the contact object that denotes when the contact must be deleted.

Is there any way to automate the process of deleting these contacts on a daily basis if their expiry date = today?

  • You can write a scheduled job to pickup those contacts which has been marked as expired and delete those contacts. – Santanu Boral Jun 25 '18 at 13:23
  • @SantanuBoral How do I set up a scheduled job? – J. Stott Jun 25 '18 at 15:25

Your scenario is a classic use case for Batch Apex. What you will need here is to write a batch class, which will look for all such Contacts those need to be deleted, and then schedule this batch daily to be able to process contact deletion based on the criteria.

As for how to write batch apex and scheduling them, there are lots of resources available on documentation and trailhead. Following links are a good place which will provide you a pointer to understand the topics around batch apex, and scheduling the batches.

In your scenario, you will need to perform the following in your batch class:

  1. In start() method - You will need to perform a SOQL to retrieve all such Contacts which have their expiry date as of today
  2. In execute() method - You will delete all such Contacts which were retrieved based on your criteria
  3. In finish() method - You may like to perform any post deletion tasks if at all.

Once you have your batch class written, write a Scheduler and schedule it (hourly, daily, etc.) based on your requirements. Refer the link above on scheduler for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.