1

Just configured a new methode in a visualforce page trying to write a variable (ID) from the VisualForce page to the Apex Controller. Console is providing me the following error

Visualforce Remoting: Parameter length does not match remote action parameters: expected 1 parameters, got

Already found a couple of others which encountered this issue, and the solutions varies per project which it encounters.

What is exactly wrong in this code prohibiting it from executing?

VisualForce page:

Window.onload = function loadPage() {
      var pageRecordId = '{!$CurrentPage.parameters.Id}';
      alert(pageRecordId);
  }

The alert is providing me the correct Id. So this method is working well.

Apex Controller:

global with sharing class BudgetGraphController {
global string pageRecordId {get;set;}
@RemoteAction global static List<Budget__c> getBudgetData(string pageRecordId){
List<Budget__c> bdgtGroupListTotal = [Select Project__r.Id, Date__c, Budget_Total__c, Expense_Expected__c From Budget__c WHERE Project__r.Id =: pageRecordId ORDER BY Timing__c ASC];
return bdgtGroupListTotal;}

Method calling the RemoteAction

    // Draw Google chart
function drawTotalChart() {
    //Call remote action method
    BudgetGraphController.getBudgetData(function (result, event) {
        dataTableArray = [];
        var datacolumn = [{label:'Timing', type:'string'}, 
                          {label:'Budget', type:'number'},
                          {label:'Expens/expect', type:'number'}];
        dataTableArray.push(datacolumn);
        //Create the data table
        for(i= 0 ; i < result.length ; i++) {
            var datatableRow = [result[i].Date__c, result[i].Budget_Total__c, result[i].Expense_Expected__c];
            dataTableArray.push(datatableRow);
        }

        var data = google.visualization.arrayToDataTable(dataTableArray);
        var options = {
            title: 'Total',
            focusTarget:'category',
            curveType: 'function',
            lineWidth: 0.75,
            legend: { position: 'bottom' },
            pointSize: 1,
             chartArea: {'width': '80%', 'height': '80%'},
            vAxis: { 
                format:'€',
                minValue: 0,
                viewWindow:{min: 0} 
            },
             hAxis: {
                   slantedText:true,
                   slantedTextAngle:90,
                    },
            crosshair:{
                trigger:'none',
                opacity:0.5,
                }
        };
        var chart = new google.visualization.LineChart(document.getElementById('Total_chart'));
        chart.draw(data, google.charts.Line.convertOptions(options));
    }, { escape: true });
}

Many thanks for providing me some more insight!

2
  • 1
    Can you also show the call to the remote action on your visualforce page? Commented Mar 1, 2020 at 21:11
  • 1
    Hi Casper, just included the code on the original post! Commented Mar 1, 2020 at 21:44

1 Answer 1

3

The total number of parameters you need to use in JavaScript will be the number of Apex parameters plus one for the callback. You neglected to include the required parameter, hence the error. The following change would suffice:

BudgetGraphController.getBudgetData('{!JSENCODE($CurrentPage.paramEters.Id)}', function (result, event) {

Note that:

global string pageRecordId {get;set;}

Is unused and unnecessary for the @RemoteAction to work.

The temporary variable is unnecessary. Note also that __r.Id is not recommended; use __c instead for performance reasons:

return [Select Project__c, Date__c, Budget_Total__c, Expense_Expected__c From Budget__c WHERE Project__c = :pageRecordId ORDER BY Timing__c ASC];

Finally, also note that this is always a Bad Idea:

Window.onload = function loadPage() {

Because Visualforce has its own onload handler that must also load or you can expect unusual behavior. Use window.addEventListener instead:

window.addEventListener('domcontentloaded', function() {
   // ... //
});
1
  • @NickKuppens for RemoteAction, you pass in the methods as a normal function call. You cannot assign to static variables (this technique works for apex:actionFunction and other types of calls). The parameter provided in Apex is how the ID gets there (@RemoteAction global static List<Budget__c> getBudgetData(string pageRecordId)), so we provide that to the remote action call. JSENCODE prevents attacks (see trailhead.salesforce.com/en/content/learn/modules/…). paramters was a misspelling. See edit.
    – sfdcfox
    Commented Mar 2, 2020 at 22:14

You must log in to answer this question.

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