function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aron Schor [Dev]Aron Schor [Dev] 

Problems setting up a custom controller

Hi,

I am having problems setting up a custom controller.  I was able to create one based on a custom object with no problem, but I can't seem to get this to work.  I have tried twice and followed the Trailhead example. Information is below.  Thanks for any help.

-> APEX CLASS

public class CudaController {
    private String sortOrder = 'Sales_Order__C';
public List<Lead> getLeads() {
List<Lead> results = Database.query(
        'SELECT LastModifiedDate, Sales_Order__C, FirstName, LastName, Description' +
        'FROM Lead ' +
        'ORDER BY ' + sortOrder + ' DESC ' +
        'LIMIT 10'
    );
    return results;
}
}

-> PAGE

<apex:page controller="CudaController">
    <apex:form >
        <apex:pageBlock title="Cuda" id="Lead_list">
            <!-- Lead_list -->
            <apex:pageBlockTable value="{! Leads }" var="Lead">
            <apex:column value="{!Lead.LastModifiedDate}"/>
            <apex:column value="{!Lead.Sales_Order__c}"/>
            <apex:column value="{!Lead.FirstName}"/>
            <apex:column value="{!Lead.LastName}"/>
            <apex:column value="{!Lead.Description}"/>
        </apex:pageBlockTable>            
        </apex:pageBlock>
    </apex:form>
</apex:page>

-> ERROR ON PAGE

unexpected token: ORDER 
An unexpected error has occurred. Your development organization has been notified.

-> EMAIL WITH ERROR THAT IS SENT WHEN I VIEW THE PAGE

Visualforce Page: /apex/CudaControllerPage

caused by: System.QueryException: unexpected token: ORDER

Class.CudaController.getLeads: line 4, column 1
             
Vidya DVidya D
There is no space between Descrption and from when Query String is formed.
Use System.debug to print your query and see it executes fine.
Aron Schor [Dev]Aron Schor [Dev]
Thanks Vidya!  I got this work, but one more thing.

        'SELECT LastModifiedDate, Sales_Order__C, FirstName, LastName, Description FROM Lead ' +
        'ORDER BY ' + sortOrder + ' DESC ' +
        'LIMIT 10'

This very odd because my other controller works like this.

        'SELECT Date__C, Company__C, Warehouse__C, Primary_Issue__C, Description_of_the_issue__C, Resolution__C ' +
        'FROM QA__C ' +

How do I add a Where clause?

In Workbench this works
SELECT LastModifiedDate, Sales_Order__C, FirstName, LastName, Description FROM Lead WHERE sales_order__c > '1

None of these work.

'SELECT LastModifiedDate, Sales_Order__C, FirstName, LastName, Description FROM Lead WHERE sales_order__c > '1' ' +
[Error] Error: Compile Error: expecting a right parentheses, found '1' at line 5 column 117

'SELECT LastModifiedDate, Sales_Order__C, FirstName, LastName, Description FROM Lead WHERE sales_order__c > '1' ORDER BY ' + sortOrder + ' ASC ' + LIMIT 10');
Error: Compile Error: line breaks not allowed in string literals at line 5 column -1

Aron
Aron Schor [Dev]Aron Schor [Dev]
Oddly, I can get this query to work, but it seems there is a space

        'SELECT Id, FirstName, LastName, Title, Email, Account.Name, Account.Accounts_Saucony_Guide_7__c ' +
        'FROM Contact ' +
        'WHERE Account.Accounts_Saucony_Guide_7__c > 1 ' +
        'ORDER BY ' + sortOrder + ' ASC ' +
        'LIMIT 10'

However, I am having problems changing the WHERE part.  He are somethings I have tried.

        'WHERE Account.Name LIKE '%Running%' ' +
        'WHERE (IsDeleted = false and Account.Name like '%Running%')
        'WHERE (IsDeleted = false and Account.Name like '%Running%') ' +
        'WHERE (Account.Name LIKE '%Running%') ' +
        'WHERE (Account.Name LIKE '%Running%')

Also, the guest id has access to these objects but when they try viewing these pages it says they don't have permission.

Any ideas?  Thanks.