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

Assing Lead Status on Upsert Based on Date Values

I was wondering if this APEX trigger I’m trying to write is bigger than a bread box.   So far – I have three triggers in my organization (one of which still needs a little tweaking) that are going to save me hours and hours of work each week.  I have one last piece of automation that will be critical – but I’m having a tough time figuring out where to start.  Here’s what my process looks like – any chance anyone can take a shot?  This is a little outside of my skill set as what I've written so far has been fairly basic.


All of my campaigns are outbound telemarketing – customers are often part of multiple lists and, on occasion, these lists can come in within days of each other.   What I would like to do – is have a trigger that applies a "stand-off" rule of ninety days for any new leads on upsert or insert.   In other words, we don't want to call these customers back to back for different prodcuts.  So as my leads are imported – if there has been an attempt or contact in the last ninety days, the lead will be imported with the status as ‘pending’, and then will change to ‘open’ once we are outside of the ninety day window.    The 'pending' status would keep these from going into the outbound dialer.


There would be two objects at work here – the lead object, which would have a field called ‘Call Date’ and another field called ‘Party ID’ to match against on upsert/insert.   The other object would be the opportunity object (in the case of conversion) and would also have the same two fields to test on import.   So I need the trigger to look at the Account object, if there’s a matching party ID, check and see if the call date is null, if not, then see if the call date is > 90 days and if that is false, import as pending.    If all of those statements are true, then  move over to the opportunity object and apply the same test.


Then the last piece would be to have those leads that are changed to a ‘pending’ status changed back to an ‘open’ status once outside of that 90 day window based on the call date match.   I think I would have to have a field that would hold the call date from any matching object and build a time based workflow from that field.    I think I can solve for this last part, but the trigger piece is blowing my mind right now!


I don't fully understand your requirement (I'm only skimming it), but Triggers can be a bit weird to conceptualise because it really is the tail wagging the dog.


Have you considered a scheduled Apex job that could run periodically to set these values?  


Ha! That's very true!  


I won't get back to this until the weekend, so my plan of attack is to write this out a little better and see if the logic starts to make sense.   If not, does a scheduled Apex job cause any issues with converting a lead the way the time based workflows do?


I didn't even think about a scheduled Apex job - so I think you may have given me an easy out!


Thank you, sir!