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
RedtagRedtag 

Return Casenumber from Sites Case Submit Form

I've modified the Web to Case form on Developerforce Quickstarts Creating Custom Web-To-Case Forms

What I've done is redirect the user to a thankyou page upon pressing the submit button.  But what I really need is for the Casenumber to be shown on the thankyou page. At the moment the casenumber does not show. I have checked the permissions and they are fine. The problem I have is how to pass the value from the first page to the thankyou page. Any help greatly appreciated.

 

Here is my page:

 

<apex:page controller="SubmitCaseController" title="Case thank you page" 
           showHeader="false" standardStylesheets="true">
  <apex:composition template="{!$Site.Template}">
  <apex:insert name="header">
    <c:SiteHeader />
    <hr/>
  </apex:insert>
    <apex:define name="body"> 
        <br/>
        <center>
        <apex:outputText value="Your information is saved.  Thank you for your interest!"/>
        <br/>
        <apex:outputText value="Your Case Reference Number is: {!c.casenumber}"/>
        </center>
        <br/>
        <br/>
    </apex:define>
  </apex:composition>
</apex:page>

 And here is the controller:

 

public class SubmitCaseController {
    
    public Case c { get; set; }
    public SubmitCaseController() {
        c = new Case();
    }
    public string casenum;
       
    
    // This method cancels the wizard, and returns the user to the  
    // Submit case page
    
    public PageReference cancel() {
			PageReference SubmitCasePage = new PageReference('/apex/SubmitCase');
			SubmitCasePage.setRedirect(true);
			return SubmitCasePage; 
    }
    
    public PageReference SubmitCase() {
        List<Contact> cons = [SELECT Id, email, AccountId, CreatedDate FROM Contact WHERE email = :c.SuppliedEmail order by CreatedDate desc];
        if (cons.size() < 1) {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.WARNING, 'Please enter an email that matches our records');
            ApexPages.addMessage(msg);
            return null;
        } else {
            try {
                c.ContactId = cons.get(0).Id;
                c.AccountId = cons.get(0).AccountId;
                
                            
                // Specify DML options to ensure the assignment rules are executed
                Database.DMLOptions dmlOpts = new Database.DMLOptions();
                dmlOpts.assignmentRuleHeader.useDefaultRule = true;
                c.setOptions(dmlOpts);

                // Insert the case
                INSERT c;
                
                casenum = c.CaseNumber;             
                
                return new PageReference('/casethankyou');
                } catch (Exception e) {
                ApexPages.addMessages(e);
                return null;
            }
        }
    }
}

 

And here is the SubmitCase page:

 

<apex:page controller="SubmitCaseController" title="Submit Case" showHeader="false"
           standardStylesheets="true">
<script>
  function confirmCancel() {
      var isCancel = confirm("Are you sure you wish to cancel?");
      if (isCancel) return true;
  
     return false;
  }  
  </script>
    
    <apex:composition template="{!$Site.Template}">
    <apex:insert name="header">
    <c:SiteHeader />
    <hr/>
    </apex:insert>
    <apex:define name="body">
    <apex:form >
        <apex:messages id="error"
                   styleClass="errorMsg"
                   layout="table"
                   style="margin-top:1em;"/>
      <apex:pageBlock title="" mode="edit">
        <apex:pageBlockButtons >
           <apex:commandButton value="Submit"
                               action="{!SubmitCase}"/>
           <apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="New Enquiry"
                               collapsible="false"
                               columns="1">
        <table>
            <tr>
                <th>Customer Name:</th>
                <td>
                <apex:inputText required="true" value="{!c.SuppliedName}"/></td>
            </tr>
            <tr>
                <th>Customer Email:</th>
                <td><apex:inputText required="true" value="{!c.SuppliedEmail}"/></td>
            </tr>
            <tr>
                <th>Business Name:</th>
                <td><apex:inputText required="true" value="{!c.SuppliedCompany}"/></td>
            </tr>
            <tr>
                <th>Subject:</th>
                <td><apex:inputText required="true" value="{!c.Subject}"/></td>
            </tr>
            <tr>
                <th>Your Problem:</th>
                <td><apex:inputTextArea required="true" rows="5" value="{!c.Description}"/></td>
            </tr>
         </table>
        
        </apex:pageBlockSection>
     </apex:pageBlock>
   </apex:form>
  </apex:define> 
 </apex:composition>
</apex:page>

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You'll need to query the information back from the database, as the case number is automatically generated when the record is saved:

 

// Insert the case
INSERT c;

Case cFromDB=[select id, CaseNumber from Case where id=:c.id];                
casenum = cFromDB.CaseNumber;     

 

 

 

All Answers

bob_buzzardbob_buzzard

You'll need to query the information back from the database, as the case number is automatically generated when the record is saved:

 

// Insert the case
INSERT c;

Case cFromDB=[select id, CaseNumber from Case where id=:c.id];                
casenum = cFromDB.CaseNumber;     

 

 

 

This was selected as the best answer
RedtagRedtag

Thanks - very obvious when Bob pointed it out.

kishore B T 10kishore B T 10
Hello BoB,
Whenever a Thankyou page is refereshed new case is generated how to stop this.
Ido Greenbaum 10Ido Greenbaum 10
Hey @bob_buzzard , and @RedTag , 
I followed this thread to implement the custom 'thank you' page for a Web-To-Case integration, with the intension of populating the CaseNumber value to be exposed and reflected back in the 'thank you' page. Unfurtunately, something isn't working, and I would be glad to receive some assistance.  Here's what I did and:

1. VisualForce Page - 'SubmitCase'. Pretty straightforward, the initial Submit a Case page: 
 
<apex:page controller="SubmitCaseController">
    <h1>Submit New Case</h1>
    <apex:form >
        <apex:pageMessages />
        <table>
            <tr>
                <th>Your Name:</th>
                <td><apex:inputText value="{!c.SuppliedName}"/></td>
            </tr>
            <tr>
                <th>Your Email:</th>
                <td><apex:inputText value="{!c.SuppliedEmail}"/></td>
            </tr>
          
            <tr>
                <th>Title:</th>
                <td><apex:inputText required="true" value="{!c.Subject}"/></td>
            </tr>
            <tr>
                <th>Your Problem:</th>
                <td><apex:inputTextArea required="true" rows="5" value="{!c.Description}"/></td>
            </tr>
            <tr>
                <td><apex:commandButton value="Submit Case" action="{!submitCase}"/></td>
            </tr>
        </table>
    </apex:form>
</apex:page>

2.  Apex Class - 'SubmitCaseController' : 
public class SubmitCaseController {

    public String CaseNumber { get; set; }
    
    public Case c { get; set; }
    public SubmitCaseController() {
        c = new Case();
    }
    public string casenum;
       
    
    // This method cancels the wizard, and returns the user to the  
    // Submit case page
    
    public PageReference cancel() {
            PageReference SubmitCasePage = new PageReference('/apex/SubmitCase');
            SubmitCasePage.setRedirect(true);
            return SubmitCasePage; 
    }
    
    public PageReference SubmitCase() {
        List<Contact> cons = [SELECT Id, email, AccountId, CreatedDate FROM Contact WHERE email = :c.SuppliedEmail order by CreatedDate desc];
        if (cons.size() < 1) {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.WARNING, 'Please enter an email that matches our records');
            ApexPages.addMessage(msg);
            return null;
        } else {
            try {
                c.ContactId = cons.get(0).Id;
                c.AccountId = cons.get(0).AccountId;
                
                            
                // Specify DML options to ensure the assignment rules are executed
                Database.DMLOptions dmlOpts = new Database.DMLOptions();
                dmlOpts.assignmentRuleHeader.useDefaultRule = true;
                c.setOptions(dmlOpts);

                // Insert the case
                INSERT c;
                
                Case cFromDB =[select id, CaseNumber from Case where id=:c.id];  
                system.debug('#######' + cFromDB.CaseNumber );      
                casenum = cFromDB.CaseNumber;

           

                return new PageReference('/apex/casethankyou');
                } catch (Exception e) {
                ApexPages.addMessages(e);
                return null;
            }
        }
    }
}

3. VisualForce Page - Thank You named 'casethankyou': 
 
<apex:page controller="SubmitCaseController" title="Case thank you page" 
           showHeader="false" standardStylesheets="true">
  <apex:composition template="{!$Site.Template}">
  <apex:insert name="header">
    <c:SiteHeader />
    <hr/>
  </apex:insert>
    <apex:define name="body"> 
        <br/>
        <center>
        <apex:outputText value="Your information is saved.  Thank you for your interest!"/>
        <br/>
        <apex:outputText value="Your Case Subject is: {!c.Subject}"/>
        <br/>
        <apex:outputText value="Your Case Record type ID is: {!c.id}"/>
        <br/>
        <apex:outputText value="Your Case Reference Number is: {!c.CaseNumber}"/>
        </center>
        <br/>
        <br/>
    </apex:define>
  </apex:composition>
</apex:page>

Unfutunately, the output is missing the Case Number: 
missing case number

I raised the debug log and can note the Case Number print for debug purposes, so am quite sure the last bit is around setting the value to the appropriate variable, and I'm not sure how. 

case number in logs
Can I please ask for your assistance? 

Thank you. 


 
Qasim KhanQasim Khan
Thanks for sharing these codes, it really helpful...Buy Essay Cheap (https://www.essaymania.co.uk/buy-essay-online)