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
paul-lmipaul-lmi 

@future methods with triggers

we're running into an issue where we're hitting errors like "unable to gain exclusive access" when triggers and apex are being fired off.  i started looking into pulling the code out of the triggers and loading it into standalone Apex classes with @future methods, but, the more I dig, the more it looks like I'll have to do a complete rewrite.

 

example

 

I have a trigger that fires AfterInsert on Case.  It looks to see if a contact is assigned to the Case, and if not, creates one based on Case.SuppliedEmail and Case.SuppliedName, then assigns it and updates the Case.  Part of this creation involves a Contact helper class I wrote for doing various things on the Contact object.  For instance, ContactUtils (the class) has a method called exists(string email) that returns true or false depending on whether the Contact exists.  I can't convert this method to @future unless I change it from returning a boolean to being both static and void.

 

I couldn't find a lot of detailed strategy around using @future with triggers, and even more so, ensuring "bulkification", which I'm sorry to say that this doesn't currently do.  We don't batch import Cases, so bulk isn't currently an issue, but I'd also like to get the code prepped and ready for that to ensure future issues do not come up.

 

Any thoughts on strategy here?

AcronymAcronym

Hey Paul,

 

I think that you are triggering on the wrong event.  If you find that you "may" alter the triggering record, such as updating a case to set the related contact, then you should be doing it in a "before" event.

 

@future methods only make sense to be static and void.  When an @future method executes, it needs to be self contained and self contexted (ie parameters passed in provide the context).  This is because there is no "execution thread" to return a value to, it's being executed on it's own.  An @future method that just determines if something exists does not seem to make sense.

 

You can certainly call the "exists" method in your Contact helper class from an @future method though.

Message Edited by Acronym on 01-04-2010 10:42 AM
paul-lmipaul-lmi
But if I call it BeforeInsert, wouldn't it block the insert until it's done executing, thus defeating the purpose of being @future?