+ Start a Discussion

Catching Missing Id in Standard Controller/Extension

I feel this should be easy, but I'm just missing something...


I have a page header like this for page cmupdate, to update a Campaign Member Status


<apex:page standardController="CampaignMember" extensions="UpdateCampaignMemberClass" sidebar="false" showHeader="false" action="{!checkidvalid}" >

 The checkidvalid method is designed to be called by the page action to check if there is no Id (e.g. a merge failed in an email)



If the page is called like this, the user is correctly redirected to an error page





But if the merge failed, the page would more likely look like this:





And then the error page is not called - you just get the {checkmanageability} reference in the debug logs...and a VF page error...


Anything I can do to catch this sort of error with an standard controller/extension construct?






Just wanted to check, do your conditions include a check for a NULL parameter values as well as a BLANK i.e. '' parameter values?




Yeah - it just never seems to get to the Controller if there is no Id - Vf seems to error out before it even initiates anything..


Here's the controller...



public class UpdateCampaignMemberClass{

public final CampaignMember cm;
private ApexPages.StandardController cmController;
public UpdateCampaignMemberClass(ApexPages.StandardController controller) {
        cmController = controller;
        this.cm= (CampaignMember)controller.getRecord();

Public PageReference checkidvalid(){
string cmid = ApexPages.currentPage().getParameters().get('id');
PageReference pdf = new PageReference('/apex/ppartyerror');
system.debug('id?' + cmid);
if (cmid==null){
return pdf; //action that does the redirrecting
//keep going
return null;




If I guessed I'd say that since the StandardController uses the id parm to identify which record you want to look at it's :


1. Picking up that the id parm value when the id key specified, even though the value is empty/null; it fails; and your extension doesn't see the light of day.

2. When their is no id parm at all the standardcontroller is handling the error in some way we can't see (like a Java default contructor?) and allowing your extension to run the constructor.


This is guess of course, and it seems like a bit on inconsistent behaviour. Try tweeting it with the #askforce hashtag, there are some peeps there that work on the code underlying VF and Apex.




is the error page using the same controller or extension? If so, then sfdc considers it a refresh of the same page. Maybe adding a setRedirect=true to the pageReference will help.


Check here for details:

VF postback (see at bottom of page und 4.)


Apex Docu for PageReference


It is, but the issue is more fundamental than that. If I just use the page name without the ?id= at the end, the error page is correctly rendered.


But when I call the page with the ?id= but with no actual Id, VF seems to immediately throw an error before any of my code runs - it appears that the StandardController is hardwired to receive an ID in this circumstance, and won't fail nicely if it doesn't?


I'd say that the right time to check for the null id is earlier in the process--*before* the user gets forwarded to cmupdate. E.g.,



<apex:page controller="idValidateController" sidebar="false" showHeader="false" action="{!checkIdValid}" >



And the checkIdValid method forwards to cmupdate (with the id parameter passed along) if the id checks out, and goes straight to your error page if it doesn't. idValidateController doesn't need much in it; just the checkIdValid method.




I think I got it ...


you need a second constructor that doesn't take any arguments. Check the Apex Docu for Classes and Constructors, it is described in there. I will try to post an example later, gotta run now