You need to sign in to do that
Don't have an account?
Mass Update with SingleEmail Trigger
I need some assistance bulkifing a trigger.
I have been tasked with setting up a Fulfillment Email trigger that notifies end users when an Asset has been shipped.
I have found I need to use the SingleEmail method due to needed to Mail Merge Asset Fields.
The system must identify the contact on the Asset, the Contacts in a group associated to the Asset, the contacts on the account requesting all notices sent, and if a web fulfillment all users in the Public Group associated to the account.
The trigger works well if I am updating a single record at a time. I run into issues when my vendor asset also updates all associated records for other accounts giving me a 101 SOQL error.
I have been able to remove the error by updating out of the for loop, but the emails now only go to the first account (as expected) [0].
I have thought about running a schedule apex class, but if I can only send 2 or 3 notices at a time, that will take too long to complete.
Any assistance is greatly appriciated. I have posted my WIP code below:
----- REMOVED CODE AS UPDATE IS AVAILABLE BELOW -----
You are getting 101 SOQL query error because you are hitting limit.
In single context there can be exceution of 100 queries.I reviewed your code and found that you are using queries within loop and that is not best practice.I would suggest you to get information in list/variable out side the loop and then assign within loop.
Hope this works for you.
Besides the email templates, which I corrected without success, where do I have queries in for loops?
Also, right now the code only works for the first item in the list when sending out a single email. How can get the code to send out multiple single emails to the correct contacts when doing a bulk update? It will send out 8 emails when the code fires, but all 8 emails go to the same contact. It should be 8 different Asset Records to 8 different Account/contacts.
I know I am hitting my limit. My question is how do I get the code to operate without hitting that limit.
Thanks
Here is my most current Code.
The code currently works because I have told it not to collect the information or send the emails for 'Vendor Letter'.
If I take that statement out, the system sends out multiple emails (depending on number of Assets) to only the first Asset information in the list.
I now have a strange occurrance where the After Update email is being activated twice.
If I move the vup2 update to inside the for loop so it will actually collect the correct information for each Asset it works.
Really, I need the for loop to collect the Contacts needed for each Asset, but that runs me passed the 100 SOQL easily on a mass update.
I have begun looking at the @future statement and running a class instead of a trigger, but I am unfamiliar with the @future statement