+ Start a Discussion

Need Help: Onclick "Save" button of theVF page Send email alert

Hi there,


I created a VF page which opens up on click of a button placed on the Opportunity object. Onclick "save" button I want it to do the following 2 actions


1) save the VF page/record

2) email to be sent to few users


Request every1 to kindly help me with the code






Chi-Towns FinestChi-Towns Finest

You can have the PageReference method do both. Can you attach a code snippit to add more clarity?


You need to set your Save button's action attribute to a method in your controller.  Are you using the Opportunity standardcontroller and a controller extension, or a custom controller?


If you're using an extension then your controller probably looks something like this.  For the purpose of illustrating how this would work, let's say your VF page is used for the purpose of creating some custom object named Services related to the Opportunity, and then you want to email the Sales Manager that it was created.


public with sharing class OpportunityControllerExtension {


            private final Opportunity opp;

            public Services__c service      { get; set; }


            public OpportunityControllerExtension( ApexPages.StandardController controller ) {

                         opp = ( Opportunity ) controller.getRecord();

                         service = new Services__c( Opportunity__c = opp.Id );



            public PageReference extensionSave() {

                         try {

                               insert service;

                         } catch( System.DmlException e ) { 

                               // do something here



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

                         email.setSubject( 'New Services Record Created' );

                         email.PlainTextBody( 'A new Services record has been created. <a href="...../" + opp.Id>Click Here</a>' );

                         email.setToAddresses( new String[] { salesmanager@acme.com } );

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


                         return Page.Success;   // given that you have a VF page named Success that displays a message




On your VF page, set your Save button's action attribute like this.



<apex:commandButton value="Save" action="{!extensionSave}" />



Hope that helps!


~ Clint