Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My scenario is, by clicking a button in a visualforce page, I need to create a new Case which preferable opens the standard New Case creation page. And after saving the new case, the related function can return the newly created case. Any suggestions/sample code for that?

I am pretty new to Salesforce development and time doesn't allow me to go through the book.

share|improve this question
up vote 3 down vote accepted

You need to redirect to /500/e?retURL=%2F500%2Fo which is the "new case" standard salesforce URL which you see when you click on the standard "New Case" button.

Create a command button and a method in your APEX code that will redirect to that URL.

For an example:

<apex:commandbutton value="New Case" action="{!createNewCase}" />

then in your apex code

public PageReference createNewCase() { return new PageReference('/500/e?retURL=%2F500%2Fo'); }

Alternatively you can do that with HTML or Javascript:

HTML anchor: <a href="/500/e?retURL=%2F500%2Fo">New Case</a>

Javascript event on a button : <input type="button" value="New Case" onclick="window.location.href = '/500/e?retURL=%2F500%2Fo';" />

EDIT (upon understanding the question better):

1) Create another VF page and controller but this time using the case standard controller - see this. This way is easier for you to understand, later on you can merge the two into 1 page, but let's leave it for now I don't want to make it complicated for you :).

2) In the above example, the retURL parameter should be your VF page with the standard controller e.g. (/500/e?retURL=%2FYOURVISUALFORCEPAGENAME). In the controller of this page you should create a method which will be called by the page's action parameter, which will redirect back to your original VF page, but this time including the ID in the parameter. The code in the controller should be something like this:

public Case caseRecord {get; set;}

public YOURCONTROLLERNAME(Apexpages.standardController controller)
    caseRecord = (Case)controller.getRecord();

public PageReference redirectBackToOriginalPage()
    return new PageReference('/apex/YOURORIGINALVISUALFROCEPAGE?caseId=' + caseRecord.Id);

3) In your controller of the original VF page, in the constructor or the action method you should then get the ID of the case after we redirect. So you should have something like:

String caseId {get; set;}

    if (String.isNotBlank(Apexpages.currentPage().getParameters().get('caseId')))
        caseId = Apexpages.currentPage().getParameters().get('caseId');
        // now you can use caseID to query more info and display whatever you like on your page

I haven't tested this, you need to tailor it to your existing code.

share|improve this answer
Thank you for your response. I was also thinking of this. But how can I get the newly-created case by using this? – Lance Shi Apr 3 '14 at 1:17
What do you mean by getting the case? Salesforce will automatically redirect to the new case if you use this URL. – Boris Bachovski Apr 3 '14 at 1:18
Yes, but I need to get the new case in original page and display the new case ID. It is something like creating a new object by using look-up field. Does it make sense? – Lance Shi Apr 3 '14 at 1:21
Then in your question you should say "return to the original page and get the case ID" :). I will update my answer shortly. – Boris Bachovski Apr 3 '14 at 1:24
I guess I understand how it works now. Thank you so much for the detailed response. – Lance Shi Apr 3 '14 at 2:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.