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
Hemalatha  ParuchuriHemalatha Paruchuri 

in below code send email is not working...

<apex:page controller="TicketGenerateController">
    <style>
        .body{
            overflow-y: auto;
            height: 100px;
        }
    </style>
    <apex:form >
   
        <apex:pageBlock >
         
            <apex:pageMessages id="pgMsg"/> 
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Edit" onclick="window.location.href='/apex/TicketGenerate?id={!objPreviewTicket.Id}';return false;"/>
                <apex:commandButton value="Send Email" action="{!SendEmail}" oncomplete="redirectPage();" rerender="pgMsg"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:pageBlock >
            <apex:iframe src="/apex/TicketPDFTemplate?id={!objPreviewTicket.Id}" />
        </apex:pageBlock>
    </apex:form>
    <script>
        function redirectPage(){ 
            alert("Email Sent Successfully");
            window.location.href = '/apex/TicketGenerateView?id={!objPreviewTicket.Id}';
           
        }
    </script>
</apex:page>
Shrikant BagalShrikant Bagal
Please post your class code?
 
Hemalatha  ParuchuriHemalatha Paruchuri

    
            Name         : TicketGenerateController
            Description  : To generate Revenue Ticket from SalesWorksheet and Referral Ticket from Prospect/Lead Source.
*/
public with sharing class TicketGenerateController {
    public Ticket__c objTicket {get;set;}
    public Ticket__c objPreviewTicket {get;set;}
    public Lead_Source__c objLeadS {get;set;}
    public String strWSId; //Holds Sales Worksheet Id
    public String strID; //Holds Account/Lead Source Id
    public string strLabel {get;set;} // Holds Ticket Label based on RecordType
    public Boolean isDisplay {get;set;}
    public Boolean isDisplayAcc {get;set;}
    public Boolean isDisplayFields {get;set;}
    public Boolean showMessage {get;set;}
    public String strComments;
    public String strRecordTypeName {get;set;}
    
    public TicketGenerateController()
    {
        String strId = ApexPages.CurrentPage().getParameters().get('Id');
        if( string.isNotBlank(strId) )
        objPreviewTicket = [select id,RecordtypeId,Name,Prospect_Client__c,Lead_Source_Designer__r.Full_Name__c,Sales_Consultant__r.Full_Name__c,
                                   Sales_Consultant__r.User__r.Email,Status__c,Date__c,Comments__c,Comments_History__c,Sales_Consultant_Name__c 
                                   from Ticket__c WHERE Id =: strId];
    }
    public TicketGenerateController(ApexPages.StandardController stdcontroller)
    {
        isDisplay = true;
        isDisplayAcc = true;
        String strId = ApexPages.CurrentPage().getParameters().get('Id');
        
       
        
        
        //If Id is not blank Query Estimate record to display View Page
        if( string.isNotBlank(strId) )
        {
            isDisplayFields = true;
            objTicket = [select Id, Name, Prospect_Client__c, Prospect_Client__r.Full_name__c, Prospect_Client__r.Company_Name__c,Prospect_Client__r.Cell_Phone__c,
                                Prospect_Client__r.ShippingStreet,Prospect_Client__r.ShippingCity,Prospect_Client__r.ShippingState, Comments_History__c ,
                                Lead_Source_Designer__c, Lead_Source_Designer__r.Full_Name__c, Sales_Consultant__c, Sales_Worksheet__c,RecordtypeId,Sales_Consultant__r.Last_Name__c,
                                Estimate__c, Invoice__c, Comments__c, Date__c, Status__c, CreatedById, CreatedDate, LastModifiedById, LastModifiedDate,Sales_Consultant__r.Name,
                                Lead_Source_Designer__r.Last_Name__c, Prospect_Client__r.Last_Name__c,Prospect_Client_Name__c,Sales_Consultant__r.Full_Name__c,
                                Sales_Consultant_Name__c,Prospect_Client_Company_Name__c,Prospect_Client_Cell_Phone__c,Prospect_Client_Street__c,Prospect_Client_City__c,
                                Prospect_Client_State__c,Lead_Source_Name__c, Sales_Consultant__r.User__r.Name
                                from Ticket__c WHERE Id =: strId];
            strComments = objTicket.Comments__c;
            strRecordTypeName = [select Name from RecordType WHERE sObjectType = 'Ticket__c' and Id =: objTicket.RecordtypeId].Name;
            strLabel = strRecordTypeName + '('+ objTicket.Name +')';
        }
        else
        {
            isDisplay = false;
            isDisplayFields = false;
            objTicket = new Ticket__c();
            objTicket.Status__c = 'Open'; 
            strLabel = 'New Referral Ticket ';
            Pagereference refRetUrl = new Pagereference(ApexPages.CurrentPage().getParameters().get('retURL')); 
            strID = refRetUrl.getParameters().get('id');
            String strRetUrl =  ApexPages.CurrentPage().getParameters().get('retURL');
            //If RetUrl contains Lead Source Id
            if( strRetUrl.Contains('LeadSource'))
            {
                isDisplayAcc = false;
                Lead_Source__c objLeadS = [select Id, Name, Last_Name__c, Full_Name__c, OwnerId, Sales_Consultant__c, Sales_Consultant__r.Full_Name__c
                                           from Lead_Source__c WHERE Id =: strID];
                objTicket.Lead_Source_Designer__c = objLeadS.Id;  //Auto-populating Lead Source
                objTicket.Lead_Source_Name__c = objLeadS.Full_Name__c;
                objTicket.Sales_Consultant__c = objLeadS.Sales_Consultant__c;   //Auto-populating Sales Consultant
                objTicket.Sales_Consultant_Name__c = objLeadS.Sales_Consultant__r.Full_Name__c;
                strLabel+='('+objLeadS.Full_Name__c+')';
            }
            //If RetUrl contains Account(Prospect/Active Client) Id
            else if( strRetUrl.Contains('Account'))
            {
                Account objAcc = [select Id, Name, Last_Name__c, Company_Name__c, Cell_Phone__c, ShippingStreet, ShippingCity, ShippingState, Sales_Consultant__c,
                                  OwnerId, Lead_Source__r.Last_Name__c, Lead_Source__c, Comments_History__c, Lead_Source__r.Full_Name__c,Full_Name__c, Owner.Name,
                                  Sales_Consultant__r.Full_Name__c
                                  FROM Account WHERE Id =: strID];
                objTicket.Prospect_Client__c = objAcc.Id;  //Auto-populating Prospect/Client
                objTicket.Prospect_Client_Name__c = objAcc.Full_Name__c;
                objTicket.Prospect_Client_Company_Name__c = objAcc.Company_Name__c;
                objTicket.Prospect_Client_Cell_Phone__c = objAcc.Cell_Phone__c;
                objTicket.Prospect_Client_Street__c = objAcc.ShippingStreet;
                objTicket.Prospect_Client_City__c = objAcc.ShippingCity;
                objTicket.Prospect_Client_State__c = objAcc.ShippingState;
                objTicket.Lead_Source_Designer__c = objAcc.Lead_Source__c;  //Auto-populating Lead Source
                objTicket.Lead_Source_Name__c = objAcc.Lead_Source__r.Full_Name__c;
                objTicket.Sales_Consultant__c = objAcc.Sales_Consultant__c;  //Auto-populating Sales Consultant
                objTicket.Sales_Consultant_Name__c = objAcc.Sales_Consultant__r.Full_Name__c;
                objTicket.Comments_History__c = objAcc.Comments_History__c;  //copying Comments History from prospect to ticket
                strLabel+='('+objAcc.Full_Name__c+')';
            }
        }
        if(strRecordTypeName == 'Referral Ticket')
            isDisplay = false;
            
        if(objTicket.Prospect_Client__c == null)
            isDisplayAcc = false;
    }

    public PageReference save()
    {
        try{
            //If Ticket is Generated from Prospect/Active Client then save with RecordType Referral Ticket
            String strRecordTypeId;
            if(objTicket.RecordtypeId == null)
                strRecordTypeId = [Select id from RecordType where sObjectType = 'Ticket__c' and Name = 'Referral Ticket'].Id;
            
            if( string.isNotBlank(strRecordTypeId))
                objTicket.RecordtypeId = strRecordTypeId;
            //adding Comments History
            datetime dt = datetime.now();
            String strFormattedDate =  dt.format('MMM-d-yyyy h:mm a');   //Holds Date in format of MM-DD-YYYY and MM as (1=Jan)
            String strDesc = '---------- '+Userinfo.getName()+'-------- '+strFormattedDate+'\n'+objTicket.Comments__c+'\n\n';
            if( objTicket.Comments_History__c!=null ){
                if(strComments!=objTicket.Comments__c){ //Comments is edited then update this to Comments History
                    objTicket.Comments_History__c = strDesc+objTicket.Comments_History__c;
                }
            }
            else if((objTicket.Id!=null) || (objTicket.Id==null && strDesc !=null)){
                if(objTicket.Comments__c != null && objTicket.Comments__c != '')
                    objTicket.Comments_History__c = strDesc;
            }
            objTicket.Comments__c = null;
            upsert objTicket;
            Pagereference refTickt = new Pagereference('/'+objTicket.Id); 
            refTickt.setRedirect(true);
            return refTickt;
        }catch(exception e)
        {
            system.debug('Exception..'+e);
        }
        return null;
    }
    //To preview Ticket before sending email
    public Pagereference Preview(){
        save();
        Pagereference refTickt = new Pagereference('/apex/TicketPreview?id='+objTicket.Id); 
        refTickt.setRedirect(true);
        return refTickt;
    }
    //Sending Ticket Information as an Email
    public Pagereference SendEmail(){
    
    
        //save();
        Ticket__c objTicketTemp = [select id,RecordtypeId,Name,Prospect_Client__c,Lead_Source_Designer__r.Full_Name__c,Sales_Consultant__r.Full_Name__c,
                                   Sales_Consultant__r.User__r.Email,Status__c,Date__c,Comments__c,Comments_History__c 
                                   from Ticket__c where id=:objPreviewTicket.Id];                                   
                                         
        String strRecordType = [Select Name from RecordType where sObjectType = 'Ticket__c' and Id=: objTicketTemp.RecordtypeId].Name;
        system.debug('------mytest'+strRecordType);
        try{
        system.debug('--mydebug---------'+objTicketTemp.Sales_Consultant__r.Email__c); 
            if( objTicketTemp!=null && objTicketTemp.Sales_Consultant__r.Email__c!=null ){ 
             
                SalesUtil.SendTicketEmail(objTicketTemp.Id,objTicketTemp.Name, objTicketTemp.Prospect_Client__c, objTicketTemp.Sales_Consultant__r.Full_Name__c, 
                                          objTicketTemp.Lead_Source_Designer__r.Full_Name__c, objTicketTemp.Status__c, objTicketTemp.Date__c, 
                                          objTicketTemp.Comments_History__c== null ? '' : String.valueof(objTicketTemp.Comments_History__c),
                                          objTicketTemp.Sales_Consultant__r.Email__c,strRecordType);
                                        

                objPreviewTicket.Status__c = 'Sent To Sales Consultant';
                update objPreviewTicket;
                
                
               
                
                Apexpages.Message msg = new ApexPages.Message(ApexPages.Severity.Info,'Email Sent Successfully to Sales Consultant');
            }
        }catch(exception e)
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.error,'Email Failed'+e));
        }
        return null;
        /*String strProspect;
        String strDesign;
        String strSalesCons;
        String strEmailId;
        
        String strProspectId = objTicket.Prospect_Client__c;
        Account objProspect = [select id,Name from Account where id=:strProspectId];
        strProspect = (objProspect!=null)? objProspect.Name : '';
        
        String strDesignerId = objTicket.Lead_Source_Designer__c;
        Lead_Source__c objLS = [select id,Name from Lead_Source__c where id=:strDesignerId];
        strDesign = (objLS!=null)? objLS.Name : '';
        
        String strSCId = objTicket.Sales_Consultant__c;
        Staff__c objStaff = [select id,Name,User__r.Email from Staff__c where id=:strSCId];
        try{
            if( objStaff!=null && objStaff.User__r.Email!=null ){
                strSalesCons = (objStaff!=null)? objStaff.Name : '';
                strEmailId = objStaff.User__r.Email;
                SalesUtil.SendTicketEmail(strProspect, strSalesCons, strDesign, objTicket.Status__c, objTicket.Date__c, objTicket.Comments__c, strEmailId);
            }else{
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.error,'Sales Consultant is Madatory'));
            }
        }catch(exception e)
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.error,'Email Failed'+e));
        }*/
    }
    
    
    //To get Prospect Details
    @remoteAction
    public static list<String> getProspectDetails( string strProspId )
    {
        list<String> lstProspect = new list<String>{'','','','','',''}; 
        if( string.isNotBlank(strProspId) )
        {
            Account objAccount = [select Id, Full_Name__c, Cell_Phone__c, Company_Name__c, ShippingStreet, ShippingCity, ShippingState From Account where Id = : strProspId]; 
            if( objAccount != null )
            {
                lstProspect.set(0,objAccount.Full_Name__c);
                if(objAccount.Company_Name__c != null && objAccount.Company_Name__c != '')
                    lstProspect.set(1,objAccount.Company_Name__c);
                if(objAccount.Cell_Phone__c != null && objAccount.Cell_Phone__c != '')
                    lstProspect.set(2,objAccount.Cell_Phone__c);
                if(objAccount.ShippingStreet != null && objAccount.ShippingStreet != '')
                    lstProspect.set(3,objAccount.ShippingStreet);
                if(objAccount.ShippingCity != null && objAccount.ShippingCity != '')
                    lstProspect.set(4,objAccount.ShippingCity);
                if(objAccount.ShippingState != null && objAccount.ShippingState != '')
                    lstProspect.set(5,objAccount.ShippingState);
            }
        }
        return lstProspect; 
    }
    
    //To get Sales Consultant Full name
     @remoteAction
    public static String getSalesConName( string strSalesId )
    {
        String strSalesConName; 
        if( string.isNotBlank(strSalesId) )
        {
            Staff__c objStaff = [select Id, Full_Name__c From Staff__c where Id = : strSalesId]; 
            if( objStaff != null )
            {
                strSalesConName = objStaff.Full_Name__c;
            }
        }
        return strSalesConName; 
    }
    
    //To get Lead Source Full name
     @remoteAction
    public static String getLeadSourceName( string strLeadSourceId )
    {
        String strLeadSourceName; 
        if( string.isNotBlank(strLeadSourceId) ) 
        {
            Lead_Source__c objLead = [select Id, Full_Name__c From Lead_Source__c where Id = : strLeadSourceId]; 
            if( objLead != null )
            {
                strLeadSourceName = objLead.Full_Name__c;
            }
        }
        return strLeadSourceName; 
    }
    
    
    // adding for create new prospect
    public pageReference CreateNewProspect()
    {
        Pagereference refProspect = new Pagereference('/apex/AccountDetailEdit?retURL=/apex/TicketGenerateView?id='+objTicket.Id);
     // Pagereference refProspect = new Pagereference('/apex/ProspectListView?retURL=/apex/TicketGenerateView?id='+objTicket.Id+'&Ticket=Referral');
         refProspect .setRedirect(true);
        return refProspect ; 
    } 
    
    
    public Pagereference SalesWS(){
        Pagereference refSW = new Pagereference('/apex/SalesWorksheet?retURL=/apex/TicketGenerateView?id='+objTicket.Id+'&Ticket=Referral');
        refSW.setRedirect(true);
        return refSW;
    }
}