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
KGoswamiKGoswami 

Interesting Requirement: Shoot an email if no lead is created in the last one hour.

The requirement demands that if no lead is created in the last one hour a specified recipient should receive an email saying that no lead was created in the last one hour.

 

Example:-

No lead was created within the time period 9:00am to 10:00am. The specified recipient should receive an email.

If no lead is created within the next one hour(10:00am-11:00am) the recipient receives another email.

 

Now suppose a lead is created at 11:30 am. In this case the specified recipient will not receive an email at 12:00 o'clock. The one hour duration will be counted from 11:30 am. That is if no lead is created till 12:30 pm then the recipient will again receive an email at 12:30 pm.

Please help. 

BrendaFlynnBrendaFlynn

I think you can do this with the new scheduler task, although it would be considerably easier to run every hour on the hour than sixty minutes since last lead.

 

What I would do, in broad strokes, is to create a custom db or field that holds the last time there was a lead created. So let's say:Date Lead_Counter__c.Lead_Last_Created

 

Use the scheduler (http://blog.sforce.com/sforce/2010/02/spring-10-saw-the-general-availability-of-one-of-my-favorite-new-features-of-the-platform-the-apex-schedulerwith-the-apex-s.html) to run this every 10 minutes.

 

The method that you would run would do the following:

 

1) Run a query to identify the last time a lead was created. The query would look something like this:

 

Integer NewLeadCount [select count() from Lead Where CreatedDate >=  Lead_Counter__c.Lead_Last_Created]

 

2) Check to see if  NewLeadCount is greater than 0. If it is, update Lead_Counter__c.Lead_Last_Created to be the current time and you're done.

 

3) If the NewLeadCount == 0, then you need to see how long it's been since the lead count was last updated. If the lead count was updated more than 60 minutes ago, send off your email. You'll want to update Lead_Counter__c.Lead_Last_Created so that emails will be sent off every 60 minutes, not every 10 until someone finally makes a lead.

 

It won't be perfectly an hour since the last lead is done, but you won't be off by more than 10 minutes if you schedule it per 10 minutes. So for example:

 

Bob creates a lead at 1:55

Job runs at 2 pm and sets Lead_Last_Created = 2pm

Job runs at 2:10 no new leads

2:20 no new leads

2:30 no new lead, etc.

3:00 no new leads -- email is sent

3:18 new lead is created -- if no additional leads are created the email will be sent next at 4:20 

TbomTbom

The new Code Schedular mentioned in the last post could work.  But couldn't you also just hook some code into the Lead's  before insert trigger and check for the most recent Lead created? Then examine the time difference and send email via Apex if necessary.