All over the web I have seen that I should get information from the current page using the following line:

Id contactId = ApexPages.currentPage().getParameters().get('id');

I need the Id. I have the following code:

public with sharing class ResetPinController {
    public static Id getId() {
        System.debug('About to get the id...');
        Id contactId = ApexPages.currentPage().getParameters().get('id');
        return contactId;

But I get the following:

16:54:16:002 USER_DEBUG [4]|DEBUG|About to get the id...
16:54:16:002 FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object

What am I doing wrong? How can I get information from the page? I am aware I can do it with a trigger but that isn't what I'm asking.

  • 1
    in what context? lightning experience, communities? is it a VF page in LEX? – glls Apr 30 '18 at 14:05
  • @glls Lightning experience – Dave Chambers Apr 30 '18 at 14:08
  • As per my knowledge, ApexPages.currentPage().getParameters().get('id') works only on the sObject's record detail page. If you try to use VF page/Lightning component on an Standalone app, you might not be able to get any id. Please, anyone can correct me if I am wrong here(I am learning too). – Satya S. Vema Apr 30 '18 at 14:11
  • @ArnoldJr. Thanks - I guess that makes sense. I hope somebody can give me the correct code in this context. – Dave Chambers Apr 30 '18 at 14:13
  • @Dave Chambers, your apex class is AuraEnabled, which leads me to believe, that you are invoking your apex method from a lightning component, is this the case? – glls Apr 30 '18 at 14:13

When you buid lightning components, if you want to pass, a record id (for example), you will want to make it "context aware" --> you might want to check Building Context-Aware Lightning Components. As @Sfdcfox mentioned, you need to pass the parameter from your client side controller to your server side controller.

If your component is used on a record page, you can use


and define an attribute so you can fetch the id from your client side controller

<aura:attribute name="recordId" type="Id" />

To get the Id from your client side controller:


and pass it as a parameter to your server side controller:

            "yourId": component.get("v.recordId")

The linked blog should help you get started

| improve this answer | |
  • Amazingggggg!!!!!!!!! – Satya S. Vema Apr 30 '18 at 14:31
  • @glls Brilliant! Thanks very much. I will try that out. – Dave Chambers Apr 30 '18 at 14:31

Apex.currentPage() only works in Visualforce. In Lightning, you are expected to pass any parameters you need to your controller. You can't get anything from the page, but you can get UserInfo data if you desire, plus any data you query from the database, custom settings, cache, etc. Your Apex Code would look like this:

@AuraEnabled public static void doSomething(Id recordId) {

If you want any parameters from the page, you can read window.location attributes in the client and pass the data in to your method.

| improve this answer | |
  • Ok thanks so in the JavaScript I can get params from window.location? – Dave Chambers Apr 30 '18 at 14:19
  • you will want to leverage lightning methods to fetch page parameteers or record id's – glls Apr 30 '18 at 14:21
  • @DaveChambers yes, you can read window.location to get the current information. If you specifically want the ID of the record you're on, use force:hasRecordId. – sfdcfox Apr 30 '18 at 14:22

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.