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
Alex Waddell 18Alex Waddell 18 

SOQL query "Order By" causing Test to fail

Hello,

I am getting an error when running my test class. The error is coming from my Controller class where a SOQL query uses the ORDER BY clause

Any ideas on how to overcome this error?
Error
System.QueryException: unexpected token: 'BY'

Class.ResourceListController.getResourceList: line 5, column 1
Class.resourceListTestClass.testMethod1: line 70, column 1
Controller
public class ResourceListController {
   public Id RecordId {get;set;}    
public List<Resource_List__c> getResourceList() {
    RecordId  = ApexPages.currentPage().getParameters().get('id');
    List<Resource_List__c> results = Database.query(
        'SELECT Id, Account__c, Account__r.Name, Distance__c, type__c, Account__r.Phone, Account__r.Description, Account__r.billingStreet, Account__r.billingCity, Account__r.billingPostalCode, Account__r.billingState ' +
        'FROM Resource_List__c ' + 
        'Where Social_Services_Assessment__c = :RecordId' +
        'ORDER BY Type__c asc, Distance__c asc'
    );
    return results;
}
    
    
}
Test Class
@isTest
public class resourceListTestClass {
	static testMethod void testMethod1()
    {
        Account ta = new Account();
        ta.name = 'Alex Demo';
        ta.First_Name__c = 'Alex';
        ta.Surname__c = 'Demo';
        ta.Primary_Insurance__c = 'Test Insurance Company';
        ta.Gender__c ='Male';
        ta.County__c ='Yavapai';
        ta.BillingStreet = '123 West Gurley Street';
        ta.BillingCity = 'Prescott';
        ta.BillingState = 'Arizona';
        ta.BillingPostalCode = '86303';
        ta.RecordTypeId = '012360000004FEPAA2';
        insert ta;
        
        Social_Services_Assesment__c sw = new Social_Services_Assesment__c ();
        sw.Account__c = ta.Id;
        sw.Radius_in_miles__c = 25;
        sw.Monthly_income__c = 5000;
        sw.Do_you_have_enough_money_each_month__c = 'No';
        sw.Do_you_recieve_SSI__c = 'No';
        sw.Do_you_receive_SSDI__c = 'No';
        sw.Do_you_receive_food_assistance__c = 'No';
        sw.Do_you_receive_cash_assistance__c = 'No';
        sw.Do_you_have_adequate_shelter__c = 'No';
        sw.Do_you_need_congregate_meals__c = 'Yes';
        sw.Do_you_need_transportation__c = 'Yes';
        sw.Do_you_have_enough_money_each_month__c = 'No';
        sw.Can_you_pay_for_your_housing__c = 'No';
        sw.Can_you_pay_your_utilities__c = 'No';
        sw.Can_you_pay_for_transportation__c = 'No';
        sw.Can_you_pay_for_medical_visits__c = 'No';
        sw.Are_you_unemployed__c = 'Yes';
        sw.SSI_SSDI_decreased__c = 'Yes';
        sw.RecordTypeId = '0121R000000yZUyQAM';
        insert sw;
        
        Account r1 = new Account ();
        r1.RecordTypeId = '0121R000000yZGLQA2';
        r1.Name = 'Resource 1';
        r1.Food_Bank__c = true;
        r1.Financial_Assistance__c = true;
        insert r1;
        
        Account r2 = new Account ();
        r2.RecordTypeId = '0121R000000yZGLQA2';
        r2.Name = 'Resource 2';
        r2.Food_Bank__c = true;
        r2.Financial_Assistance__c = true;
        insert r2;
        
        Resource_List__c RL1 = new Resource_List__c ();
        RL1.Account__c = r1.Id;
        RL1.Social_Services_Assessment__c = sw.id;
        RL1.Type__c = 'Food Bank';
        insert RL1;
        
        Resource_List__c RL2 = new Resource_List__c ();
        RL2.Account__c = r2.Id;
        RL2.Social_Services_Assessment__c = sw.id;
        RL2.Type__c = 'Food Bank';
        insert RL2;
        
test.StartTest();
PageReference PageRef = Page.ResourceList;
test.setCurrentpage(PageRef);
System.currentPageReference().getParameters().put('RecordId',sw.Id);
ResourceListController obj = new ResourceListController();
obj.getResourceList();
test.StopTest();
        
    }
}



 
Best Answer chosen by Alex Waddell 18
Agustin BAgustin B
Hi, you are missing a space before de ORDER
public class ResourceListController {
   public Id RecordId {get;set;}    
public List<Resource_List__c> getResourceList() {
    RecordId  = ApexPages.currentPage().getParameters().get('id');
    List<Resource_List__c> results = Database.query(
        'SELECT Id, Account__c, Account__r.Name, Distance__c, type__c, Account__r.Phone, Account__r.Description, Account__r.billingStreet, Account__r.billingCity, Account__r.billingPostalCode, Account__r.billingState ' +
        'FROM Resource_List__c ' + 
        'Where Social_Services_Assessment__c = :RecordId ' +
        'ORDER BY Type__c asc, Distance__c asc'
    );
    return results;
}
    
    
}


If it helps please mark as correct, it may help others.

All Answers

Agustin BAgustin B
Hi, you are missing a space before de ORDER
public class ResourceListController {
   public Id RecordId {get;set;}    
public List<Resource_List__c> getResourceList() {
    RecordId  = ApexPages.currentPage().getParameters().get('id');
    List<Resource_List__c> results = Database.query(
        'SELECT Id, Account__c, Account__r.Name, Distance__c, type__c, Account__r.Phone, Account__r.Description, Account__r.billingStreet, Account__r.billingCity, Account__r.billingPostalCode, Account__r.billingState ' +
        'FROM Resource_List__c ' + 
        'Where Social_Services_Assessment__c = :RecordId ' +
        'ORDER BY Type__c asc, Distance__c asc'
    );
    return results;
}
    
    
}


If it helps please mark as correct, it may help others.
This was selected as the best answer
Alex Waddell 18Alex Waddell 18
Wow, what a simple fix! I cant believe something that trivial was giving me so many issues!

Thanks :)
ios man 18ios man 18
I see in Workbench that field OwnerId in Task has a relationshipName of Owner. But AccountId in Task has a relationshipName of Account. So in Apex coding ometv (https://ometv.onl) chatroulette (https://chatroulette.onl) chat avenue (https://chatavenue.vip) I should be using the relationshipName of a field when that field is a Lookup to another sObject, correct? Thanks