Friday, October 17, 2014

Salesforce DML Limit... ARGH!

ARGH
So there I am...  5AM on a Friday morning and I check a Salesforce task that should have run hours ago.   Thunk Thunk Thunk...  Is this thing ON???   Apparently not.

I run it manually and THWACK...  " Too many DML statements: 151"  After an hour of Googling and poking at the execution log the reason for this THWACK is obvious.  This Salesforce job was updating leads within a loop.  That's OK if the loop makes less than 150 iterations.  Shit...  This loop was trying to update thousands of leads.

The solution?  Pfff...  Rather than update each individual lead I need to change the code to place my updates in a List<Lead> and then, after the loop, updating the List.

What the Hell?!?!?  It worked in the dev Sandbox fine, just the way it was!  This is like taking a hot sports car out for a test drive, turning heads while driving 95 MPH down the freeway, feverishly buying the thing, then, once you are sitting in this testicular expression of your manhood, discovering it will only do 65MPH.

The specific lesson here is this...  When writing a Salesforce job (or anything for that matter) in APEX, DO NOT update table data within a loop.

Now leave me alone.  I gotta go rewrite this damned APEX job.

***Edited Oct 17, 2014 - 3PM***
Fine.  Just Effin Fine.  I found out why it worked in the sandbox.  Another programmer meant to refresh his leads in the sandbox while I was testing.  Guess what...  He refreshed my test data as well.  So, even if the 150 DML Limit works in the Sandbox, my testing wouldn't have caused a problem because there was only 50 leads in the damned Lead table!!!

New version is done and tested.  Guess I will get up before the dogs Monday and roll this out.

No comments:

Post a Comment