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 

Email Template help

I'm trying to create an email that is sent automatically when a form is completed.Here's what my controller for the form I'm working on looks like:

public class GLeadExtension {
public Lead aLead ;
public GLeadExtension(ApexPages.StandardController controller) {
aLead = new Lead() ;
}
public PageReference step1() {
return Page.mhform422;
}
public PageReference step2() {
return Page.mhformpt2422;
}
public PageReference step3() {
return Page.mhformpt3422;
}
public PageReference step4() {
return Page.mhformconfirm;
}

public PageReference step5(string emailTo) {
return Page.mhformendpage;
}
public PageReference home() {
return Page.FileNotFound;
}

public static void send (string emailTo){
String[] toAddresses = new String[] {'margiro@piab.com'};
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

mail.setToAddresses(toAddresses);
mail.setReplyTo('support@acme.com');
mail.setSenderDisplayName('Piab Support');
mail.setSubject('New Case Created');
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setPlainTextBody('An order for a conveyor has been created');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

public PageReference save() {
PageReference pr ;
try{
//ZillowService p = new ZillowService() ;
// make the web service call out
//ZillowTypes.PropertySearchResponse r = p.searchZillow( aLead.Street, aLead.City, aLead.State) ;
// store in custom field
//aLead.Home_Value__c = r.getZEstimateAmount() ;
// insert new record into DB
insert aLead ;
// redirect to the newly inserted lead
// pr = new PageReference( 'http://www.piab.com' );
pr = new PageReference( '/' + aLead.id );
}
catch( Exception e){
System.debug( '**** Caught Exception' +e ) ;
}
return pr ;

}
}

 

This doesn't send an email even though I believe I linked the button with the message code correctly because on the site it brings me to another page which makes me think that the email is tying to be sent. Is this code correct?

 
Message Edited by Margiro on 05-05-2009 01:57 PM
Best Answer chosen by Admin (Salesforce Developers) 
MarkSilberMarkSilber
I'm not sure why the email isn't working, but since it looks like you are working with Leads, why not use Lead auto response rules instead?  In order to use Lead assignment and auto response rules with Visualforce / Apex, you will need to add code similar to this:

// Select the Active Lead Assignment Rule. There can only be 1 active rule string id =[Select Name, Id From AssignmentRule where Active=true].id; database.DMLOptions dmo = new database.DMLOptions(); dmo.AssignmentRuleHeader.assignmentRuleId=id; dmo.AssignmentRuleHeader.useDefaultRule= false; dmo.EmailHeader.triggerUserEmail = true; dmo.EmailHeader.triggerAutoResponseEmail = true; NewLead.setOptions(dmo);