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
Jyosi jyosiJyosi jyosi 

Visual force page extension to insert and send email

Hello Everyone,
I am trying send out the email and create a case when the email has been sentout from the system

I am able to send the Email,but when i try to create a records for the case its not happening.
Here is the code 

##################Visual forcePage#########################################<apex:page standardController="Contact" tabStyle="contact" showHeader="true" showChat="false" extensions="sendEmail,SubmitCaseController">
<apex:form >
  <apex:pageBlock title="Termination Form">
  <apex:pageblockSection columns="2" >
  <apex:outputField value="{!contact.LastName}"/>
  <apex:outputField value="{!contact.FirstName}"/>
  <apex:outputField value="{!contact.Phone}"/>
  </apex:pageblockSection>
  <apex:pageblockButtons >  
  <apex:commandButton value="Save" action="{!Save}"/>
  <apex:commandButton value="Send Email" action="{!send}" /> 
  </apex:pageblockButtons>
  </apex:pageBlock>
</apex:form>
  </apex:page>

First Extention that trigger email   -----By using this extension i am trying to sendout email
public class sendEmail {

    public sendEmail(ApexPages.StandardController controller) {
    contact = [ select Id,LastName,FirstName,Phone,Email from contact where id = :ApexPages.currentPage().getParameters().get('id')];

    }

    public String subject { get; set; }
    public String body { get; set; }

    private final Contact contact;
    public contact getcontact() {
        return contact;
    }
    public PageReference send() {
      
       /* Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'}; 
        String[] ccAddresses = new String[] {'}; 
        message.setTemplateId('00Xo0000000YyL2');
        message.setTargetObjectId('005o0000000kUqP');
        message.setToAddresses(toAddresses);
        system.debug('ToAddress@@@@@@@'+ toAddresses );
        system.debug('CCC@@@Address@@@@@@@'+ ccAddresses );
        message.setccAddresses(ccAddresses );
        message.setBccSender(false);
        message.saveAsActivity=false;
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{ message });
        system.debug('MessageSent@@@@@@@@@@@@@@@@@@@@@@@'+message );
        //return controller.view();
        */
       return null;
    }
}

Second Extension that creates a case   -----By using this extension i am trying to create a case ,but the case has not been created
public class SubmitCaseController {
   public Case c { get; set; }
     public final Contact cnt ;
    public SubmitCaseController(ApexPages.StandardController controller) {
        c = new Case();
        cnt = [ select Id,LastName,FirstName,Phone,Email from contact where id = :ApexPages.currentPage().getParameters().get('id')];
        system.debug('cntlist@@@@'+cnt);
    }
    public contact getcontact() {
        return cnt ;
    }
    public PageReference send() {
    try{
    if(cnt!=null)
    {
          c.ContactId = cnt.Id;
          c.Status='New';
          c.Origin='Email';
          c.Reason='Other';
          system.debug('Caselist@@@@@@@@@@@@@@@@@@'+c);
          insert c;
    }      

     }catch(DMLException e){
         ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new case.'));
     }
     
    
   
     return new PageReference('/thanks');
    }
    

}


Thanks a lot

Regards,
Jyo


 
Best Answer chosen by Jyosi jyosi
Roy LuoRoy Luo
Both extensions have 'send' method, that won't fly. Do you need to have a UI action to create a Case or would it be auto created? If no UI action, why bother a second controller extension? You might just append the Case creation codes to the end of sending email.
 

All Answers

Roy LuoRoy Luo
Both extensions have 'send' method, that won't fly. Do you need to have a UI action to create a Case or would it be auto created? If no UI action, why bother a second controller extension? You might just append the Case creation codes to the end of sending email.
 
This was selected as the best answer
Jyosi jyosiJyosi jyosi
Hello Roy Luo,
Got a typical requirment.
Here it how it goes.
1) There will be button on detail page 
2)Once they click the button visualforce page opens.
3) Here i am able to send the Email by using the template.it may be personal/Offical Email Id.)
//this all the section has been developed.
4) When i click "Send Email" button on visualforce page ,i need to create a case.//
Reg the point 5 do you suggest me what is the best way to get accomplisied?
5)Suppose if we get reply from the above email id i need to update the case with few values. 
I Have enabled to email to case and i need to read the some value /May be emailid and update the case if the email id matches?

Could you please suggest me how to achevie this?

Thanks for the help.