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
Simon BairdSimon Baird 

Adding Limit to Database.query

Hi there,

I need to limit the number of records returned from this query. When i use the LIMIT clause before the last semi colon I get an error "mismatched input expecting semi colon". Any thoughts guys?

 public List<Event> start(){
      String query = 'Select Id,IsRecurrence,Event_Processed__c,Allowance_Type__c,Session_Coverage_Type__c,Session_Type__c,Message_from_Office_to_Podiatrist__c,Additional_Message_From_Office_to_Pod__c,Subject,OwnerId,owner.Name,WhatId,startdatetime,of_Session_Sharing_Day__c,Wing_1__c,Wing_2__c,Wing_3__c,Wing_4__c,Wing_5__c,Wing_6__c,Wing_7__c,Wing_8__c,Wing_9__c,Rooms_1__c,Rooms_2__c,Rooms_3__c,Rooms_4__c FROM Event WHERE Do_Not_Create_Session__c = false AND IsRecurrence = false AND Event_Processed__c = false AND RecordType.Name IN :lstEventRecordTypes AND What.RecordType.Name IN :lstAccountRecordTypes AND Owner.Title IN :lstOwnerTitles AND ActivityDate = NEXT_N_DAYS :'+ lastNumberOfDays ; 
      return Database.query(query);
Best Answer chosen by Simon Baird
Mahesh DMahesh D
Hi Simon,

Please check the below code:
 
public List<Event> start(){
    String query = 'Select Id,IsRecurrence,Event_Processed__c,Allowance_Type__c,Session_Coverage_Type__c,Session_Type__c,Message_from_Office_to_Podiatrist__c,Additional_Message_From_Office_to_Pod__c,Subject,OwnerId,owner.Name,WhatId,startdatetime,of_Session_Sharing_Day__c,Wing_1__c,Wing_2__c,Wing_3__c,Wing_4__c,Wing_5__c,Wing_6__c,Wing_7__c,Wing_8__c,Wing_9__c,Rooms_1__c,Rooms_2__c,Rooms_3__c,Rooms_4__c FROM Event WHERE Do_Not_Create_Session__c = false AND IsRecurrence = false AND Event_Processed__c = false AND RecordType.Name IN :lstEventRecordTypes AND What.RecordType.Name IN :lstAccountRecordTypes AND Owner.Title IN :lstOwnerTitles AND ActivityDate = NEXT_N_DAYS :'+ lastNumberOfDays+' LIMIT 10'; 
    return Database.query(query);
}

Please do let me know if it helps you.

Regards,
Mahesh

All Answers

Mahesh DMahesh D
Hi Simon,

Please check the below code:
 
public List<Event> start(){
    String query = 'Select Id,IsRecurrence,Event_Processed__c,Allowance_Type__c,Session_Coverage_Type__c,Session_Type__c,Message_from_Office_to_Podiatrist__c,Additional_Message_From_Office_to_Pod__c,Subject,OwnerId,owner.Name,WhatId,startdatetime,of_Session_Sharing_Day__c,Wing_1__c,Wing_2__c,Wing_3__c,Wing_4__c,Wing_5__c,Wing_6__c,Wing_7__c,Wing_8__c,Wing_9__c,Rooms_1__c,Rooms_2__c,Rooms_3__c,Rooms_4__c FROM Event WHERE Do_Not_Create_Session__c = false AND IsRecurrence = false AND Event_Processed__c = false AND RecordType.Name IN :lstEventRecordTypes AND What.RecordType.Name IN :lstAccountRecordTypes AND Owner.Title IN :lstOwnerTitles AND ActivityDate = NEXT_N_DAYS :'+ lastNumberOfDays+' LIMIT 10'; 
    return Database.query(query);
}

Please do let me know if it helps you.

Regards,
Mahesh
This was selected as the best answer
Simon BairdSimon Baird
Thanks Mahesh