1

I am attempting to grab the value of the "Status" field from an SObject.

@RemoteAction
public static String getStatus() {
    Id NTHRecordId = ApexPages.currentPage().getParameters().get('id');
    String nthRecordQuery = 'SELECT ' +  TransactionContext.getFieldNames('Negotiation_Team_Hub__c') +' FROM Negotiation_Team_Hub__c WHERE id = \''+ NTHRecordId + '\'';
    Negotiation_Team_Hub__c NTHRecord = database.query(nthRecordQuery);
    String status =(String)NTHRecord.get(Schema.Negotiation_Team_Hub__c.Status__c);
    return status;
}

I get below exception

Visualforce Remoting Exception: Attempt to de-reference a null object"

Assuming that I'm going about getting the field in the correct manner (a stretch), why might I be hitting this error?

3
  • The query might be failing and NTHRecord could be null. Check your debug logs for more detials.
    – HSG
    Jun 9, 2015 at 16:21
  • Yeah. check in debug log for exact line of defect.
    – Victor
    Jun 9, 2015 at 16:22
  • 1
    Make the ID value a method parameter i.e. getStatus(Id NTHRecordId); an @RemoteAction isn't related to ApexPages. See Do RemoteAction methods have access to page parameters? - but not the accepted answer.
    – Keith C
    Jun 9, 2015 at 16:26

3 Answers 3

3

The issue is that ApexPages.currentPage() is not available in a Remoting context, therefore this line is throwing a Null Pointer Exception when you attempt to access query-string parameters from a null Page Reference:

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

The solution, as you have already guessed, is to pass any needed parameters along with your Remoting request. Assuming that you are trying to retrieve the Id of the record in context, there are several ways to do that:

  1. If you're using a Standard Controller, retrieve the Id off of the record in context from the Standard Controller. For instance if you're using the Account Standard Controller, then you can get the Id using {!Account.Id}, like this:

    Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.NTHController.getStatus}',
        '{!Account.Id}', 
         function(result, event) {
             console.log(event);
             console.log(result);
         }
    )
    
  2. You can pass along the value of the id URL Query String parameter directly into your Remote Action:

    // Get an object containing all Query String parameters as key/value pairs
    var params = (function(){
        var allParams = {},
            search = window.location.search,
            arr;
        if (search.length) {
            arr = search.substring(1).split('&');
            for (var i=0;i<arr.length;i++) { 
                var parts = arr[i].split('=');
                allParams[parts[0]]=parts[1];
            }
        }
        return allParams;
    })();
    
    Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.NTHController.getStatus}',
        params.id, 
         function(result, event) {
             console.log(event);
             console.log(result);
         }
    )
    

In both cases you will need to adjust your getStatus remote action to accept the Id as a parameter, e.g.

@RemoteAction
public static String getStatus(String NTHRecordId) {
    String nthRecordQuery = 'SELECT ' +  TransactionContext.getFieldNames('Negotiation_Team_Hub__c') +' FROM Negotiation_Team_Hub__c WHERE id = \''+ NTHRecordId + '\'';
    Negotiation_Team_Hub__c NTHRecord = database.query(nthRecordQuery);
    String status =(String)NTHRecord.get(Schema.Negotiation_Team_Hub__c.Status__c);
    return status;
}
0

Based on Keith C's comments, I wound up inserting a parameter into my Javascript remote invocation of the Apex method:

Visualforce.remoting.Manager.invokeAction(
    '{!$RemoteAction.NTHController.getStatus}','{!Id}', function(result, event) {
      console.log(event);
      console.log(result);
    }
  )

This did indeed lead me to a different error:

Visualforce Remoting Exception: List has no rows for assignment to SObject

So the issue was that I wasn't able to get the ID through ApexPages and now need to troubleshoot how to get to the page's ID through the frontend.

1
  • Use your browser's "View Source" to check that {!Id} expanded to an ID; does your controller expose an Id property? It would be more common for it to expose an SObject reference that in turn has an Id property.
    – Keith C
    Jun 9, 2015 at 17:33
0

Please declare a instance variable in class and fetch id in just constructor from page parameters. Whenever you invoke remote action pass that instance variable to remote action as parameter as we cannot use instance variables in static functions so pass instance variable as local variable. Moreover, you need not write this --> Visualforce.remoting.Manager.invokeAction. you can directly call method as we do usually.

Please check the following sample code :-

/* Class : Code */

public class DemoManger {

    public String strRecordId { get; set;}

    public DemoManger() {
        strRecordId = ApexPages.currentPage().getParameters().get('id');
    }

    @RemoteAction
    public static String fetchAccount(String strAccountId ) {

    }
}
<script>

var strAccountId = '{!strRecordId}';
DemoManger.fetchAccount
( strAccountId,
  function(result,event) {
  }
);

</script>

Thanks...!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .