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
SteveBowerSteveBower 

Are my URL Parameters being reordered?

In a controller class I have:

p = new PageReference('/apex/CreateContactUpdateRecordStep1?original=' + original.id +
'&contact=' + contact.id + '&id=' + indoubt.id);

In a testing class I have:
String url = nextPage.getUrl();
...
System.assertEquals(url,
'/apex/CreateContactUpdateRecordStep1' +
'?original=' + control.original.id +
'&contact=' + control.contact.id +
'&id=' + control.indoubt.id);

The data is all correct, however the assertion fails because the order of the parameters in the string
returned from the getURL call is different from the order specified in the new PageReference call.

The URL returns '/apex/CreateContactUpdateRecordStep1?contact=xxxxx&id=yyyyy&original=zzzzz'

It's as if the parameters were reordered alphabetically? (Might this be because I'm adding my parameters as part of a string instead of using p.getParameters().put('original',zzzzz) etc?

Now that I write this, I'll test with the put() method... I'll bet the url is returned by dumping the alphabetically ordered parameter Map. (If so, it might help if it were documented somewhere so others
could avoid it.)

-Steve.
Ron HessRon Hess
The pageReference class has a map of parameters, but a map is an unordered list, so when getUrl() extracts these for you, no order is provided.

I don't think you can rely on an order for these page parameter map items, alpha or otherwise.
SteveBowerSteveBower
Yeah, you have to do it yourself with an assertion for each parameter you want to check.

And, to check the base of the url, since getUrl() returns the whole thing, with parameters, you have to strip off the parameters yourself to get the base.

Ah well.  :-)  Thanks, Steve.

Ron HessRon Hess
can't you use currentpage().getParameters() and assert that the map has what you expect?
SteveBowerSteveBower
Sure, but isn't that essentially the same as using something like:

kaos = new CreateContactUpdateRecordController(stdcontrol);
pageRefUpdate.getParameters().clear();
pageRefUpdate.getParameters().put('id', c.id);
Test.setCurrentPage(pageRefUpdate);
nextPage = kaos.myFunction(); //

and then checking the URL and Parameter map that has been returned in nextPage? Isn't ApexPages.currentPage() pointing to the same page that has just been returned from my controller? nextPage in this case.

Regardless of where I get the Parameter map from, currentpage(), or one I return myself, I'll still need to check the map one parameter at a time. And if I want to verify that the pageReference returned from a method is '/apex/Pagetwo', I still have to check that against the base of currentpage().getURL(), or nextPage.getURL().

Or have I missed something? The above all seems to work just fine. Thanks, Steve.