Objective:

In this blog, you’ll learn how to send QR codes via email, an essential feature for seamless business processes. We’ll cover how to send QR codes for both standard and custom objects.

Previously, I explained various methods to create a QR code. However, I have updated the blog to note that Google has deprecated its API for generating QR codes. Fortunately, there are alternative methods available. Please refer to my previous blog for more details.

Approach:

For standard objects, we’ll use email alerts to send emails with QR codes to contacts. For custom objects, we’ll use batch Apex to send the emails. Let’s explore both approaches in detail.

Scenario: We are organizing an event and need to implement a QR code check-in system for all attendees. Each attendee should receive a unique QR code to facilitate their check-in process.

Sending QR Codes for Standard Objects

To send QR codes via email for standard objects, follow these steps:

  1. Create a Formula Field to Generate QR Code:
    Please refer to my previous blog for generating a QR code. – QR Code Generation Blog
  2. Create an Email Template:
    a. Go to Setup.
    b. Search for “Classic Email Templates” and click on “New Template.”
    c. Select the “Custom” option.



    Fill in the required fields:
    a. Select the Folder.
    b. Check “Available for Use.”
    c. Give a proper Email Template Name.
    d. Add a Subject for your email.
    Click on “Next.”



  3. Add HTML Body:
    a. In the HTML body, add your HTML code to attach the QR code in the email.
    b. Use merge fields to insert attendee name and other details.
    c. Click on “Next.”
    d. Click on “Copy text from HTML version.”
    e. Click on “Save.”




  4. Create an Email Alert:
    a. Go to Setup -> search “Email Alerts.”
    b. Click on “Email Alerts.”
    c. Click on “New Email Alert.”
    d. Select the Object and add the email field from that object where you want to send the email.



    You can use Flow or Batch Apex to use this email alert to send the QR code to the attendee. Below, I explain how to send it using batch Apex for the custom object.

Sending QR Code for Custom Object

Note: Email templates can take only standard objects like Contact, Lead, User.

  1. Write a Batch Apex Class:
    a. Create a Batch Apex class to process the records of the custom object and send the email.
    b. Ensure the user running the batch job has the appropriate permissions to send emails and access the custom object records.

Example Batch Apex Class:

public class QRCodeEmailSendBatch implements Database.Batchable < SObject > {
    public void execute(Database.BatchableContext bc, List < Attendee__c > 
    scope) {
        List < Attendee__c > attendeesToUpdate = new List < Attendee__c > ();
        List < Messaging.SingleEmailMessage > emails = new
        List < Messaging.SingleEmailMessage > ();
        for (Attendee__c attendee: scope) {
            if (attendee.Email__c != null) {
                String emailMsgBody = '<html><body>' +
                '<p>Dear ' + attendee.Name + ',</p>' +
                '<p>We are excited to welcome you to Salesforce DreamIn 
                 Event. To streamline your check-in process, we have 
                 generated a unique QR code for you.</p>' +
                '<p>Please find your QR code below:</p>' +
                '<p><img src="' + attendee.QR_Code_Url__c + '" alt="QR
                 Code" /></p>' +
                '<p><strong>How to Use Your QR Code:</strong></p>' +
                '<p>Save the QR Code: Save this email or the QR code 
                 image to your mobile device, or print it out to bring 
                 with you.</p>' +
                '<p>Present at Check-In: When you arrive at the event, 
                 present your QR code at the check-in counter for a 
                 quick and seamless entry.</p>' +
                '<br>' +
                '<p>Thanks & Regards,</p>' +
                '<p>Neha Rananaware</p>' +
                '</body></html>';

            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();                 
                email.setTreatTargetObjectAsRecipient(false);
                email.setSenderDisplayName('Neha Rananaware');
                email.setSubject('Check-In QR Code for the Upcoming Event');
                email.setToAddresses(new String[] {
                   attendee.Email__c
                });
                email.setHtmlBody(emailMsgBody);
                emails.add(email);
                attendee.QRCodeEmailSend__c = true;
                attendeesToUpdate.add(attendee);
            }
        }

        // Send the email messages in bulk
        if (!emails.isEmpty()) {
            try {
                Messaging.sendEmail(emails);
            } catch (Exception e) {
                // Handle email sending errors
                System.debug('Error sending emails: ' + e.getMessage());
            }
        }

        // Update the Attendee records
        if (!attendeesToUpdate.isEmpty()) {
            try {
                update attendeesToUpdate;
            } catch (DmlException e) {
                // Handle DML errors
                System.debug('Error updating attendee records: ' + e.getMessage());
            }
        }
    }

    public void finish(Database.BatchableContext bc) {
        // Optional: Add any post-processing logic here.
    }
}
  1. Test the Batch Apex:
    • Test the Batch Apex to ensure it works correctly.
    • Create a test class to verify the functionality

By following these steps, you can efficiently send QR codes through email for both standard and custom objects in Salesforce. This feature will enhance the check-in process for events, making it seamless and efficient for attendees.