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
EricLehnenEricLehnen 

Trigger to update field when date field = today()

Hello everyone,

I tried creating workflow rules to meet my needs. However, the WFR will only fire when the account record is updated and I need a trigger to evaluate realtime. Unfortunately, I do not understand Apex, but I am starting to learn.

What I am trying to accomplish, is simply return two fields to Null when a date field = Today()

SIS_Status__c (Picklist)
Coma_until__c (Date field)
Dead_until__c (Date field)

The basic scenario is a sales rep will set the SIS_Status__c field located in the account record to coma or dead. Then, they will populate the date (Coma_Until__c and/or Dead_Until__c) when SIS_Status__c should be reset to Null. When Today() = the date entered in the date field, it will reset the SIS field.

Here are the three possible scenarios:

Senario 1:
SIS_Status__c = Dead
Dead_Until__c = 5/1/14
Coma_Until__c = 5/1/15

Under this scenario, when today's date = 5/1/14, it will make Dead_Until__c = null and change SIS_Status__c = Coma. Then when today's date = 5/1/15, it will change Coma_Until__C = null and SIS_Status__c = null.

Senario 2:
SIS_Status__c = Dead
Dead_Until__c = 5/1/14
Coma_Until__c = Null

Under this scenario, when today's date = 5/1/14, it will make Dead_Until__c = null and change SIS_Status__c = Null.

Senario 3:
SIS_Status__c = Coma
Dead_Until__c = Null
Coma_Until__c = 5/1/14

Under this scenario, when today's date = 5/1/14, it will make Coma_Until__c = null and change SIS_Status__c = Null.

If someone has a template trigger I could use to go off of or help me write this trigger, I would really appreciate the help!
Cheers!

temhof4242temhof4242
I would recommend maybe a different approach.  You are asking the user to provide 1 status and potentially two dates.  In addition to the compexity of coding a solution as you describe and it would likely have to be a scheduled job since you are not triggering on a record change, but in addition your solution necessary destroys user provided data.  I try to avoid both.   So, I would likely use two sets of fields like (Sales SIS Status, Sales Dead Until, Sales Coma Until).  Then use a second set of fields (Calculated SIS Status, Calculated Dead Until, Calculated Coma Until).  The second set would simply be formula text fields in which you can base your formulas on TODAY() and the content of the 3 user provided fields.  Then you base all notifications and reports on the calculated fields, but you always have the user entered data retained.
EricLehnenEricLehnen
Thanks for the reply!

Ah, I understand what you mean now by creating the formula fields. This would work, but eventually, a sales person would have to go back to the original Sales SIS to update it. So, saving the data is not necessary. I don't know if this solution would totally work beacuse I need the original Sales fields to be updated somehow.

For instance with your solution I have:
Calculated SIS Status Coma
Calculated Dead = Null
Calculated Coma = 3/12/2014
Sales SIS Status = Coma
Sales Dead = Null
Sales Coma = 3/12/2014

I could write the formula so that a Dead/Coma status would populate in the calculated SIS Status when the Calculated Dead/Coma dates are populated. But the Sales data would remain in there. It is imperative that the data is updated beacuse the SIS Status field has other statuses (Prospect, Qualified, etc.).
EricLehnenEricLehnen
Can anyone else chime in? I should be able to figure out how to write the apex trigger. I just need to know if the trigger will fire independently of the account object being updated (i.e. I don't need to modify the account for the trigger to initiate).

Cheers!