Search code examples
javascriptsalesforce-lightning

Salesforce Lightning - JS- Syntax Issue


Using Salesforce lightning, I'm trying to finish my custom emailer component, but theres a problem with my syntax. I don't understand what the error is. Basically, this is the actual emailer, it will allow attachments and have scheduling functions. Basically I create the email component, add the attachment module, and schedule the email using flows

import { LightningElement} from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import sendEmail from '@salesforce/apex/Example1EmailSender.sendEmail';


export default class EmailerComponent3 extends LightningElement {

    toAddress;
    subject;
    body;
    cc;
    bcc;
    showSpinner = false;
    ccClicked = false;
    bccClicked = false;
    uploadedFiles;
    files;
    fileids;
    

    handleInputChange(event) {
        switch (event.target.name) {
            case 'toAddress': 
                this.toAddress = event.target.value;
            case 'cc':
                this.cc = event.target.value;
            case 'bcc':
                this.bcc = event.target.value;
            case 'subject':
                this.subject = event.target.value;
            case 'body': 
                this.body = event.target.value;
        }
    }

    handleEmailSend() {
        this.showSpinner = true;
        var fileIds = [];
        var contentVersionIds = [];
        this.files.forEach((file) => {
            contentVersionIds.push(file.contentVersionId);
        });
    
    }

    
        sendEmailer({

            'fileIds': fileIds,
            'ToAddress' : ToAddress,
            'cc' :  cc,
            'bcc' : bcc,
            'subject': subject, 
            'body': body,
            'contentVersionIds': contentVersionIds
        })
            .then(
                result: => {
                this:showSpinner = false,
                console.log('APEX RESPONSE: ' + result);
                this.showToast('success', 'Your email was sent successfully!', '');
                }
            
            .catch(
                error: => {
                this.showSpinner = false;
                console.log('error');
                console.log(error);
                this.showToast('error', 'Uh oh!', 'Ensure you have included a valid email address and content in the subject line and body of your email.');
            })

        
        }
    

    showToast(type, title, message) 
    {
        const event = new ShowToastEvent({
            title: title,
            message: message,
            variant: type
        });
        this.dispatchEvent(event);
    }

    ccButtonClicked() 
    {
        this.ccClicked = true;
    }
    
    bccButtonClicked() 
    {
        this.bccClicked = true;
    }

    handleUploadFinished(event) 
    {

    
    

        this.files = event.detail.files;
        console.log(this.files);

    }

Please see error message Error Message


Solution

  • this:showSpinner should probably be this.showSpinner line 59

    you also have issues when using .then and .catch like this: .then(result: => ...) when it should instead look like this: .then(result => ...) (remove the :)

    Also, you can't access this keyword inside a .then or .catch callback function. You probably want to use async/await with try/catch blocks. Here's an example of sendEmailer

    try {
          const result = await sendEmailer({
            fileIds: fileIds,
            ToAddress: ToAddress,
            cc: cc,
            bcc: bcc,
            subject: subject,
            body: body,
            contentVersionIds: contentVersionIds,
          });
          this.showSpinner = false;
          console.log("APEX RESPONSE: " + result);
          this.showToast("success", "Your email was sent successfully!", "");
        } catch (error) {
          this.showSpinner = false;
          console.log("error");
          console.log(error);
          this.showToast(
            "error",
            "Uh oh!",
            "Ensure you have included a valid email address and content in the subject line and body of your email."
          );
        }
    

    these are just basic javascript syntax issues... does your ide not pick it up and warn you?