4

I have a VF page that uses standard controller account. I want to modify the error message that is displayed when I type the wrong id (invalid) in the URL to avoid getting a message like this: (I want to still display the VFPage but with a custom message, as my VFPage doesnt include header and sidebar ) enter image description here

I have an extension for that page, and within the constructor I have something like this

try{
    this.acctURL   = (Account)controller.getRecord();
}catch(system.DMLException e){
    displayIDError = true;
    errorMessage   = 'Salesforce cant find an account with an ID of: ' + acctIDPage + ' Please contact your Salesforce Administrator';
}

also it is necessary to do:

this.acctURL = (Account)controller.getRecord();

inside a try?

1 Answer 1

4

I don't think there's any way of modifying this page as it's one of the standard error pages produced by the server. This error is generated before your code even gets a chance to run so there's nothing you can do to handle it.

The only thing you could do navigate to a different custom page first that uses a custom controller, that controller could then attempt to verify the ID and type in an init method, and then display an error if it fails or redirect to the existing page if successful.

The custom controller would look a little like this:

public class AccountIdChecker 
{        
    public PageReference Init()
    {
        String acctId = ApexPages.currentPage().getParameters().get('Id');

        if(acctId == '')
        {   
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'No id specified!'));
            return null;
        }

        try
        {
            Id realAcctId = acctId;
            if(Schema.Account.SObjectType != realAcctId.getSobjectType())
            {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Id specified is not an account id!'));
                return null;
            }
        }
        catch (Exception e)
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Bad id specified!'));
            return null;
        }

        <<return reference to existing page here>>
    }
}

And the page would be something simple, like this:

<apex:page controller="AccountIdChecker" action="{!Init}">
    <apex:pageMessages/>
</apex:page>

In answer to the second part of your question, (Account)controller.getRecord(); will always return an account record (using the account standard controller), it'll be a new record if no ID was specified in the URL, or an existin gone if a valid ID is passed in, so you shouldn't need a try/catch block.

3
  • Thank you for the idea, I guess it is not necessary to do this.acctURL = (Account)controller.getRecord(); inside a try right?
    – manza
    Nov 28, 2014 at 1:26
  • No, just added that after you accepted, missed it the first time around. Not sure what type acctURL is in your code, but it should be an Account SObject.
    – Matt Lacey
    Nov 28, 2014 at 1:27
  • yeah, i had something like:public Account acctURL { get; set; }, i just didnt want to add all the code. Cool Thank you very much!!!
    – manza
    Nov 28, 2014 at 1:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .