function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Justin GreenJustin Green 

Apex Trigger vs. Workflow - When do they "trigger"?

Hello,

We have many workflow rules that update dates, statuses, and what not that are not working properly because the workflows only fire when someone updates an account or opp field and saves the change.  We're needing these types of rules to work without someone updating a record, based on dates.

Example: Opportunity has a contract end date of 12-31-2014.  We want the Account Status to update or a new opp created based on the contract end date minus 90 days (as an example).  And we'd like that to update whether or not someone makes a change to the account and saves the change (which seems to be required under workflow rules).

Will Apex triggers accomplish this?  If so, I'm planning on learning basic Apex coding to do simple triggers.  If not, then we may have to hire someone outside to come in to do this.

Thanks!
BalajiRanganathanBalajiRanganathan

if you want to update the account status 90 days before contact end date, you can use workflow with time dependent action and Time Trigger (Time based workflow) and cross object field update

https://help.salesforce.com/apex/HTViewSolution?id=000005245&language=en_US
https://help.salesforce.com/HTViewHelpDoc?id=workflow_cross_object_field_updates.htm&language=en_US

but if you want to create new opporunity which is not possible using workflow, i would suggest create a bacth process(Batch Apex) and schdule it to run daily which will check for opportunity with contract end date 90 days before.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm
Justin GreenJustin Green
Thank you balaji!

With respect to time-based workflows - we have existing workflows that were created to update these fields based on a Contract End Date.  However, if nobody updates the opportunity or account, these workflows never get fired, which means the workflow leaves data in an unreliable state.  I read through the documentation you linked to, and while it's possible that the existing workflows were not correctly written, it's still not clear to me if the time-based workflows will fire regardless of whether there is a saved update to a record.
BalajiRanganathanBalajiRanganathan
1) you can write your workflow evaluation criteria is set to  'Evaluate the rule when a record is: created, and any time it’s edited to subsequently meet criteria'
2) you can set your rule criteria as contract end date of is not empty. and you can set the time trigger based on Contract date (before 90 days of Contract date).
3) above setting will put the action into time based workflow queue when the contract date is set to some date.
4) if the contract date is required on creation of record you can also set the evaluation criteria to Evaluate the rule when a record is: created
5) Once the action is in the queue, it will have scheduled date set as contract date - 90. there is no update required on the record to trigger this action.
6).Also the scheduled date changes dynamically when ever you update the contract date.

you can also monitor the time based workflow queue in the Setup, click Monitoring | Time-Based Workflow