You need to sign in to do that
Don't have an account?
Trigger firing at wrong time
I'm having an issue with my trigger that is confusing me. My trigger is called on a before update when a specific field on the contact becomes empty. My trigger code calls a separate @future apex class only if the field i check against begins populated before the save and then becomes empty and then makes a callout to an external system. There is also a third party batch process that does upserts on all contacts every night and updates some fields. The field that fires off the callout is never modyfied during this batch process.
The problem is during the nightly batch process the upserts are causing my trigger to call the @future class causing it to throw an error.
caused by: System.DmlException: Update failed. First exception on row 0 with id 0033000000z3r2cAAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, InvalidEmailContactTrigger: execution of BeforeUpdate
caused by: System.AsyncException: Future method cannot be called from a future or batch method: EmailIssueRemover.removeEmailIssue(MAP<Id,String>, String, String)
for(Contact contact : trigger.old) { emailissue = contact.Email_Issue__c; } for(Contact contact : trigger.new) { if(contact.Email_Issue__c == null && emailissue != null) { contactsToUpdate.put(contact.Id, emailissue); session = contact.session_id__c; server = contact.server_url__c; lastName = contact.lastName; changed = true; } } if(lastName == 'ApexRunTestIssue'){ EmailIssueRemover.test(contactsToUpdate); } else { if(changed){ EmailIssueRemover.removeEmailIssue(contactsToUpdate, session, server); } }
Can anyone explain why this might happen.
So heres the deal on your trigger (and yes you should remove the trigger.old loop and replace it with the reccomendation of the previous poster:
All Answers
Batches are considered a Future method because they happen sometime in the future. It does work on an object causing your trigger to fire. Your trigger also calls a future method. Future methods are not allowed to call other future methods.
I understand this but the future method should only be called if the field becomes empty. In the batch process the Email Issue field is never modyfied and thus i would assume that the future method would never be called.
Try putting a debug statement right before ur @future method and see if it prints up.
After looking at my code i realize that the issue might have to do with the variable 'changed' being true.
Im not very experienced with apex and triggers and wondering if you can explain what happens in this trigger during its lifetime. I understand that trigger is bulkified and will handle 200 records each time but im wondering how the variables are changed during this process and how the trigger handles calling the future method only once for every 200 records.
Ditch the first loop and replace your if statement with:
Thanks so much i will try this.
i actually left out a couple lines that are in my trigger code. I set the value of emailissue to null just before setting it to the old value.
Does this mean that what you asked me to change won't really make a differnce or do you still believe my problem is in that loop. I havent yet been able to test the change and feel like the boolean variable 'changed' is more likely to be causing the problem and that i should maybe have a better way of checking to see if a contacts email issue was changed.
thanks
i posted full code below of my trigger.
What I posted will definitely help your trigger. There may be other issues we find after, but I can guarantee that is one issue your trigger has.
i only asked because the trigger does work fine when updated directly through a contacts page and also through bulk uploader just doesnt seem to work in the batch process since the future method is being called unnecessarily.
thanks again
I think you have only been getting lucky with it working with the bulk uploader.
So heres the deal on your trigger (and yes you should remove the trigger.old loop and replace it with the reccomendation of the previous poster:
Thanks to both of you for all the help this is the explanation i was looking for.