Search Here

When an opportunity line item is created an email should go to Opportunity Account Client Contact — Client Contact would be a field on Account lookup to contact.

 Email Template: Hi, Contact Name

Your Order has been proceeding.
The order detail is below.
Product Name: “Product Name”
Product Code: “Product Code”
Unit Price: “Unit Price”
List Price: “List Price”

Thanks
User First Name User Last Name

trigger sendEmailToContact on OpportunityLineItem (after insert){

Set<Id> oppId =new Set<Id>();

String userName;

Map<Id,Opportunity> oppIdConEmailMap = new Map<Id,Opportunity>();

for(OpportunityLineItem oppProd:Trigger.new){

oppId.add(oppProd.OpportunityId);

userName=oppProd.CreatedBy.Name;

}

List<Opportunity> oppList=[Select Id,Account.Client_Contact__r.Email,Account.Client_Contact__r.LastName,Account.Client_Contact__c From Opportunity where Id=:oppId];

if(oppList.size()>0){

for(Opportunity opp:oppList){

oppIdConEmailMap.put(opp.Id,opp);

}

}

List<Messaging.SingleEmailMessage> emailList= new List<Messaging.SingleEmailMessage>();

for(OpportunityLineItem oppProd:Trigger.new){

if(oppIdConEmailMap.containsKey(oppProd.OpportunityId)){

Opportunity oppObj=oppIdConEmailMap.get(oppProd.OpportunityId);

String sendTo=oppObj.Account.Client_Contact__r.Email;

if(sendTo!=null){

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

mail.setTargetObjectId(oppObj.Account.Client_Contact__c);

mail.setSenderDisplayName('System Administrator');

mail.setUseSignature(false);

mail.setBccSender(false);

mail.setSaveAsActivity(false);

mail.setSubject('New Opportunity Product was Created.');

String body = 'Dear '+oppObj.Account.Client_Contact__r.LastName+', <br/>';

body += 'Your Order has been proceed.<br/>';

body +='Order detail is below.<br/>';

body +='Product Name : '+oppProd.Name+'.<br/>';

body +='Product Code : '+oppProd.ProductCode+'.<br/>';

body +='Unit Price : '+oppProd.UnitPrice+'.<br/>';

body +='List Price : '+oppProd.ListPrice+'.<br/>';

body +='Thanks <br/>'+userName;

mail.setHtmlBody(body);

mail.toAddresses = new String[]{sendTo};

emailList.add(mail);

}

}

}

if(emailList.size()>0){

Messaging.SendEmailResult[] results = Messaging.sendEmail(emailList);

if (results[0].success)

{

System.debug('The email was sent successfully.');

} else {

System.debug('The email failed to send: '+ results[0].errors[0].message);

}

}

}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.