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
Ckevin1984Ckevin1984 

Question about soql

How can i reach the last record in the for loop as following:

for (Timesheet__c timeSheet: [select Total_Hours__c, Hours__c from Timesheet__c])

I want to do some operation in the last timesheet. Thanks in advance for any help.

Best Answer chosen by Admin (Salesforce Developers) 
TomSnyderTomSnyder

consider :

 

 Timesheet__c ts;

try {

   ts = [select Total_Hours__c, Hours__c from Timesheet__c ORDER by createddate desc LIMIT 1][0]

 }

catch (exception ex) { //throw no Timesheet}

 

 

i ordered by createddate,you may want to do another date..

Message Edited by tesii on 03-22-2010 01:48 PM

All Answers

TomSnyderTomSnyder

consider :

 

 Timesheet__c ts;

try {

   ts = [select Total_Hours__c, Hours__c from Timesheet__c ORDER by createddate desc LIMIT 1][0]

 }

catch (exception ex) { //throw no Timesheet}

 

 

i ordered by createddate,you may want to do another date..

Message Edited by tesii on 03-22-2010 01:48 PM
This was selected as the best answer
Ckevin1984Ckevin1984
Thanks :)