Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?

share|improve this question
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 at 5:22
1  
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. –  brovasi Apr 3 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 at 5:31
    
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 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 at 6:27
show 2 more comments

1 Answer 1

up vote 4 down vote accepted

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%.

share|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 at 22:48
    
Yes - we have an interim page with a custom controller. –  Bob Buzzard Apr 4 at 7:28
add comment

Your Answer

 
discard

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.