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
Francisco Riccomagno 1Francisco Riccomagno 1 

How can I send a custom email when a custom object is completed?

Hello, I created a VisualForce page in order to send an email to an email field within my custom object with a PDF attached. To do this I used the recomendations posted here: http://blog.jeffdouglas.com/2010/07/16/create-and-email-a-pdf-with-salesforce-com/
This is working fine, but now I want that to be done when my custom object meets a specific criteria. In my case, when a checkbox is set to TRUE. I tried to do this by creating a workflow, but the options that i see there are to send an email with some infromation shown in the same worflow wizzard. Is there a way to make the worflow to call the Visual Force page I created?
ShashForceShashForce
Hi,

You should be using an APEX trigger to send an email when a checkbox is checked on a record. Something like:

triggger sendEmail on custom_object__c(after insert, afetr update){
	for(custom_object__c co:trigger.new){
		if(co.check_box__c=true){
			//
			//code to generate your email body and attachment, followed by code to send email
			//
		}
	}
}

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
Francisco Riccomagno 1Francisco Riccomagno 1
But I have all my logic in a Controller Extension that is called from a VisualForce page. I have to duplicate that logic here as well?
ShashForceShashForce
Yes, you have to replicate the same logic in your trigger.
Francisco Riccomagno 1Francisco Riccomagno 1
Ok, I did that, but for some reason the PDF attached gets corrupted. Here is the code for attaching the pdf to the email:
PageReference pdf = Page.PdfGeneratorTemplate;
                // add parent id to the parameters for standardcontroller
                pdf.getParameters().put('id',registration.Id);
                
                // the contents of the attachment from the pdf
                Blob body;
                
                try {
                
                  // returns the output of the page as a PDF
                  body = pdf.getContent();
                
                // need to pass unit test -- current bug  
                } catch (VisualforceException e) {
                  body = Blob.valueOf('Some Text');
                }
                
                Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
                attach.setContentType('application/pdf');
                attach.setFileName('myFile.pdf');
                attach.setInline(false);
                attach.Body = body;
                
                String strHTMLBody;
                String strPlainBody;
                strHTMLBody= '<h2>Purchase Confirmation</h2>';
                strPlainBody= 'Purchase Confirmation\n';
                
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                
                OrgWideEmailAddress[] owea = [SELECT Id FROM OrgWideEmailAddress WHERE Address LIKE '%donotreply%'];
                if ( owea.size() > 0 ) {
                    mail.setOrgWideEmailAddressId(owea.get(0).Id);
                }
                
                mail.setUseSignature(false);
                mail.setToAddresses(new String[] { registration.Email__c });
                mail.setSubject('Product Confirmation');
                mail.setPlainTextBody(strPlainBody);
                mail.setHtmlBody(strHTMLBody);
                mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 
                
                // Send the email
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });