2

I have a remoting method called from a controller which is given below

@RemoteAction
public static PageReference openLocatorPage(String addressId){
    Address__c addr = [SELECT Id, Name, Latitude__c, Longitude__c FROM Address__c WHERE Id = :addressId];
    Decimal latitude = addr.Latitude__c;
    Decimal longitude = addr.Longitude__c;
    String urlLocatorPage = '/apex/LeadLocator?lat='+latitude+'&lon='+longitude;
    PageReference pr = new PageReference(urlLocatorPage);
    return pr;
}

and my code in javascript using this controller is

LeadLocatorPreProcessorControllerV2.openLocatorPage($scope.CustomerSite.AddressID,function(results,event){
    console.log(results);
    //window.open(results,"_self");
});

Now the problem I am facing is that my url should be

/apex/LeadLocator?lat=1234&lon=1234

but instead my URL comes out to be

/apex/LeadLocator?lat=1234&lon=1234

any help regarding this amp; being appended to my URl would be appreciated.

thanks

3

That & is the HMTL entity equivalent of the ampersand character.

I'm afraid that I don't have anything on hand that I can test this with, but after looking through the documentation on javascript remoting, I came across Configuring a JavaScript Remoting Request.

There is an escape parameter that defaults to true. This could be what is responsible for escaping your ampersand.

If that is the case, then you should be able to disable the HTML escaping by passing a configuration object as the third parameter to the remote method call and setting the escape value to false.

LeadLocatorPreProcessorControllerV2.openLocatorPage(
    $scope.CustomerSite.AddressID,
    function(results,event){
        console.log(results);
        //window.open(results,"_self");
    },
    {'buffer': true, 'escape': false, 'timeout': 30000 }
);
  • This will almost certainly fix the issue. – Caspar Harmer Sep 19 '16 at 17:47
  • Is there any reason to use PageReference instead of just returning a simple url as String from the RemoteAction? This isn't the first time that I saw this. – smukov Sep 19 '16 at 17:55
  • @smukov About the only thing that I can think of is that using PageReference will automatically URL-encode the query string. You can also call an @RemoteAction annotated method via apex just like any other method, so perhaps it's being used elsewhere. – Derek F Sep 19 '16 at 18:23
  • @DerekF Hmm, good points. Thank you for this information. – smukov Sep 19 '16 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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