1

I have custom table in LWC

HTML

<td>
<div>
<label>
<lightning-input type="checkbox" ></lightning-input>
</label>
</div>
</td>

on click of the checkbox, I want to get the List of ids of the checked rows and pass it to the below apex class

APEX:

public class SendEmailClass{

@Auraenabled
public static void sendEmail(List<Id> Ids){
// i will fetch the record data using ids from the LWC and send it to vf page to create a an attachment

//Send Email to users with the attachment created by vf page
}
}

Can anyone help me with passing the data and sending that passed data as an email?

5
  • Are you saying that every time they click on the checkbox you want apex to send an email? So if a user is to click on 3 checkboxes you will trigger the email process 3 times sending 3 emails to the first selection, 2 to the second and 1 to the last? I am a little confused.
    – Arthlete
    Jul 2 '20 at 15:48
  • I have the given table with checkbox so when the user click the checkbox or multiple checkboxes, then I have a button which onclick should send email to the user with the data from the checked rows as an attachment.
    – SFDC IN
    Jul 2 '20 at 15:59
  • Okay so you don't want to send an email when the user clicks the checkbox but when the user clicks the button to send the email, is that correct?
    – Arthlete
    Jul 2 '20 at 16:04
  • @Arthlete.. yes correct! the user will select the rows and when he clicks on the button then the email should be sent with the rows data as an attachment.
    – SFDC IN
    Jul 2 '20 at 16:22
  • Okay I will post an answer in a minute :)
    – Arthlete
    Jul 2 '20 at 16:26
1

Your best bet is to use a lightning-datatable component. It provides selection functionality out of the box. You just need to configure the columns array and data elements as shown in the first example in the documentation here: https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/documentation.

Just leave the hide-checkbox-column attribute off the invocation.

When you want to get the data use

this.template.querySelector('lightning-datatable').getSelectedRows();

As for the sending of the email attachment, you can use SingleEmailMessage with a Visualforce email template to execute that method. The messaging:attachment component can be used in the template to define your attachment body (or a VF component to be more easily controlled). https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_messaging_attachment.htm

Remember that SingleEmailMessage has a rate limit of < 1000 records per day. You may need to use an intermediary step if the volume is going to be high.

1

I have put a playground together of how you can gather the ids of the selected checkboxes on the click of a button. You can take a look at the playground here.

I will post the code here as well.

HTML:

<template>
    <template for:each={myCollection} for:item='item'>
        <lightning-input class="input-checkbox" label={item.label} key={item.id} type="checkbox" data-id={item.id}></lightning-input>
    </template>
    <button onclick={handleClick}>Click me!</button>
</template>

JS:

@track myCollection = [
        {id: '1', label: 'One'},
        {id: '2', label: 'Two'},
        {id: '3', label: 'Three'},
        {id: '4', label: 'Four'}
    ];

    handleClick(evt) {
        let allCheckboxes = this.template.querySelectorAll('.input-checkbox');
        let idsToEmail = [];
        for (let aCheckbox of allCheckboxes) {
            if (aCheckbox.checked === true) {
                idsToEmail.push(aCheckbox.dataset.id);
            }
        }
        console.log(idsToEmail);
    }

I cannot connect to an APEX method from the playground so I will expand on the JS code here. What you want is to import the APEX method you need to and then pass the ids once you put the array together.

import sendEmails from '@salesforce/apex/YourClassName.yourMethodNameToSendEmails";

@track myCollection = [
    {id: '1', label: 'One'},
    {id: '2', label: 'Two'},
    {id: '3', label: 'Three'},
    {id: '4', label: 'Four'}
];
    
handleClick(evt) {
    let allCheckboxes = this.template.querySelectorAll('.input-checkbox');
    let idsToEmail = [];
    for (let aCheckbox of allCheckboxes) {
       if (aCheckbox.checked === true) {
           idsToEmail.push(aCheckbox.dataset.id);
       }
    }
    this.handleSendEmails(idsToEmail);
}
handleSendEmails(idsToEmail) {
    sendEmails({idsToEmail: idsToEmail})
        .then(result => {
            if (result === true) {
                // everything went ok, handle the success of the operation.
            }
        })
        .catch(error => {
            //handle your error
        });
}
8
  • Thanks @Arthlete. that helped. How to send the selected data as attachment in Email ?
    – SFDC IN
    Jul 2 '20 at 17:53
  • The attachment has to be a file. Your selected data is in a string format. What exactly is it that you are trying to attach to the email?
    – Arthlete
    Jul 2 '20 at 17:57
  • the data in an excel file.
    – SFDC IN
    Jul 2 '20 at 18:03
  • So you need APEX to put an excel file together from what you send it from the JS side and attach that to the email you are sending?
    – Arthlete
    Jul 2 '20 at 18:04
  • 1
    No problem! Thanks for your time
    – SFDC IN
    Jul 2 '20 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.