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
Steve RossSteve Ross 

Inserting and then editing a case records

Hello,

I have a requirement where a case needcs to be created in a page controller, and then redirected to the case edit page where the user can make futher edits before saving. What I found however is that two recrods are created in this way, one when I insert it in my controller and another when the use clicks save on the case edit page. It was my hope that clicking save would update the existing record, not create a new one. Here is what I'm doing in my controller:     

                Case myCase = new Case();
                .... update myCase fields....
                insert myCase; // insert the case record after various fields have been updated
                casePage = new ApexPages.StandardController(myCase).edit();
                casePage.setRedirect(true);
                return casePage;

My workaround is to redirect to the detail view of the case record and have the user edit and then save.
Any ideas of how to meet my requirement?

Thanks in advance!

Himanshu ParasharHimanshu Parashar
Hi Steve,

I was hoping that it should update the current case only instead of creating a new one and I found the same but if you are facing difficulty. you can try following code. It will work the way you are looking for.
 
PageReference casePage = new PageReference('/' + myCase.id + '/e?retURL=%2F' + mycase.id);
casePage.setRedirect(true);
return casePage;

Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Steve RossSteve Ross
Since you need to do the insert to get a refereance to the record Id. not performing the insert will not work. Sorry if I didn't explain this will enough. Anybody else have an idea on this?