Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 1 down vote accepted

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>
share|improve this answer
    
Thanks Himanshu. I am returning this error 'No remoted actions found to resolve '$RemoteAction.ChartController.getDatatable'. Any idea? Thanks – Andy Hitchings Nov 12 '15 at 14:47
    
in which controller you have defined your remote action? – Himanshu Nov 12 '15 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"> – Andy Hitchings Nov 12 '15 at 14:51
    
that is correct :) – Himanshu Nov 12 '15 at 14:52
    
Fantastic - this is working now. Many thanks, Himanshu. – Andy Hitchings Nov 12 '15 at 14:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.