function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MargiroMargiro 

Send Email Using the Save Button

I have a basic email being sent using this code:

public PageReference step3() {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{'margiro@piab.com'};
mail.setToAddresses(toAddresses);
mail.setReplyTo('support@acme.com');
mail.setSenderDisplayName('Piab Support');
mail.setSubject('New Conveyor Order');
mail.setBccSender(false);
mail.setUseSignature(false);
String msg = 'new conveyor ordered';
mail.setHtmlBody(msg+ lead.company);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
return Page.mhformpt3422;
}

 There are two things i need to do to it to make it work more efficient.

  1. I need this email code to work for the save button not the custom step3 button that I have it working on now.        .
  2. I also would like the email to return the msg string plus either the id of the lead with a hyperlink to it on the site(prefered ove other option) or the company of the lead that was created.(I thought that this is set up correctly in the code now but it does not return a value)

Any help is greatly appreciated!

 

Thanks,

Matt

Message Edited by Margiro on 05-28-2009 01:02 PM
sfdcfoxsfdcfox

To send an email on standard save, you could use a workflow rule with an email template that has merge fields to place the desired information into an email. This method would be preferred if at all possible. Otherwise, you should consider writing your code as a trigger instead; triggers execute when the standard save() function is invoked.

 

Either approach will likely solve your second problem, which probably has to do with the fact that the record hasn't been saved yet, and therefore does not have an ID associated with it.

MargiroMargiro
I wrote a trigger for the code but I dont know how to make it send values from the new lead in the email body...Any help?

trigger mhform on Lead (after insert) {
Messaging.SingleEmailMessage mail= new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{'margiro@piab.com'};
mail.setToAddresses(toAddresses);
mail.setReplyTo('support@acme.com');
mail.setSenderDisplayName('Piab Support');
mail.setSubject('New Conveyor Order');
mail.setBccSender(false);
mail.setUseSignature(false);\

String msg = 'new conveyor ordered';

mail.setHtmlBody(msg);

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail});
}

 

Message Edited by Margiro on 06-02-2009 02:00 PM