3

I'm currently finishing an Apex controller which gathers data for a Google chart displayed on a Visual Force Page. The standard Query without the WHERE clause is just working fine for all the data which is present. But once entering the WHERE clause I'm running into errors:

Already found out that RemoteAction is not able to handle to standard method of retrieving the record ID With having methodes in "RemoteAction" they are unable to retrieve page related paramaters, such as the ID. Please find another related page on Stack Exchange: Link Here

'{!$CurrentPage.parameters.Id}' // VisualForce Script

//Custom controller

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

In the console the record ID is displayed correctly, but is referring back to as not defined. Which can also been seen in the URL

enter image description here

The following error is occuring in the console

Uncaught ReferenceError: a0IL000000CAaDEMA1 is not defined

Could someone help me out? Thanks!

2
  • This is just a guess, but have you tried Id instead of id? The example on this page uses Id : developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – gorav
    Commented Feb 29, 2020 at 13:22
  • Tried to change it to "Id", but the same error occurs. Also tried on a Sandbox record but no result and the same error. Also tried to override the value in the preview browser from the Developer Console. No result :(. Commented Feb 29, 2020 at 15:14

3 Answers 3

1

You know what is odd about that error message? Have another look at it:

Uncaught ReferenceError: a0IL000000CAaDEMA1 is not defined

It has the custom object ID in the remote action from {!$CurrentPage.parameters.Id}!

So it is most likely something with the SOQL query itself...

global with sharing class BudgetGraphController {

    @RemoteAction 
    global static List<Budget__c> getBudgetData() {
        List<Budget__c> bdgtGroupListTotal = 
            [select Project__r.Id, Date__c, Budget_Total__c, Expense_Expected__c 
             from Budget__c 
             where Project__r.Id =: '{!$CurrentPage.parameters.Id}' ORDER BY Timing__c ASC];
        return bdgtGroupListTotal;
    }
}

This is the part that stands out to me:

Project__r.Id =: '{!$CurrentPage.parameters.Id}'

I believe those single quotes in combination with the SOQL variable binding syntax are the problem. If you had one or the other you would probably be OK. But using both is causing an issue.

Personally, I try and avoid putting the colon against the equals symbol in SOQL.

So you could try one of the following:

Without binding syntax

        List<Budget__c> bdgtGroupListTotal = 
            [select Project__r.Id, Date__c, Budget_Total__c, Expense_Expected__c 
             from Budget__c 
             where Project__r.Id = '{!$CurrentPage.parameters.Id}' ORDER BY Timing__c ASC];

Or

With Binding Syntax

       List<Budget__c> bdgtGroupListTotal = 
            [select Project__r.Id, Date__c, Budget_Total__c, Expense_Expected__c 
             from Budget__c 
             where Project__r.Id = :{!$CurrentPage.parameters.Id} ORDER BY Timing__c ASC];

What isn't clear to me is why you are using the Visualforce binding syntax in Apex.

2
  • Is {!$CurrentPage.parameters.Id} even a valid merge syntax combination in APEX? For APEX, it should be Project__r.Id =: ApexPages.currentPage().getParameters().get('id') and sync this SOQL query is part of RemoteAction (a Static method) I don't think there is any other way to get the Project ID other than passing it in Function as parameter from Visualforce Page code, where this RemoteAction is being initiated from.
    – VarunC
    Commented Mar 2, 2020 at 22:13
  • 1
    @VarunC agreed. It didn't make sense, and I called it out in my answer. But I left it in place as the important part was the binding syntax vs. single quotes. Or at least from what I can see in the questions code. There are definitely some other questions around how the ID was actually getting into Apex. Commented Mar 2, 2020 at 23:46
0

Try putting the id in the query string, at the end of the url, like this.

?id=Your record Id

2
0

I think your code should read like this:

global with sharing class BudgetGraphController {

    @RemoteAction 
    global static List<Budget__c> getBudgetData(string projectId) {
        List<Budget__c> bdgtGroupListTotal = 
            [select Project__r.Id, Date__c, Budget_Total__c, Expense_Expected__c 
             from Budget__c 
             where Project__r.Id =: projectId ORDER BY Timing__c ASC];
        return bdgtGroupListTotal;
    }
}

In this I've replaced your SOQL query parameter to be a Function Parameter projectId.

And call this method from Visualforce like this:

Visualforce.remoting.Manager.invokeAction(
    '{!$RemoteAction.BudgetGraphController.getBudgetData}',
    '{!$CurrentPage.parameters.id}',
    function(result, event){
        if (event.status) {
            // do anything here
        }
        else {
            alert('error: ' + event.message);
        }
    },
    {escape:true}
);

Here I've called the RemoteAction by passing projectId from Visualforce using {!$CurrentPage.parameters.id}.

You must log in to answer this question.

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