0

I just started to learn apex programming and i was wondering how to link apex trigger to existing workflow.

For example, i do have a workflow that in a CASE changes a checkbox field old_case_c from FALSE to TRUE if criteria is met: CreatedDate > 50 days AND old_case_c is FALSE. Besides criteria, this workflow is triggered only when case is updated (this is how workflow works).

Is it possible to somehow create a scheduled apex trigger that queries all CreatedDate > 50 days (and old_case_c is FALSE) cases and changes old_case_c field to the TRUE daily. BUT if workflow criteria is changed by a person (who don't have intention to learn apex) to the CreatedDate > 60 days, this apex trigger will be querying all CreatedDates > 60 days (and old_case_c is FALSE).

This way this scheduled apex trigger will be controlled by a workflow (that could be set as inactive one).

Thanks in advance!

1 Answer 1

1

To answer directly, you can't link Apex triggers and workflows in the way you're discussing, but you can configure declarative automation and Apex to pull from the same store of configuration values - Custom Settings. By creating a Custom Setting with a field to store the number of days you want to use, you can have both declarative and programmatic solutions adapt when you change that configuration.

In a Workflow Rule, you can easily reference Custom Settings values by choosing "Run this rule if the -> formula evaluates to true" and clicking Insert Field. Your hierarchy custom settings are listed in the field tree for selection.

Additionally, note that Scheduled Apex and triggers are separate functionality. What you're describing is closer to a scheduled Batch Apex job.

However, you don't need to go to that extremity to achieve what you're seeking here. Your objective looks like it's achievable with plain old Time-Dependent Workflow Actions.

You'd set up a criterion for old_case__c being false, and establish a time-dependent action for 50 days after creation date, with a field update to set old_case__c to true.

Time-based actions are a little counterintuitive, so I recommend reading through the doc linked above thoroughly to figure out how they handle changes to dates and criteria fields (it generally just does the right thing for you), but this should get you set with no Apex at all.

While it's true that this won't solve the issue of your existing Cases, you can work around this. Create a new field (say Case_Cleanup__c, a Checkbox) on your Case. Then add a new Workflow Rule with the entry criterion of Case_Cleanup__c = true, and use the same Time-Dependent Action you use for your primary workflow (50 days after Creation Date, update old_case__c). Finally, just use a Data Loader job to set the Case_Cleanup__c field on all your old Cases that need to be handled, and you'll get the proper timed actions scheduled via that new workflow. Once they're all processed in ~2 months, you can remove the new field and the extra workflow.

7
  • Thank you for your comment! I've failed in my example to mention, that i want apex trigger to evaluate another field. Addition to the example: If case Status is "Paid" then it's not triggered. So Time-Dependant Workflow rules won't do the job.
    – RomanK
    Commented Aug 2, 2018 at 15:57
  • I will look into those custom settings though
    – RomanK
    Commented Aug 2, 2018 at 15:58
  • I don't see how that prevents the use of time dependent actions in either Workflow or Process Builder.
    – David Reed
    Commented Aug 2, 2018 at 16:08
  • David, does this work for already created cases or just for the new ones?
    – RomanK
    Commented Aug 2, 2018 at 22:30
  • Just new ones. Old ones you could transform with a data loader.
    – David Reed
    Commented Aug 3, 2018 at 0:45

You must log in to answer this question.

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