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
Jan Kopejtko 2Jan Kopejtko 2 

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.
 
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?
sachinarorasfsachinarorasf
Hi Jan,

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