Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a Visualforce page that renders part of library content in it, but I also give them the option to type in a search which will renavigate them to the default Libraries tab with the search option they filled in earlier. For some reason when I redirect them via Page Reference, it clears out the information on the URL. For now I have values hardcoded in, but I'm just trying to get the simpliest things working first.

public PageReference doSearch()
{
  PageReference pRef = new PageReference('/sfc/#search?searchTerm=testing&shepherdToken=3hhlYajWjfdqOLQNlORGloGav8BWt7ZKFeAwWdELQSKmmZaWOmcS8usuaXys9gtgqdAP0DGsg69Fb6FZFclETy7UVbW1V1HKh4uIhQMYPAQ_MgNvEpDHNC1UTzyoCY8xaFlZkcC3ZGTLmADq8V7mVDsV3B8%3D&searchPageNum=0&searchWorkspaceIds=%5B%22ALL_WORKSPACES%22%5D&masterWorkspaceId=ALL_WORKSPACES');

  pRef.setRedirect(true);
  return pRef;
}

I arrive at the Libraries page.... but it clears the URL to /sfc/#search.

EDIT: I have also tried using pRef.setRedirect(false);

share|improve this question

2 Answers 2

Your URI is invalid according to RFC 3986: the fragment part (the "#search") should come after the query part (the "?searchTerm..."). If you fix that, the PageReference will correctly parse the query terms rather than throwing them away. Its a pity PageReference silently discards the query terms rather than throwing an exception.

(You can confirm this by writing a unit test using your current URI and the corrected URI.)

PS

Here is a test case fail illustrating that having the anchor at the beginning results in the query string getting made part of the anchor. The test pass1 shows that removing the anchor fixes the query string. And pass2 shows that putting the anchor at the end results in both the query string and anchor being correctly parsed.

@isTest
private class Test {

    @isTest
    static void fail() {
        PageReference pr = new PageReference('/sfc/#search?searchTerm=testing&shepherdToken=3hhlYajWjfdqOLQNlORGloGav8BWt7ZKFeAwWdELQSKmmZaWOmcS8usuaXys9gtgqdAP0DGsg69Fb6FZFclETy7UVbW1V1HKh4uIhQMYPAQ_MgNvEpDHNC1UTzyoCY8xaFlZkcC3ZGTLmADq8V7mVDsV3B8%3D&searchPageNum=0&searchWorkspaceIds=%5B%22ALL_WORKSPACES%22%5D&masterWorkspaceId=ALL_WORKSPACES');
        // The problem: the query string gets included in the anchor
        System.assertEquals('search?searchTerm=testing&shepherdToken=3hhlYajWjfdqOLQNlORGloGav8BWt7ZKFeAwWdELQSKmmZaWOmcS8usuaXys9gtgqdAP0DGsg69Fb6FZFclETy7UVbW1V1HKh4uIhQMYPAQ_MgNvEpDHNC1UTzyoCY8xaFlZkcC3ZGTLmADq8V7mVDsV3B8%3D&searchPageNum=0&searchWorkspaceIds=%5B%22ALL_WORKSPACES%22%5D&masterWorkspaceId=ALL_WORKSPACES', pr.getAnchor());
        System.assertEquals(0, pr.getParameters().size());
    }

    @isTest
    static void pass1() {
        PageReference pr = new PageReference('/sfc/?searchTerm=testing&shepherdToken=3hhlYajWjfdqOLQNlORGloGav8BWt7ZKFeAwWdELQSKmmZaWOmcS8usuaXys9gtgqdAP0DGsg69Fb6FZFclETy7UVbW1V1HKh4uIhQMYPAQ_MgNvEpDHNC1UTzyoCY8xaFlZkcC3ZGTLmADq8V7mVDsV3B8%3D&searchPageNum=0&searchWorkspaceIds=%5B%22ALL_WORKSPACES%22%5D&masterWorkspaceId=ALL_WORKSPACES');
        System.assertEquals(null, pr.getAnchor());
        System.assertEquals(5, pr.getParameters().size());
    }

    @isTest
    static void pass2() {
        PageReference pr = new PageReference('/sfc/?searchTerm=testing&shepherdToken=3hhlYajWjfdqOLQNlORGloGav8BWt7ZKFeAwWdELQSKmmZaWOmcS8usuaXys9gtgqdAP0DGsg69Fb6FZFclETy7UVbW1V1HKh4uIhQMYPAQ_MgNvEpDHNC1UTzyoCY8xaFlZkcC3ZGTLmADq8V7mVDsV3B8%3D&searchPageNum=0&searchWorkspaceIds=%5B%22ALL_WORKSPACES%22%5D&masterWorkspaceId=ALL_WORKSPACES#search');
        System.assertEquals('search', pr.getAnchor());
        System.assertEquals(5, pr.getParameters().size());
    }
}
share|improve this answer
    
That's really interesting... because Salesforce itself builds it that way. I can even get it to copy/paste in the browser and it works. Just the PageReference discards it. Do you have any ideas then on how to make my navigation work? I'm not really sure how to fix that. –  dphil May 5 at 15:52
    
@dphil All I can suggest is to do some string parsing of the URI yourself e.g. move the #search to the end. Guess this must be one of those standards that is so often ignored that most platforms are not too fussy. –  Keith C May 5 at 16:35
    
It doesn't work if I move the #search unfortunately. –  dphil May 5 at 16:36
    
@dphil I've added my test case; pass2 suggests both the query string and anchor can work. –  Keith C May 5 at 16:55
    
Well, at least it's loading the page now... but it doesn't actually do the filtered search if the #search isn't directly behind the /. When I place it at the end like yours, it's as if I simply just navigated to the page normally without filters. I'll +1 for the help, but it still hasn't fixed my problem. –  dphil May 5 at 17:10
up vote 1 down vote accepted

So far the only workaround I found is to use onclick javascript to navigate me away. I prefer using my controller for this, but as of now this is my only solution:

function navigateAway()
{
  window.location.href = 'https://cs7.salesforce.com/sfc/#search?searchTerm=testing&searchPageNum=0&searchWorkspaceIds=%5B%22ALL_WORKSPACES%22%5D&masterWorkspaceId=ALL_WORKSPACES';
}
share|improve this answer
    
Seems like a reasonable solution given that the PageReference one would require potentially fragile code and unit tests. –  Keith C May 5 at 16:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.