10

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?

13

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

|improve this answer|||||
  • Wow, I've been saving it to a hidden field on page load and then grabbing it later with JS. Didn't realize there was a VF global variable that handles it natively. Thanks! – geeljire May 1 '13 at 0:08
  • 1
    If you haven't already, google salesforce global variables. There are lots of handy things like user info, page info, session info, site info, etc. – Shane McLaughlin May 1 '13 at 1:51
10

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>
|improve this answer|||||

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.