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
SFDC ROCKSFDC ROCK 

How to find opportunity id for last 2 or 3 hours in soql ?

Hi All,
How to find opportunity id for last 2 or 3 hours in soql ?
I want to use this in batch apex as query string in where clause.

Currently I am using below code for retriving id for one day with help of custom setting:
=============================
from Opportunity where\t'; query += 'createddate = '+day; //custom setting

Thanks,
Subodh
Khan AnasKhan Anas (Salesforce Developers) 
Hi Subodh,

Greetings to you!

Please try below code:
DateTime dt = System.Now().addHours(-2);
List<Opportunity> oppList = [SELECT Id, Name FROM Opportunity WHERE CreatedDate>=:dt];
System.debug('List Of all Opportunities created within last 2 hours -> ' + oppList);

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Deepali KulshresthaDeepali Kulshrestha
Hi Subodh,


I gone through your problem, refer through these query and find opportunity id for last 2 or 3 hours :

  DateTime dt = System.now().addHours(-3);
  List<Opportunity> lopp = [SELECt Name  FROM Opportunity WHERE CreatedDate >= :dt];


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Ajay K DubediAjay K Dubedi
Hi Subodh,
public class timeTest 
{
    public static void main()
    {
        System.debug(system.now());
        datetime dt1 = system.now().addHours(-3);
        System.debug(dt1);
    }
}

    output- 2019-10-22 14:24:04
            2019-10-22 11:24:04
* If you want to get record using its time then, please use below code
List<Opportunity> oppList = [SELECT Id, Name FROM Opportunity WHERE CreatedDate>=:dt];
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com