+ Start a Discussion
madhu_ramadhu_ra 

System.currentPageReference() returns null

Hi All,

 

I'm developing simple VF page to edit one of my custom objects and for that I'm using an apex extension. In the extension, I'm refering to 

System.currentPagereference().getParameters().get('Id');

 to query the relevent record. But it's saying am trying to refer a null object. When I do drill down the issue, I got that it's because of my System.currentPageReference()  is returning a null object instead of a pagereference. Is my code is out of the scope? Any idea would be appreciated.

 

I'm doing this inside my remoteAction method like follows

 

@RemoteAction
	Global static void saveRecord(String[] recordValues)
	{
		if(recordValues.size()>0)
		{
			PageReference recordId = System.currentPageReference();//.getParameters().get('id');
			}
}

 

I know there may be lots of other possible ways to do this rather than remoteAction, but this time I need to get the context of the System.currentPageReference() as well.

 

Thanks,

Madhura

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi madhu,

 

Using RemoteAction you can't get the "System.currentPageReference()" in your controller method.

but using javascript you can get the value of your VF page's parameter and Pass that valuein your RemoteAction method.

For more information see below example..

Visualforce Page:

<apex:page controller="remotingController" sidebar="false" showHeader="false" standardStylesheets="false">
    <apex:includescript value="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"/>
    <script>
        function getParam(name){
            name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
            var regexS = "[\\?&]"+name+"=([^&#]*)";
            var regex = new RegExp( regexS );
            var results = regex.exec( window.location.href );
            if( results == null )
            return "";
            else
            return results[1];
        }
        var img_param = getParam('id');
        alert(img_param);
        function Save(){
            var textvalue = $('.texts').text();
            Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.remotingController.renameAlert}', textvalue, img_param,
            function(event){
            alert(event);
            }
            , {escape:true});
        
        return false;
    }
    </script>
    <apex:form >
        <div class = "position">
            <apex:outputLabel value="TextInput"></apex:outputLabel>
            <apex:inputText styleclass="texts" style="margin-left:30px;" />
            <button onclick="Save();">Save </button>
        </div>
    </apex:form>
</apex:page>

 

Apex Class:

global class remotingController  {
    public remotingController(){
    }    
    @RemoteAction  
    global static void  renameAlert(string text, string recordId){       
        system.debug(':::::::text:::::::::::');
        system.debug(':::::::text:::::::::::'+recordId);
    }
}

 

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

All Answers

hitesh90hitesh90

Hi madhu,

 

Using RemoteAction you can't get the "System.currentPageReference()" in your controller method.

but using javascript you can get the value of your VF page's parameter and Pass that valuein your RemoteAction method.

For more information see below example..

Visualforce Page:

<apex:page controller="remotingController" sidebar="false" showHeader="false" standardStylesheets="false">
    <apex:includescript value="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"/>
    <script>
        function getParam(name){
            name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
            var regexS = "[\\?&]"+name+"=([^&#]*)";
            var regex = new RegExp( regexS );
            var results = regex.exec( window.location.href );
            if( results == null )
            return "";
            else
            return results[1];
        }
        var img_param = getParam('id');
        alert(img_param);
        function Save(){
            var textvalue = $('.texts').text();
            Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.remotingController.renameAlert}', textvalue, img_param,
            function(event){
            alert(event);
            }
            , {escape:true});
        
        return false;
    }
    </script>
    <apex:form >
        <div class = "position">
            <apex:outputLabel value="TextInput"></apex:outputLabel>
            <apex:inputText styleclass="texts" style="margin-left:30px;" />
            <button onclick="Save();">Save </button>
        </div>
    </apex:form>
</apex:page>

 

Apex Class:

global class remotingController  {
    public remotingController(){
    }    
    @RemoteAction  
    global static void  renameAlert(string text, string recordId){       
        system.debug(':::::::text:::::::::::');
        system.debug(':::::::text:::::::::::'+recordId);
    }
}

 

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

This was selected as the best answer
madhu_ramadhu_ra

Thanks hitesh90 for the quick response.

Your trick is working. But as I have asked in the question also, what I need to know clearly is the context of System.currentPageReference(). I tried to find out this hear and there but could not. Thanks for addressing it and so accepting your answer.

 

For the id issue what I did as an alternative is just load the assign the id also to a variable since I'm using that objects's standardController. No any extra work needed for that. But I love hitesh90's regex :)

 

Thanks,

Madhura