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
insanedukeposseinsanedukeposse 

best way to handle custom controller constructor when no record id (URL parameter) is passed?

I have a problem I'd like some feedback on. This only happens occasionally, but it is annoying to everybody involved.

 

I have a custom controller that expects that have an id passed as a parameter. If the id is passed correctly, the contructor loads the record no problem. However, when an action happens to the record, the URL changes from /apex/myAccount?acctid=0014000000aaaaa to simply /apex/myAccount. This is no problem within the context of this session of this controller. However, the next thing that happens is that someone bookmarks this page - they don't finish their work or they need to come back to this record later, etc.

 

I have a sample of how I handle this situation below, but I'm wondering if there is a better way.

 

You'll notice that what I do to prevent an error from being thrown when no record is returned, and to prevent the rest of the controller code from blowing up, and the VF from thowing an error, is to create a new/blank record. This is NOT ideal. You'll also notice that Salesforce core platform logic handles this in a very unique way with the "URL Does Not Exist" message you get if you put in a bogus salesforce id into the URL. 

 

Thanks for the feedback!

 

Controller - with 2 different methods that wipe out the URL parameters...

 

 

public class myController{ public Account myAccount {get; set;} //constructor public myController() { string id = System.currentPageReference().getParameters().get('acctid'); //get temp record Account[] tempAccount = [select id, name from account where id = :id]; //did you get an actual record? if (tempAccount.size() == 1) { myAccount = tempAccount[0]; } else { ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Error , 'Oops! We could not load your record.'); ApexPages.addMessage(myMsg); myAccount = new Account(); } } public void dosomething (){ myAccount.Name = 'Elvis.' + myAccount.Name; } public PageReference dosomethingelse () { myAccount.Name = myAccount.Name + '.Presley' ; return null; } }

 

 

 

 Here's the VF...

 

 

<apex:page tabstyle="Account" Controller="myController"> <apex:pageMessages /> <apex:form id="form"> <apex:pageBlock > <apex:pageBlockButtons > <apex:commandButton action="{!dosomething}" value="Do Something" /> <apex:commandButton action="{!dosomethingelse}" value="Do Something Else" /> </apex:pageBlockButtons> <apex:pageBlockSection > <apex:inputField value="{!myAccount.Name}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

Ditto what BritishBoy said... just check the parameter for null.

 

As to what to do, I'd probably just send them to "/001" as that's the overview page for Accounts.

 

Another alternative if you think your users would be confused by that is to stay on the same visualforce page, put up a nice message saying "Which Account do you wish to Edit" and then put up a single Account Lookup box so they can choose an Account, and then you can proceed from there.

 

Best, Steve.

All Answers

BritishBoyinDCBritishBoyinDC

What do you want to actually happen in this situation?

 

Re capturing the error if there is no Id passed in, I think you can just use this construct:

 

 

if (ApexPages.currentPage().getParameters().get('acctid') != null) { //fetch account } else { //handle error - redirect to a different page etc? }

 

 

 

SteveBowerSteveBower

Ditto what BritishBoy said... just check the parameter for null.

 

As to what to do, I'd probably just send them to "/001" as that's the overview page for Accounts.

 

Another alternative if you think your users would be confused by that is to stay on the same visualforce page, put up a nice message saying "Which Account do you wish to Edit" and then put up a single Account Lookup box so they can choose an Account, and then you can proceed from there.

 

Best, Steve.

This was selected as the best answer
insanedukeposseinsanedukeposse
Thanks guys. Those suggestions are along the lines of what I was looking at doing. They require a tiny bit more work, and I have to get my entire team to be consistent in their approach. I was hoping for a magic bullet, but no such luck.