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
dreamrealdreamreal 

Recurring Emails

Hey all, 

 

I need your help with trying to create a function that sends emails on a recurring basis. I called support but they werent very clear on how I could do this, so I'm turning to you for help. 


My function should send an email to the assigned user every 2 hour if the case priority is high and the status is new

 

So far I've tried the following but I believe it will not work:

 

 

trigger alertUser on Case (after update, after insert) {
datetime now = datetime.now();
datetime created = case.Createddate;
If (math.mod(datetime.getTime(created)  -  datetime.getTime(now), 7200000) == 0){
//send email
}
} 

 Thanks in advance

 

Venkat PolisettVenkat Polisett

I believe, trigger is not a correct choice here. You are planning to send an email every 2 hours for new high priority cases. Your trigger will fire on create and and if and only if some one updates some thing on the Case again. If nobody touches your case for a day, nobody gets informed.

 

You have 2 choices.

 

  1. Timebased Workflow
  2. Schuedled Batch job

If you know how many times you want to send email while the status is new, you can write a timebase workflow to do that. I belive, the best practice would be to escalate the case if the assignee did not act on the case in the given time frame.

 

You can also write a batch job that fires every 2 hours on the clock and looks at the cases that are not worked on by the assgnees in a given time frame.


dreamreal wrote:

Hey all, 

 

I need your help with trying to create a function that sends emails on a recurring basis. I called support but they werent very clear on how I could do this, so I'm turning to you for help. 


My function should send an email to the assigned user every 2 hour if the case priority is high and the status is new

 

So far I've tried the following but I believe it will not work:

 

 

trigger alertUser on Case (after update, after insert) {
datetime now = datetime.now();
datetime created = case.Createddate;
If (math.mod(datetime.getTime(created)  -  datetime.getTime(now), 7200000) == 0){
//send email
}
} 

 Thanks in advance

 


 

 

BrianWKBrianWK

I've actually done this in the past. The time-based workflow is the easiest solution available. A trigger will not work since it fires immediately upon insert/update and you're looking for something to repeatedly do a function regardless of the lack of update.

 

Using a trigger means that a case gets created and MUST be edited at in order to fire off the e-mail. The trigger option will immediate cause you an issue since I could create a case - and never ever update it. That case would never spawn the e-mail.

 

The Schedule Batch is a second option. You would write a scheduling class and a class to do the actual work. There you would query all Cases that are new and older than 2 hours. The schedule class would run every two hours and call that "work" class. The problem with this is it won't actually achieve what you're trying to do. You want to have an e-mail send every 2 hours for each indivudal case. The scheduler won't do that. The scheduler will fire off every two hours. So if the scheduler starts at 12:00AM and a case is created at 1:00AM -- that case won't get emaield until 4AM -- 3 hours after it's been created.

 

Quite honestly Venkat is correct to try to use the workflow with time based emails. If for some reason you need use APEX to get the email template you want - you could use the timed workflow to update a field -- and that would cause the trigger to fire and send the email. Not the route I would take, but it would give you the option.

dreamrealdreamreal

Your points about using the trigger are correct and I appreciate the responses. However I do not understand how using a time-based workflow rule will create recurring emails. I know for sure that you can set up email alerts at specific times, but beyond however many alerts you set up, no emails will be sent. In addition you cannot beat the system. I attempted to set it up so a workflow rule updated a field which would then trigger the workflow rule again, but to no avail. I must be missing something. If you guys could explain how exactly it would help me immensely. 

BrianWKBrianWK

Good points. You're right that once you've run out of pre-set time workflows it would stop working.

 

I haven't tested this to see if it would work but here's a suggestion.

 

1. Add a field on the case - I would choose a checkbox.

2. Create WorkFlow A - This fires when the status is new and the checkbox == false. This workflow has 1 timebased workflow that send the email and marks the checkbox to True

3. Create Workflow B - This fires when the status is new and the checkbox == True. This workflow as 1 teimbased workflow that send the email and marks the checkbox to false

 

Hopefully Workflow A and Workflow B will continue to fire each-other off until someone updates the status. This should have the Email fire every 2 hours. Since every 2 hours it would send an email, update the checkbox field, and start the timer on the other Workflow rule.

 

 

dreamrealdreamreal

Thats a beautiful solution, I was thinking along the lines of using ISCHANGED() with two rules but that wouldn't work. I have managed to create these two rules though, and finger's crossed this should work. Thanks a ton.

BrianWKBrianWK

One more thing you may want to watch out for...

 

My solution assumed that no one was changing the case except the workflows. I think it could be feasible that if I edit a case (didn't change the status) at hour 1, that it would "reset" the clock so to speak.

 

This would be a problem only if your workflow allows people to edit cases and not update the status.

dreamrealdreamreal

Unfortunately this doesn't work because a workflow cannot trigger another workflow or something alone those lines. Thanks for the effort though.

BrianWKBrianWK

D'oh...

 

Wonder if we could get a bit creative.

 

This is going to be a pain but it MIGHT work. I haven't tested yet - but here are the pieces

 

1. Workflow Rule A -- Has 2 hr time base to send email when Status = new and "Checkbox" = false. At 2 hrs it sends the email and flips Checkbox = true

2. Trigger 1 - Checks to see if the Checkbox field is true and Status is new. If both is true it send the records to:

3. Future Class 1 -- This is a @future class that updates the records by updating the records to checkbox = false

 

So Case is created setting off Workflow rule. 2 hours later an e-mail is sent and the record is edited marking the checkbox true. This edit calls the trigger which passes the case into a future class. The future class then updates the case marking the checkbox to false.

 

Rinse and repeat. The future class would be important since it would update the record outside of the existing session.

 

Seems pretty darn lame to have to do this though. How many hours do you anticpate a case being "new" and requiring the e-mail?

dreamrealdreamreal

Yea, it also seems like a long process. I'd need these updates to be about a week so I think I'll just create each each email alert for each couple of hours (over several workflow rules - can only have 10 time triggers per rule). Thanks for your help.

fb123fb123

This is a great idea! I think it might just do the trick for this rule that I just wrote about in a different post. We're trying to send recurring "SUBSCRIPTION TO BE RENEWED" e-mails. Sending one using time-based workflow is easy but because we have an auto-renew feature, we need these to go out annually as long as someone is an active subscriber.

 

Now that I've perused this thread, it seems we can add a "Sent?" Checkbox custom field (default = FALSE) that gets set to TRUE when an e-mail fires away but is reverted to FALSE 1 hour later, using a time-based workflow field update.

 

We can then update the workflow rule to only run if the checkbox value is FALSE, and the value being changed from TRUE to FALSE and vice versa would cause the rule to be triggered again, thus meeting the evaluation criteria of "When a record is created, or when a record is edited and did not previously meet the rule criteria."

 

I'll update everyone shortly. Please feel free to reply with any additional comments/ideas!

 

-------------------

 

Here's what I posted at

http://community.salesforce.com/t5/Formulas-Validation-Rules/Before-Rule-Trigger-Date/m-p/170706/highlight/false#M7181

 

"I was wondering if anyone can tell me whether something like this is even possible when it comes to applying the "Before Rule Trigger Date" option.

 

We have tried to put together a workflow rule on a custom subscription object that sends a customer a reminder 30 days before the subscription renewal date. It's very easy to have this rule apply only once, but the problem is our subscriptions are auto-renewed and we need to send these reminders before every renewal date (so once a year as long as the subscription is active).

 

Since we need a time-based workflow action for this, the rule is only evaluated when a record is created or when it is updated to re-trigger the rule.

 

This formula, for example, would work just fine if we only needed a reminder to be sent once:

 

 

AND(!ISBLANK(next_sched_renewal_date__c),ISPICKVAL(subscr_status__c,"Current"))

 and the time-based trigger would send an e-mail 30 Days prior to next_sched_renewal_date__c.

 

 

Unfortunately, the rule would continue to evaluate to TRUE, hence it would not be triggered again.

 

Since we needed to have the reminder e-mail go out every year during an active subscription, we came up with this formula that evaluates to true when the next scheduled renewal date is equal to TODAY():

 

 

AND(!ISBLANK(next_sched_renewal_date__c),next_sched_renewal_date__c = TODAY(),ISPICKVAL(subscr_status__c,"Current"))

 and the time-based trigger would send an e-mail 30 Days Before Rule Trigger Date (i.e. next renewal date).

 

 

The thinking was that Salesforce would know that the rule will be true in 30 days and will fire the e-mail accordingly at that time. Unfortunately, that's not how the "Before Rule Trigger Date" option works. It adds the reminder e-mail to the queue ONLY on the actual Rule Trigger Date and has it go out then, not 30 days before.

 

Can anyone think of a way to accomplish this task?

 

Thank you all in advance,

 

Dan"

fb123fb123

It looks like this isn't going to work after all. We set up everything up correctly using Checkbox and Picklist field types as the toggle fields, but that workflow rule simply couldn't trigger another one, thus discontinuing the recurring chain.

 

Salesforce's user guide states the following in Field Update Considerations:


"The results of a field update CANNOT trigger additional workflow rules or other rules such as validation, assignment, auto-response or escalation rules."

 

Perhaps scheduling a recurring apex job may be an option.

Akhil AnilAkhil Anil

You can achieve this by setting up 2 workflow rules that will update a custom date field which will mimic recurring workflows like this
Workflow Rule 1 : Update Custom Date field to Today (Immediate Action)
Workflow Rule 2 : Update Custom Date Field to Today (Time-triggered action one day after the first workflow rule). This date will also be a part of the the criteria in the second workflow rule.

The detailed explanation for the same can be found in the below link

https://force-base.com/2016/11/13/power-of-point-click-series-2-learn-how-to-set-up-recurring-time-based-workflows-in-salesforce/
Aaron MatternAaron Mattern
Hi Akhil, your solution is clever, and I see how it would work for a daily email.  However, do you think the same thing would work for an email every hour?
coolkrishcoolkrish
Hi,

I tried Akhil's solution but after second WF fires, it is not firing again like in a loop.
After the first fire, I don't see the request queued up in Tme-Based workflow queue.

Thanks,
Krishna.