+ Start a Discussion
Nina GonzalezNina Gonzalez 

Apex based on condition of two fields

Hello, I am new to Apex (although been a salesforce admin for 5+ years) and hope someone can provide some guidance.

I have two use cases that have similarities:

1. When date field < today () and another field is > 0, update a third field.

2. When date field = today + one year and another field = specific value, create a record with specific values.

I can't do this with flow or process builder because the record will not necessary change to trigger a process builder update. 

I suspect I am doing an "after insert" and "after update". 

I suppose I will need to establish a set of records and a loop.to look for them.

I will use an "if" statement to specify my conditions. 

How do I tell the trigger when to run? It is possible to set them up on a schedule. For example, the first use case should run every night. The other use case should run daily. Do either of these requirements put unusual burden on SF limits?

Thanks for your help.
 
Egor Gerasimenko 9Egor Gerasimenko 9

You don't want a trigger here, since triggers are just like workflows in the sense that a record must change (be updated, inserted, or deleted) for triggers to execute. You can create schedulable classes and then schedule them to run on specific days at a specific time, like a daily job. This trailhead demo is specifically about how to do that

https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_scheduled

Nina GonzalezNina Gonzalez
Thank you, this is so helpful!