-1

I am unable to form a SOQL query to get a record for below scenario. Get a record from obj A which is created after 3 minutes of another record in another object obj B.

And also i want to get first record that is created after 3 minutes, can we get a first record that is created within timeframe of 3 minutes not all of them. Keeping a limit, not giving exact record.

Please help.

0

Use addMinutes either to stored DateTime variable or directly in your SOQL

ObjB__c bRec= [select createdDate from ObjB__c where id = 'your id'];
System.debug([select id from ObjA__c where createdDate > :bRec.createdDate.addMinutes(3)]);
  • thanks, and also if i do this i will get all the records that are created after 3 minutes, can we get a first record that is created within timeframe of 3 minutes – Test Nov 1 at 10:27
  • You could add limit 1 along with order by createdDate desc where desc would be optional – Pranav Parakh Nov 1 at 10:34
0

Pranav Parakh is right. You can get the first record of objA created after 3 minutes of a record of objB created using LIMIT 1 and ORDER BY.

I have here just added List to SOQL query result to avoid null exception. I have also used addSeconds() method instead of addMinutes()

    List<objB__c> objB = [SELECT CreatedDate FROM objB__c WHERE Id='objBRecodId'];

    if(objB.size() > 0){
        List<objA__c> objA = [SELECT Id, Name 
                              FROM objA__c 
                              WHERE CreatedDate > :objB[0].CreatedDate.addSeconds(180) 
                              ORDER BY CreatedDate DESC 
                              LIMIT 1];

        if(objA.size() > 0){
           system.debug(objA[0]); 
        }
     }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.