2

Given a "save" method such as this:

public PageReference save() {

    try {
        insert current_search;
    } catch(System.DMLException e) {
        ApexPages.addMessages(e);
        return null;
    }

    // After successful Save, navigate to the page that shows SearchResult__c objects that are children
    // of this Search__c. 
    PageReference redirectSuccess = new PageReference('/apex/SearchResult/' + current_search.Id);
    return (redirectSuccess);
}

I expect to end up at URL that looks like this:

https://na35.visual.force.com/apex/SearchResult/a0041000001QFAAO

but instead I end up here:

https://na35.visual.force.com/apex/a0041000001QFAAO

and I see the message:

Page a0041000001QFEmAAO does not exist

How do I redirect to here?

https://na35.visual.force.com/apex/SearchResult/a0041000001QFAAO

3
  • Is the current code saved as with searchresult in the pagereference
    – Eric
    Sep 12, 2016 at 22:18
  • 1
    Is that even valid? Shouldn't it be ?id=a00... instead of /a00...?
    – Adrian Larson
    Sep 12, 2016 at 22:19
  • are you sure the save() method is getting called and there is not another place where the redirecturl is being populated?
    – Rao
    Sep 12, 2016 at 22:26

1 Answer 1

7

The "apex" resource URL ignores path names, and only uses the last file name on the path. I tested this in my org with:

/apex/pages/TestPage

And it resolved to:

/apex/TestPage

I actually didn't know about this behavior before, and I certainly wouldn't depend on it, but you definitely need to use a parameter instead of part of the file path:

PageReference redirectSuccess = new PageReference('/apex/SearchResult?id=' + current_search.Id);
3
  • And it has to be the fox who answers such oddities in the platform
    – Rao
    Sep 12, 2016 at 22:27
  • Sounds like my hunch was right. Interesting finding.
    – Adrian Larson
    Sep 12, 2016 at 23:02
  • 1
    @AdrianLarson Surprising, really. I don't think I've ever intentionally tried to break /apex/ before, but there we have it. Always something new to learn in Salesforce.
    – sfdcfox
    Sep 12, 2016 at 23:07

You must log in to answer this question.

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