How to Auto-Populate To Address in Send Email

How to Auto-Populate “To Address” in Send Email?

Spread the love

Question

I have a requirement to create a Quick Action button on the standard Case object to send emails. The “To Address” field should always auto-populate with a predefined email address, regardless of previous emails sent from the case.

To achieve this, I created an object-specific Quick Action (Send Email) and set the “To Address” field with a predefined default email address. Additionally, I created a custom Quick Action that invokes a headerless LWC component. In the invoke() method, I redirect to the standard Send Email Quick Action and pass default values using the NavigationMixin as shown below:

import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';

export default class CustomSendEmail extends NavigationMixin(LightningElement) {
    @api recordId;
    predefinedEmail = 'default@example.com'; // Replace with your required email

    @api invoke() {
        var pageRef = {
            type: "standard__quickAction",
            attributes: {
                apiName: "Case.SendEmail",
            },
            state: {
                recordId: this.recordId,
                defaultFieldValues: this.encodeValues()
            },
        };
        this[NavigationMixin.Navigate](pageRef);
    }

    encodeValues() {
        return encodeDefaultFieldValues({
            ToAddress: this.predefinedEmail,
            Subject: "Predefined Subject", // Optional
            HTMLBody: "Default email content" // Optional
        });
    }
}

1. Custom LWC Quick Action (Recommended Solution)

The best way to ensure the To Address field always defaults to a predefined email is by using a Custom LWC Quick Action. This allows complete control over the email composition process while ensuring consistency.

How It Works:

Instead of directly using the standard Send Email Quick Action, we create an LWC Quick Action that:

  • Retrieves the predefined email address dynamically or from a hardcoded value.
  • Navigates to the standard Send Email Quick Action but explicitly sets the “To Address” field every time it is invoked.
  • Prevents the previously used recipient from being retained.

Code Implementation:

import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { encodeDefaultFieldValues } from 'lightning/pageReferenceUtils';

export default class CustomSendEmail extends NavigationMixin(LightningElement) {
    @api recordId;
    predefinedEmail = 'default@example.com'; // Replace with your predefined email

    @api invoke() {
        var pageRef = {
            type: "standard__quickAction",
            attributes: {
                apiName: "Case.SendEmail",
            },
            state: {
                recordId: this.recordId,
                defaultFieldValues: this.encodeValues()
            },
        };
        this[NavigationMixin.Navigate](pageRef);
    }

    encodeValues() {
        return encodeDefaultFieldValues({
            ToAddress: this.predefinedEmail,
            Subject: "Predefined Subject", // Optional
            HTMLBody: "Default email content" // Optional
        });
    }
}

Why This Works:

  • Every time the Quick Action is used, NavigationMixin.Navigate ensures that the “To Address” field is reset to the predefined email.
  • The defaultFieldValues override any cached email recipient from previous sends.
  • Users cannot manually change the behavior since the predefined email is passed dynamically within the Quick Action.

2. Using a Salesforce Flow (Declarative Approach – No Code Solution)

If you prefer a no-code solution, you can achieve a similar result using Salesforce Flow instead of an LWC component.

How It Works:

  1. Create a Screen Flow that contains a Send Email action.
  2. Set the To Address field to your predefined email address.
  3. Add the Flow as a Quick Action on the Case object.

Steps to Implement:

  1. Go to Setup → Process Automation → Flows and create a Screen Flow.
  2. Add a Send Email action in the Flow and configure:
    • Recipient Type: “Email Address”
    • To Address: Set to the predefined email address.
    • Subject/Body: (Optional) Add predefined values if needed.
  3. Save and Activate the Flow.
  4. Go to Case Object → Buttons, Links, and Actions and create a Quick Action to launch the Flow.
  5. Add the Quick Action to the Case Page Layout.

Why This Works:

  • Unlike the standard Quick Action, Flows do not retain previous email recipients unless explicitly configured.
  • The email will always be sent to the predefined email without any risk of caching the previous recipient.
  • This method is ideal for users who prefer Salesforce’s declarative tools over custom development.

3. Clearing the “To Address” Field via JavaScript (Not Recommended)

It is possible to use JavaScript inside an Aura Component to clear the “To Address” field before launching the standard Send Email Quick Action. However, this approach is not recommended because:

  • Salesforce’s standard Quick Action interface is not fully accessible for direct DOM manipulation.
  • Any JavaScript-based modifications can be overwritten by future Salesforce updates, making the solution unreliable.
  • JavaScript in Aura Components can introduce security vulnerabilities and maintenance challenges.

For these reasons, it is best to use either Custom LWC Quick Action or Salesforce Flow to achieve the required behavior.

Final Recommendation

The best solution depends on your business needs and technical expertise:

  • If you want full control over the email process and prefer code-based solutions, the Custom LWC Quick Action is the best approach.
  • If you prefer a no-code declarative solution, use Salesforce Flow to send emails with a predefined recipient.
  • Avoid using JavaScript-based solutions as they are unreliable and can break with Salesforce updates.

Outcome:

By implementing either the LWC Quick Action or the Flow-based approach, you can ensure that the “To Address” field always defaults to the predefined email address, regardless of the number of emails sent from the same case.

Job-Oriented Salesforce Training with 100% Money Back Assurance

Our Salesforce Course is structured to provide a comprehensive understanding of the Salesforce platform, equipping you with the essential skills needed to excel in the CRM industry. The program covers important modules like Salesforce Admin, Developer, and AI, seamlessly integrating theoretical concepts with hands-on application. Through practical assignments and real-world projects, you will develop the expertise to solve complex business challenges using Salesforce solutions. Our experienced instructors ensure that you gain both technical proficiency and industry-relevant insights to succeed in the Salesforce ecosystem.

Beyond technical expertise, our Salesforce training in Hyderabad includes personalized mentoring, certification preparation, and interview coaching to enhance your career opportunities. You’ll have access to extensive study materials, live project exposure, and dedicated support throughout your learning journey. By the end of the course, you will be fully prepared for certification exams and possess the real-world skills that employers seek. Start your Salesforce career with us and explore limitless career possibilities. Sign up for a Free Demo today!

Open Chat
1
Dear Sir/Madam
How can I help you?