1

I have a @RemoteAction method which return a List of opportunities via a SOQL statement that needs to use the dynamic 'WHERE Account.Id =: ApexPages.currentPage().getParameters().get('id')'.

This wasn't working though because it transpires, as per the docs, that JavaScript remoting is async and therefore, there is no PageReference context.

The solution apparently, as per ApexPages.currentPage().getParameters with javascript remoting, is to pass the parameter(s) explictly to a a method from JavaScript.

My code is below and although I am not getting any errors when I save the respective VF page (it's actually a VF component, but I don't think that matters) or the method, the data is not being displayed as expected on the Visualforce page.

Can someone please advise?

Thanks,

  @RemoteAction //Remote Action to expose method to JavaScript Remoting
public static List<Opportunity> getDatatable(Id accountid){
   return [select name, stagename, createddate, amount from Opportunity WHERE Account.Id=: accountid ORDER by Name ASC ];
           } 

 "fnInitComplete": function(oSettings) { 
                  ChartController.getDatatable('{!$currentPage.parameters.id}')(function(result, event){

FYI to readers: another good sources to read: Do RemoteAction methods have access to page parameters?

1 Answer 1

3

Remote action can be called with following syntax.

<script type="text/javascript">
    "fnInitComplete": function(oSettings) { 

        var recordid='{!$currentPage.parameters.id}';
        alert(recordid);
        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.ChartController.getDatatable}',
            recordid, 
            function(result, event){
                if (event.status) {

                    //your success code

                } else if (event.type === 'exception') {
                    //Your exception handling
                } else {

                }
            }, 
            {escape: true}
        );
    }
    </script>
5
  • Thanks Himanshu. I am returning this error 'No remoted actions found to resolve '$RemoteAction.ChartController.getDatatable'. Any idea? Thanks Nov 12, 2015 at 14:47
  • in which controller you have defined your remote action?
    – Himanshu
    Nov 12, 2015 at 14:49
  • Ah - sorry, I think I know what the problem was...It was a component that was marked as <apex:component>...I have now added <apex:component controller="ChartController"> Nov 12, 2015 at 14:51
  • that is correct :)
    – Himanshu
    Nov 12, 2015 at 14:52
  • Fantastic - this is working now. Many thanks, Himanshu. Nov 12, 2015 at 14:52

You must log in to answer this question.

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