Do RemoteAction methods have access to page parameters?

All we need is an easy explanation of the problem, so here it is.

I’m getting a null exception for attempting to grab a record id from the url query params: ApexPages.Currentpage().getParameters(‘id’). Can RemoteAction methods access these parameters?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

No, not from apex. But there’s an easy workaround:

Pass them in from the script on the page using {!$currentpage.parameters.something}

You still need to check for nulls in the code because it might legitimately be null

Method 2

Can RemoteAction methods access these parameters?

Nope 🙂

By their very nature, RemoteAction methods are stateless. If you check out the HTTP request itself, it doesn’t carry information about the invoking page or the request parameters on it.

If you need to operate on a specific record, you can reflect the Id in the remote method signature and then transmit it with the remote action invokation. For example:

Extension.cls

public class Extension {
  @RemoteAction static public void convert(Id recordId) {
    Object__c obj = [SELECT Status__c FROM Object__c WHERE Id = :recordId];
    obj.Status__c = 'converted';
    update obj;
  }
}

Converter.page

<apex:page standardController="Object__c" extensions="Extension">
  <apex:commandButton
    onclick="{!$RemoteAction.Extension.convert}('{!Id}', function() {});return false;"
    value="Convert"
  />
</apex:page>

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply