As a follow-up to my first question, "How to create a new case and return the case by clicking a button" -- I need to extend the standard controller case with an extension, but when I create a new case, I need the CASE-ID for the extension.

I know the code to get the CASE-ID would be:

caseRecord = (Case)controller.getRecord();

...but this does not work, since a new case does not appear to have a CASE-ID until after it is saved.

How to get Case ID on a new Case page?

  • 1
    Can you please explain what are you trying to achieve.?? When are you opening this page..?? On new button..or on detail page of case..?? – miku Apr 3 '14 at 5:22
  • 2
    Your case doesn't exist yet when you click on new, you need to save the case first, so you will never get the id of the case of course. You might insert the case into the constructor of your extension and default all the values. – Cloud Ninja Apr 3 '14 at 5:28
  • @brovasi: Understand that you feel you don't have a complete answer, but appears that your comment is more of an answer than a comment. Why not just post an answer and request the original poster clarify the body of their question and/or post a comment to your answer? – blunders Apr 3 '14 at 5:31
  • 1
    You can't get the recordid of the case before saving..After you insert the case ..Like a dml operation "insert c" then you can get the ID using "c.id" We can never get the ID before any record is created... – miku Apr 3 '14 at 5:41
  • 1
    Guessing that if you create a case with null values, then allow the user to edit the case, you would be able to get the ID because the case is already created. Issue is that if the user does not really create the case, then you'll have to deal with that, which is why cases are not created until they're created; meaning until they have an ID, they're not created, so there is no need to undo them being created. – blunders Apr 3 '14 at 6:27

You can't get access to the id until the record is saved, as no id exists prior to that point. The way I've handled this in the past is to override the new button with a small Visualforce page that has an action attribute on the page component. The action method invoked by this inserts a case with the minimum number of fields populated and then redirects the user to the edit page for the new case.

The downside to this is that you can end up with orphaned records - i.e. those that were created through this mechanism and then the user changed their mind. There are a few ways to try to mitigate against this (overriding the edit page so that cancel deletes the case, the first edit taking place in a visualforce page, scheduled apex to clean them up), none of which solve the problem 100%.

|improve this answer|||||
  • Thank you for your response, Bob. If I understand your method correctly, you are not using the standard controller to insert the new case. Instead, you insert the new case using the action method. Is that correct? – Lance Shi Apr 3 '14 at 22:48
  • Yes - we have an interim page with a custom controller. – Bob Buzzard Apr 4 '14 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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