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
AddaAdda 

Custom send email notification checkbox on Visualforce Page

Hi All,

I have created a custom button on case feed that helps to change owner either to user or queue. Now, I need to include a send email notification checkbox in that visualforce page that will send email when case owner is changed same as standard SFDC Change Owner page.

Please can anyone help? Here is my visualforce and apex code.
 
apex:page tabStyle="Case" standardController="Case" extensions="ChangeCaseOwner" sidebar="false">

<apex:form id="formId">
<apex:includeScript value="/soap/ajax/26.0/connection.js"/>  
<apex:includeScript value="/support/console/26.0/integration.js"/>
<apex:inputHidden id="isInConsole" value="{!isInConsole}" />
<apex:sectionHeader title="Change Case Owner"/>
<!-- <p>This screen allows you to transfer cases from one user or queue to another. When you transfer ownership, the new owner will own:</p>
<ul><li>all open activities (tasks and events) for this case that are assigned to the current owner</li></ul>
<p>Note that completed activities will not be transferred. Open activities will not be transferred when assigning this case to a queue.</p> -->
<apex:pageBlock mode="Edit">
<apex:pageMessages ></apex:pageMessages>
<apex:pageBlockButtons location="bottom">
<apex:outputText rendered="{!shouldRedirect}">
                <script type="text/javascript">
                    window.top.location.href = '{!redirectUrl}';
                </script>
</apex:outputText>
<apex:commandButton value="Save" action="{!save}" />
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
<br/><apex:pageBlockSection title="Select New Owner" collapsible="false" columns="3">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Owner"></apex:outputLabel>
<apex:inputField value="{!Case.ownerId}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
<script type="text/javascript">
    document.getElementById("j_id0:formId:isInConsole").value = sforce.console.isInConsole();
</script>

</apex:page>

Apex Code
public class ChangeCaseOwner {
    ApexPages.StandardController   controllerInstance;
    public String redirectUrl {public get; private set;}
    public Boolean shouldRedirect {public get; private set;}
    public String isInConsole{get; set;}
    public String browserUrlforRedirection{get;set;}
   
    public ChangeCaseOwner (ApexPages.StandardController controller) {
        controllerInstance = controller;
    } 
    
    public pageReference save () {
        controllerInstance.save();
        case caseRecord = (Case)controllerInstance.getRecord();
        shouldRedirect = true;
        system.debug('correctVal' + isInConsole);
        String baseUrl = System.URL.getSalesforceBaseUrl().toExternalForm();
        String url;
        if(isInConsole == 'false') {
            url = baseUrl + '/' + caseRecord.Id ;
            system.debug('inside false');
        }
        else{
            url = baseUrl + '/console' ;
        }
        system.debug('final url' + url);
        redirectUrl  = url;
        system.debug(redirectUrl);
        //redirectUrl = 'https://cs30.salesforce.com/console';
        return null;

    } 
    
    public pageReference cancel () {
        case caseRecord = (Case)controllerInstance.getRecord();
        shouldRedirect = true;
        system.debug('correctVal' + isInConsole);
        String baseUrl = System.URL.getSalesforceBaseUrl().toExternalForm();
        String url;
        if(isInConsole == 'false') {
            url = baseUrl + '/' + caseRecord.Id ;
        }
        else{
            url = baseUrl + '/console' ;
        }
        redirectUrl  = url;
        return null;

    } 

}


 
S.Haider RazaS.Haider Raza
Adda,
Below link will show you how to send email using apex, just in case you were not aware of it
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

For the check box add a Boolean public variable in the controller, and use the inputCheckbox with the value to the boolean variable, then use this variable to send out email.

Below is an example with the checkbox
<apex:page id="communitiesSelfRegPage" showHeader="false" sidebar="false" controller="XeroCommunitiesSelfRegController" title="{!$Label.site.user_registration}" standardStylesheets="false">
    <script>
        function validateForm(){
            if(document.getElementById('{!$Component.theform.pgblock.firstName}').value == ''){alert('Provide first name'); return false;}
            if(document.getElementById('{!$Component.theform.pgblock.lastName}').value == ''){alert('Provide last name'); return false;}
            if(document.getElementById('{!$Component.theform.pgblock.communityNickname}').value == ''){alert('Provide nick name'); return false;}
            if(document.getElementById('{!$Component.theform.pgblock.email}').value == ''){alert('Provide email'); return false;}
            if(document.getElementById('{!$Component.theform.pgblock.tc}').value == false){alert('Terms & Conditions'); return false;}
            return true;
        }
    </script>
    
    <apex:form id="theForm" forceSSL="true">
        <apex:pageBlock id="pgblock">
                <apex:pageMessages id="error"/>
                <h2>Join our community</h2>
                <apex:outputLabel value="First Name" for="firstName"/>
                <apex:inputText id="firstName" value="{!firstName}" label="First Name"/><br/>
                <apex:outputLabel value="Last Name" for="lastName"/>
                <apex:inputText id="lastName" value="{!lastName}" label="Last Name"/><br/>
                <apex:outputLabel value="{!$Label.site.community_nickname}" for="communityNickname"/>
                <apex:inputText id="communityNickname" value="{!communityNickname}" label="{!$Label.site.community_nickname}"/><br/>
                <apex:outputLabel value="{!$Label.site.email}" for="email"/>
                <apex:inputText id="email" value="{!email}" label="{!$Label.site.email}"/><br/>
                <apex:outputLabel value="{!$Label.site.password}" for="password"/>
                <apex:inputSecret id="password" value="{!password}"/><br/>
                <apex:outputLabel value="{!$Label.site.confirm_password}" for="confirmPassword"/>
                <apex:inputSecret id="confirmPassword" value="{!confirmPassword}"/><br/>

                <apex:inputCheckbox value="{!termsconditions }" id="tc"><apex:outputLink value="http://google.com.au">Terms &amp; Conditions</apex:outputLink>
                    <apex:actionsupport event="onchange" rerender="working" />     
                </apex:inputCheckbox>
                    
                <apex:outputPanel id="working">    
                    <apex:pageBlockSection id="debugBlock" rendered="{!IF(termsconditions == true,true,false)}" >
                        <apex:commandButton value="Save" action="{!registerUser}" onclick="return validateForm();"/>
                    </apex:pageBlockSection>
                </apex:outputpanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Below is the controller
/**
 * An apex page controller that supports self registration of users in communities that allow self registration
 */
public with sharing class XeroCommunitiesSelfRegController {

    public Boolean termsconditions {get;set;}
    public String firstName {get; set;}
    public String lastName {get; set;}
    public String email {get; set;}
    public String password {get; set {password = value == null ? value : value.trim(); } }
    public String confirmPassword {get; set { confirmPassword = value == null ? value : value.trim(); } }
    public String communityNickname {get; set { communityNickname = value == null ? value : value.trim(); } }
    
    public XeroCommunitiesSelfRegController() {}
    
    private boolean isValidPassword() {
        return password == confirmPassword;
    }

    public PageReference registerUser() {
    
           // it's okay if password is null - we'll send the user a random password in that case
        if (!isValidPassword()) {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, Label.site.passwords_dont_match);
            ApexPages.addMessage(msg);
            return null;
        }    
        
        if(!termsconditions){
        	ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Terms & conditions not agreed');
            ApexPages.addMessage(msg);
            return null;
        }

        String profileId = Xero_Variables__c.getOrgDefaults().ProfileId__c; 
        String roleEnum = null; // To be filled in by customer.
        String accountId =  createAccount(firstname, lastname); 
        
        String userName = email;

        User u = new User();
        u.Username = userName;
        u.Email = email;
        u.FirstName = firstName;
        u.LastName = lastName;
        u.Username = email;
        u.CommunityNickname = communityNickname;
        u.ProfileId = profileId;
        
        String userId = Site.createPortalUser(u, accountId, password);

        if (userId != null) { 
            if (password != null && password.length() > 1) {
                return Site.login(userName, password, null);
            }
            else {
                PageReference page = System.Page.XeroCommunitiesSelfRegConfirm;
                page.setRedirect(true);
                return page;
            }
        }
        return null;
    }
       
    private String createAccount(String firstname, String lastname){
        Account acc = new Account(Name = firstname+' '+lastname, OwnerId=Xero_Variables__c.getOrgDefaults().Account_OwnerId__c);
        insert acc;
        return String.valueOf(acc.Id);
    }
    
}


 
AddaAdda
Sorry Raza, I am not a strong developer so I don't understand your code. Your code refer to communities and I am refering something else within Salesforce on Case Feed object. The checkbox I am looking for is send email notification checkbox that you see when you change case owner in a standard change owner button. I want to replicate the same in the custom button.

Thanks
Adda
S.Haider RazaS.Haider Raza
Adda changed controller is as below
 
public class ChangeCaseOwner {
    ApexPages.StandardController   controllerInstance;
    public String redirectUrl {public get; private set;}
    public Boolean shouldRedirect {public get; private set;}
    public String isInConsole{get; set;}
    public String browserUrlforRedirection{get;set;}
    public Boolean sendemail {get;set;}
   
    public ChangeCaseOwner (ApexPages.StandardController controller) {
        controllerInstance = controller;
    } 
    
    public pageReference save () {
        controllerInstance.save();
        case caseRecord = (Case)controllerInstance.getRecord();
        if(sendemail)sendEmail(caseRecord.OwnerId);
        shouldRedirect = true;
        system.debug('correctVal' + isInConsole);
        String baseUrl = System.URL.getSalesforceBaseUrl().toExternalForm();
        String url;
        if(isInConsole == 'false') {
            url = baseUrl + '/' + caseRecord.Id ;
            system.debug('inside false');
        }
        else{
            url = baseUrl + '/console' ;
        }
        system.debug('final url' + url);
        redirectUrl  = url;
        system.debug(redirectUrl);
        //redirectUrl = 'https://cs30.salesforce.com/console';
        return null;

    } 
    private void sendEmail(String ownerId){
        String forwardTo = [SELECT email FROM user WHERE Id=:ownerId LIMIT 1].email;
		Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new List<String>{forwardTo});
		//mail.setReplyTo(email.fromAddress);
		//mail.setSenderDisplayName(email.fromName);
		mail.setSubject('Case Assignment');
		mail.setPlainTextBody('Case has been assigned to you');
		//mail.setHTMLBody(email.htmlBody);
        Messaging.sendEmail( new List<Messaging.EMail> { mail });
    }    
    public pageReference cancel () {
        case caseRecord = (Case)controllerInstance.getRecord();
        shouldRedirect = true;
        system.debug('correctVal' + isInConsole);
        String baseUrl = System.URL.getSalesforceBaseUrl().toExternalForm();
        String url;
        if(isInConsole == 'false') {
            url = baseUrl + '/' + caseRecord.Id ;
        }
        else{
            url = baseUrl + '/console' ;
        }
        redirectUrl  = url;
        return null;

    } 
    private void sendEmail(String ownerId){
        String forwardTo = [SELECT email FROM user WHERE Id=:ownerId LIMIT 1].email;
		Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new List<String>{forwardTo});
		//mail.setReplyTo(email.fromAddress);
		//mail.setSenderDisplayName(email.fromName);
		mail.setSubject('Case Assignment');
		mail.setPlainTextBody('Case has been assigned to you');
		//mail.setHTMLBody(email.htmlBody);
        Messaging.sendEmail( new List<Messaging.EMail> { mail });
    }
}

Below is the visualforce page, added the checkbox. Please position the item as required.
 
<apex:page tabStyle="Case" standardController="Case" extensions="ChangeCaseOwner" sidebar="false">

<apex:form id="formId">
<apex:includeScript value="/soap/ajax/26.0/connection.js"/>  
<apex:includeScript value="/support/console/26.0/integration.js"/>
<apex:inputHidden id="isInConsole" value="{!isInConsole}" />
<apex:sectionHeader title="Change Case Owner"/>
<!-- <p>This screen allows you to transfer cases from one user or queue to another. When you transfer ownership, the new owner will own:</p>
<ul><li>all open activities (tasks and events) for this case that are assigned to the current owner</li></ul>
<p>Note that completed activities will not be transferred. Open activities will not be transferred when assigning this case to a queue.</p> -->
<apex:pageBlock mode="Edit">
<apex:pageMessages ></apex:pageMessages>
<apex:pageBlockButtons location="bottom">
<apex:outputText rendered="{!shouldRedirect}">
                <script type="text/javascript">
                    window.top.location.href = '{!redirectUrl}';
                </script>
</apex:outputText>    
    <apex:inputCheckbox value="{!sendemail}" selected="false" />
<apex:commandButton value="Save" action="{!save}" />
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
<br/><apex:pageBlockSection title="Select New Owner" collapsible="false" columns="3">    
    {!sendemail}
<apex:pageBlockSectionItem >    
<apex:outputLabel value="Owner"></apex:outputLabel>
<apex:inputField value="{!Case.ownerId}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
<script type="text/javascript">
    document.getElementById("j_id0:formId:isInConsole").value = sforce.console.isInConsole();
</script>

</apex:page>


 
S.Haider RazaS.Haider Raza
Adda did the above helped?