You need to sign in to do that
Don't have an account?

How to bulkify a simple email trigger?
Hello, I have a simple trigger to send an email after deletion of a Quote line item record. It takes some information from the parent Quote like:
Status and Name.
The thing is I don't know how to bulkify the code. What if someone mass deletes let's say 100 records? What if those records will each have a different Quote?
How do I then pair the deleted record with the parent quote to evalute if the email should be sent based on Status or Name?
Status and Name.
trigger QuoteLineItemEmailBeforeDelete on QuoteLineItem (after delete) { Set<Id> quids = new Set<Id>(); for(QuoteLineItem qli :Trigger.Old) { quids.add(qli.QuoteId); } Quote[] quo = [Select Id, Name, Status, RecordTypeId from Quote Where id In :quids]; if((quo[0].Status == '2. Validated - Waiting for order' || quo[0].Status == '3. Offer partially entered in SAP' || quo[0].Status == '4. Offer fully entered in SAP')&& (Quo[0].Name.Contains('H3'))) { Messaging.reserveSingleEmailCapacity(trigger.size); List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>(); for (QuoteLineItem qli : Trigger.old) { Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); email.setToAddresses(new String[] {'becommingaprogrammer@example.com'}); email.setSubject('Oznámení o změně nabídky'); email.setPlainTextBody('Došlo ke změně nabídky ' + Quo[0].Name + '.\n\n' + 'Pro otevření záznamu v Salesforce klikněte na následující odkaz:\n\n' + 'https://sg--testing.lightning.force.com/lightning/r/Quote/' + quo[0].Id + '/view'); emails.add(email); } Messaging.sendEmail(emails); } }
The thing is I don't know how to bulkify the code. What if someone mass deletes let's say 100 records? What if those records will each have a different Quote?
How do I then pair the deleted record with the parent quote to evalute if the email should be sent based on Status or Name?
Try this:
trigger QuoteLineItemEmailBeforeDelete on QuoteLineItem(before insert) {
Set<Id> quids = new Set<Id>();
for(QuoteLineItem qli :Trigger.Old) {
quids.add(qli.QuoteId);
}
List<Quote> quo = new List<Quote>();
quo = [Select Id, Name, Status, RecordTypeId from Quote Where id In :quids];
for(Quote quLoop: quo){
if((quLoop.Status == '2. Validated - Waiting for order' || quLoop.Status == '3. Offer partially entered in SAP' ||
quLoop.Status == '4. Offer fully entered in SAP')&& (quLoop.Name.Contains('H3'))) {
Messaging.reserveSingleEmailCapacity(trigger.size);
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
for (QuoteLineItem qli : Trigger.old) {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
//you can add list of email ids below
email.setToAddresses(new String[] {'becommingaprogrammer@example.com'});
email.setSubject('Oznámení o změně nabídky');
email.setPlainTextBody('Došlo ke změně nabídky ' + quLoop.Name + '.\n\n' + 'Pro otevření záznamu v Salesforce klikněte na následující odkaz:\n\n' + 'https://sg--testing.lightning.force.com/lightning/r/Quote/' + quLoop.Id + '/view');
emails.add(email);
}
Messaging.sendEmail(emails);
}
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com