You need to sign in to do that
Don't have an account?
Indianajones
help me please with single email
I created a lwc component that sends information about Opportunity.
The problem is that the data is written to Single Email only if I use 4 functions.
1. handleToNameReceiverChange
2. handleToEmailAddressChange
3. handleToEmailSubjectChange
4. handleToEmailBodyChange
They work so that if I change the value of the field manually, the data is put into Email. If I don't change them, I get an undefined error and that the body is required in Email.
If I change the data in the fields manually, such as writing forexample , it works fine.
I want the fields to be taken as I specified in the queries, so that the Name of receiver, email, email subject were readonly and entered automatically in a single email. And the body remains editable.
Help me please.
SendInvoiceService.apxc
The problem is that the data is written to Single Email only if I use 4 functions.
1. handleToNameReceiverChange
2. handleToEmailAddressChange
3. handleToEmailSubjectChange
4. handleToEmailBodyChange
They work so that if I change the value of the field manually, the data is put into Email. If I don't change them, I get an undefined error and that the body is required in Email.
If I change the data in the fields manually, such as writing forexample , it works fine.
I want the fields to be taken as I specified in the queries, so that the Name of receiver, email, email subject were readonly and entered automatically in a single email. And the body remains editable.
Help me please.
SendInvoiceService.apxc
public with sharing class SendInvoiceService { // @AuraEnabled // public static List<ContentVersion> getPreview(id oppIds) { // Opportunity oppor = [SELECT Id, Invoice_Number__c From Opportunity where id=:oppIds]; // return [SELECT Id, Title, FileExtension, ContentDocumentId From ContentVersion where title=:oppor.Invoice_Number__c]; // } @AuraEnabled public static List<ContentDocument> getPreview(id oppIds) { try { Opportunity Opportunitys = [ select id,Invoice_Number__c from Opportunity where id=:oppIds]; List<ContentVersion> cvs = [SELECT Id, Title, FileExtension, ContentDocumentId From ContentVersion where Title=:Opportunitys.Invoice_Number__c]; return [select Id from ContentDocument where LatestPublishedVersionId in :cvs]; } catch (Exception e) { throw new AuraHandledException(e.getMessage()); } } @AuraEnabled(cacheable=true) public static SendInvoice__c getInvoice(id oppId) { Opportunity Opportunity=[select id,Invoice_Number__c, (select IsPrimary,Contact.Email,Contact.AccountId,Contact.FirstName from OpportunityContactRoles where IsPrimary=true) from Opportunity where id=:oppId]; OpportunityContactRole oppCR=Opportunity.OpportunityContactRoles; SendInvoice__c invoice=new SendInvoice__c(); invoice.Body__c=[select body from EmailTemplate where name='OppTextAndr'].body+' body'; invoice.Subject__c=Opportunity.Invoice_Number__c; invoice.Email__c=oppCR.Contact.Email; invoice.FirstName__c=oppCR.Contact.FirstName; system.debug(invoice.Body__c+' '+invoice.Subject__c+' '+invoice.Email__c+' '+invoice.FirstName__c +' '+invoice.LastName__c+' '+invoice.AttachId__c); return invoice; } @AuraEnabled public static void sendEmail(List<String> toAddress, String subject, String body, String name, id oppId) { Messaging.reserveSingleEmailCapacity(1); try{ messaging.SingleEmailMessage mail = new messaging.SingleEmailMessage(); System.debug(toAddress); System.debug(body); System.debug(name); System.debug(subject); mail.setToAddresses(toAddress); mail.setReplyTo('no-reply@xooa.com'); mail.setSenderDisplayName(name); mail.setSubject(subject); mail.setPlainTextBody(body); Messaging.sendEmail(new List<messaging.SingleEmailMessage> {mail}); } catch (exception e){ throw new AuraHandledException(e.getMessage()); } } }SendInvoiceLWC.html
<template> <lightning-card title="Send Invoice" > <lightning-input type="name" name="nameReceiver" label="Name of Receiver" onchange={handleToNameReceiverChange} value={realFormData.FirstName__c} if:true={realFormData} ></lightning-input> <lightning-input type="email" name="emailAddress" label="Email" onchange={handleToEmailAddressChange} value={realFormData.Email__c} if:true={realFormData} ></lightning-input> <lightning-input type="emailsubject" name="emailSubject" onchange={handleToEmailSubjectChange} label="EmailSubject" value={realFormData.Subject__c} if:true={realFormData}></lightning-input> <lightning-input type="emailbody" name="emailBody" onchange={handleToEmailBodyChange} label="EmailBody" value={realFormData.Body__c} if:true={realFormData} ></lightning-input> <lightning-button label="Show me PDF" onclick={openPDF}></lightning-button> <lightning-button label="send Email" onclick={sendEmailHandler}></lightning-button> </lightning-card> </template>sendInvoiceLWC.js
import { LightningElement, api, wire, track } from 'lwc'; import getInvoice from '@salesforce/apex/SendInvoiceService.getInvoice'; import getPreview from '@salesforce/apex/SendInvoiceService.getPreview'; import sendEmail from '@salesforce/apex/SendInvoiceService.sendEmail'; import NAME_FIELD from '@salesforce/schema/SendInvoice__c.FirstName__c'; import SUBJECT_FIELD from '@salesforce/schema/SendInvoice__c.Subject__c'; import EMAIL_FIELD from '@salesforce/schema/SendInvoice__c.Email__c'; import BODY_FIELD from '@salesforce/schema/SendInvoice__c.Body__c'; import { getSObjectValue } from '@salesforce/apex'; import { NavigationMixin } from 'lightning/navigation'; export default class sendInvoiceLWC extends NavigationMixin(LightningElement) { @track contents; @track error; @api recordId; @track email = ''; @api wiredContact; @api realFormData; @api InvoiceObj; @api wiredContent; @api realFormContent; @api PreviewObj; @track content; @track error; @api contentVId; connectedCallback() { getPreview({ oppIds: this.recordId }) .then((result) => { this.contents = result; this.error = undefined; }) .catch((error) => { this.error = error; this.contents = undefined; }); } openPDF() { this[NavigationMixin.Navigate]({ type: 'standard__namedPage', attributes: { pageName: 'filePreview' }, state : { recordIds: '0695j000007MhNrAAK,0695j000007MhNrAAK', selectedRecordId:'0695j000007MhNrAAK' } }) } @wire(getInvoice, { oppId:'$recordId' }) fetchedInvoice( resp){ this.wiredContact = resp; this.realFormData = this.wiredContact.data; this.InvoiceObj=resp; } get Subject() { return this.InvoiceObj.data?getSObjectValue(this.InvoiceObj.data,SUBJECT_FIELD,EMAIL_FIELD,BODY_FIELD,NAME_FIELD):'nothing'; } handleToNameReceiverChange(event) { if (event.target.name === 'nameReceiver') { this.name = event.target.value; } } handleToEmailAddressChange(event) { if (event.target.name === 'emailAddress') { this.email = event.target.value; } } handleToEmailSubjectChange(event) { if (event.target.name === 'emailSubject') { this.emailsubject = event.target.value; } } handleToEmailBodyChange(event) { if (event.target.name === 'emailBody') { this.emailbody = event.target.value; } } sendEmailHandler(event) { console.log("Sending email to", this.email); console.log("Email subject this subject", this.emailsubject); console.log("Email body this body", this.emailbody); sendEmail({ toAddress: this.email, subject: this.emailsubject, name: this.name, body: this.emailbody}); } }xml
<?xml version="1.0" encoding="UTF-8"?> <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>52.0</apiVersion> <isExposed>true</isExposed> <targets> <target>lightning__RecordAction</target> </targets> <targetConfigs> <targetConfig targets="lightning__RecordAction"> <actionType>ScreenAction</actionType> </targetConfig> </targetConfigs> </LightningComponentBundle>help me please!!!