0% found this document useful (0 votes)
12 views2 pages

CampaignEmailSender Js

The document is a JavaScript file for a Lightning Web Component that facilitates sending campaign emails in Salesforce. It includes functionality to fetch email templates, display template details, and send both regular and preview emails. The component handles user interactions, such as selecting templates and modifying email subjects and bodies, while providing feedback through toast messages for success or error events.

Uploaded by

timex8055
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

CampaignEmailSender Js

The document is a JavaScript file for a Lightning Web Component that facilitates sending campaign emails in Salesforce. It includes functionality to fetch email templates, display template details, and send both regular and preview emails. The component handles user interactions, such as selecting templates and modifying email subjects and bodies, while providing feedback through toast messages for success or error events.

Uploaded by

timex8055
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

//campaignEmailSender.

js
import { LightningElement, wire, track, api } from 'lwc';
import getEmailTemplates from
'@salesforce/apex/CampaignEmailSenderController.getEmailTemplates';
import getEmailTemplateDetails from
'@salesforce/apex/CampaignEmailSenderController.getEmailTemplateDetails';
import sendEmails from '@salesforce/apex/CampaignEmailSenderController.sendEmails';
import sendPreviewEmails from
'@salesforce/apex/CampaignEmailSenderController.sendPreviewEmails';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { CloseActionScreenEvent } from 'lightning/actions';

export default class CampaignEmailSender extends LightningElement {


@api recordId; // Campaign Id passed to the component
@track templateOptions = [];
selectedTemplate = '';
emailSubject = '';
emailBody = '';

@wire(getEmailTemplates)
templates({ error, data }) {
if (data) {
this.templateOptions = data.map(template => ({
label: template.Name,
value: template.Id
}));
} else if (error) {
console.error(error);
}
}

handleTemplateChange(event) {
this.selectedTemplate = event.detail.value;

// Fetch template details (subject and body) when a template is selected


getEmailTemplateDetails({ templateId: this.selectedTemplate, campaignId:
this.recordId })
.then((result) => {
console.log(result);
this.emailSubject = result.Subject;
this.emailBody = result.body;
})
.catch(error => {
console.error(error);
this.showToast('Error', 'Failed to load template details',
'error');
});
}

handleSubjectChange(event) {
this.emailSubject = event.target.value;
}

handleBodyChange(event) {
this.emailBody = event.target.value;
}

sendEmails() {
sendEmails({
campaignId: this.recordId,
templateId: this.selectedTemplate,
subject: this.emailSubject,
body: this.emailBody
})
.then(() => {
this.showToast('Success', 'Emails sent successfully', 'success');
this.dispatchEvent(new CloseActionScreenEvent());
})
.catch(error => {
console.error(error);
this.showToast('Error', 'Failed to send emails', 'error');
this.dispatchEvent(new CloseActionScreenEvent());
});
}

showToast(title, message, variant) {


const evt = new ShowToastEvent({
title,
message,
variant
});
this.dispatchEvent(evt);
}

sendPreviewEmails() {
sendPreviewEmails({
campaignId: this.recordId,
templateId: this.selectedTemplate,
subject: this.emailSubject,
body: this.emailBody
})
.then(() => {
this.showToast('Success', 'Preview emails sent successfully',
'success');
})
.catch(error => {
console.error(error);
this.showToast('Error', 'Failed to send preview emails', 'error');
});
}
}

You might also like