You need to sign in to do that
Don't have an account?
can we send the email by using Messaging.SingleEmailMessage and keep OpportunityOwner as From Address in Salesforce
Hello,
I have a scenario. I have developed the LWC component in which Send Email button is present. When User Mark the Opportunity as "Indent" the modal popup appears on screen and when user click on Send Email Indented OppDetails are sent to receipients group Email Id. Here currently Organisation Wide Email Address Name will be displayed and email Id will be displayed as "From" address.
I wanted to send the email by Opportunity Owner Name i.e. Opportunity Owner Name should be displayed as "From" Address. Below is my Apex Class Please Provide Solution asap.
public class DealWonVerification {
public List<Opportunity> Oppdetails { get; private set; }
public List<OpportunityLineItem> OppProds { get; private set; }
public List<Order> OppOrders { get; private set; }
@AuraEnabled(cacheable=true)
public static List<Opportunity> getOppDetails(String OppId){
List<Opportunity> Oppdetails = [SELECT Id, Name, Owner.Name, Account.Name, CloseDate, Amount, StageName, Remark_by_SCM__c, Remarks_by_the_Sales__c
FROM Opportunity WHERE Id = :OppId];
return Oppdetails;
}
@AuraEnabled(cacheable=true)
public static List<OpportunityLineItem> getOpportunityLineItems(String OppId) {
return [SELECT Id, Product2.Description, Quantity, UnitPrice, TotalPrice, Colour__c, Customization__c, Packing__c, New_Code_Required__c
FROM OpportunityLineItem WHERE OpportunityId = :OppId];
}
@AuraEnabled(cacheable=true)
public static List<Order> getOrdersdetails(String OppId) {
return [SELECT Id, OrderNumber, Quantity__c, Sales_Price__c, TotalAmount, EffectiveDate, Division_Code__c, EndDate
FROM Order WHERE OpportunityId = :OppId];
}
@AuraEnabled
public static void SaveOppData(String OppId, String remarksbyscm, String remarksbysales){
Opportunity Opp = [SELECT Id, Remark_by_SCM__c, Remarks_by_the_Sales__c FROM Opportunity WHERE Id = :OppId];
Opp.Remark_by_SCM__c = remarksbyscm;
Opp.Remarks_by_the_Sales__c = remarksbysales;
update Opp;
}
@AuraEnabled
public static void toSendEmailWithTemplate(String OppId){
List<Order> OppOrder_s = [SELECT Id, OrderNumber, Quantity__c, Sales_Price__c, TotalAmount, EffectiveDate, Division_Code__c, EndDate
FROM Order WHERE OpportunityId = :OppId];
List<Opportunity> OppOwner = [SELECT OwnerId, Owner.Email, Owner.Name FROM Opportunity WHERE Id = :OppId];
String oppMail = OppOwner.isEmpty() ? null : OppOwner[0].OwnerId;
String OppOwnerEmail = OppOwner.isEmpty() ? null : OppOwner[0].Owner.Email;
System.debug('oppMail'+oppMail);
System.debug('OppOwnerEmail'+OppOwnerEmail);
Boolean hasHLProduct = false;
Boolean hasNonHLProduct = false;
List<Messaging.Email> emailsToSend = new List<Messaging.Email>();
for(Order o : OppOrder_s) {
if(o.Division_Code__c == 'HL') {
hasHLProduct = true;
System.debug('HL Ordered Product Order Number => '+ o.OrderNumber);
} else if (o.Division_Code__c != 'HL') {
hasNonHLProduct = true;
System.debug('Non HL Ordered Product Order Number => '+o.OrderNumber);
}
}
if (hasHLProduct && hasNonHLProduct){
// Both HL and Non-HL products
sendEmail('pranav.chavan181@gmail.com', 'Deal Won Alert_HL', oppMail, OppId, emailsToSend);
sendEmail('pranav.chavan@kriosispl.in', 'Deal Won Alert_NonHL', oppMail, OppId, emailsToSend);
} else if (hasHLProduct) {
// Only HL products
sendEmail('pranav.chavan181@gmail.com', 'Deal Won Alert_HL', oppMail, OppId, emailsToSend);
} else if (hasNonHLProduct) {
// Only Non-HL products
sendEmail('pranav.chavan@kriosispl.in', 'Deal Won Alert_NonHL', oppMail, OppId, emailsToSend);
}
// Send all the emails together
List<Messaging.SendEmailResult> sendResults = Messaging.sendEmail(emailsToSend);
// Check if there are any email sending failures
for (Messaging.SendEmailResult result : sendResults) {
if (!result.isSuccess()) {
// Handle the error (e.g., log the error, display a message to the user, etc.)
System.debug('Email sending failed with error: ' + result.getErrors());
}
}
}
private static void sendEmail(String recipientEmail, String emailTemplateName, String targetObjectId, String whatId, List<Messaging.Email> emailsToSend) {
System.debug(recipientEmail +emailTemplateName+ targetObjectId+ whatId );
Messaging.SingleEmailMessage emailMessage = new Messaging.SingleEmailMessage();
Emailtemplate emailTemplate = [SELECT Id, Subject, Body FROM EmailTemplate WHERE Name = :emailTemplateName];
emailMessage.setToAddresses(new List<String>{recipientEmail});
emailMessage.setTemplateId(emailTemplate.Id);
emailMessage.setTargetObjectId(targetObjectId);
emailMessage.setWhatId(whatId);
emailMessage.setHTMLBody(emailTemplate.Body);
emailMessage.setSaveAsActivity(false);
OrgWideEmailAddress[] owea = [SELECT Id FROM OrgWideEmailAddress WHERE Address = 'ajay.joshi@kriosispl.in' LIMIT 1];
if (owea.size() > 0) {
emailMessage.setOrgWideEmailAddressId(owea[0].Id);
}
emailsToSend.add(emailMessage);
}
@AuraEnabled
public static void SaveRecord(List<ModifiedRecord> modifiedRecords) {
Set<Id> oliIds = new Set<Id>();
Map<Id, OpportunityLineItem> oliMap = new Map<Id, OpportunityLineItem>();
for (ModifiedRecord record : modifiedRecords) {
oliIds.add(record.rowId);
}
if (!oliIds.isEmpty()) {
oliMap = new Map<Id, OpportunityLineItem>([SELECT Id, Colour__c, Customization__c, Packing__c, New_Code_Required__c
FROM OpportunityLineItem WHERE Id IN :oliIds]);
}
for (ModifiedRecord record : modifiedRecords) {
OpportunityLineItem oli = oliMap.get(record.rowId);
if (oli != null) {
if (record.fieldName == 'Colour') {
oli.Colour__c = record.modifiedValue;
} else if (record.fieldName == 'Customization') {
oli.Customization__c = record.modifiedValue;
} else if (record.fieldName == 'Packing') {
oli.Packing__c = record.modifiedValue;
} else if (record.fieldName == 'NewCode') {
oli.New_Code_Required__c = record.modifiedValue;
}
}
}
if (!oliMap.isEmpty()) {
update oliMap.values();
}
}
public class ModifiedRecord {
@AuraEnabled
public String rowId { get; set; }
@AuraEnabled
public String fieldName { get; set; }
@AuraEnabled
public String modifiedValue { get; set; }
}
}
But Instead of this I wanted to keep Opportunity Owner Name as From
To send the email from the "DealWonVerification" Apex class using the Opportunity Owner's name as the "From" address, you'll need to modify the code to fetch the Opportunity Owner's email address and use it in the "sendEmail" method. Here's how you can do it:
Modify the "toSendEmailWithTemplate" method to fetch the Opportunity Owner's email address: Update the "sendEmail" method to use the Opportunity Owner's email address as the "From" address:
With these changes, the "toSendEmailWithTemplate" method will now use the Opportunity Owner's email address as the "From" address when sending the email. Make sure to deploy the updated Apex class to your Salesforce org to apply the changes.
Hope this helps !
Thank you.