[SalesForce] SOQL QUERY WITH CreatedDate

I have two objects SetupAuditTrail(API) and RMSEventLog__c(Custom).I am trying to insert all records from SetupAuditTrail to RMSEventLog__c using a Batch class.

I want to set filter criteria in SOQL statement such that the query should insert those records from SetupAuditTrail to RMSEventLog__c whose CreatedDate is greater than CreatedDate of last record inserted in RMSEventLog__c.

I am able to capture CreatedDate of last record inserted in RMSEventLog__c.
Also I am getting my desired result by putting the value of first query in second query.

select EventOccuranceDate__c from RMSEventLog__c where Datasource__c = 
'Salesforce' AND EventCategory__c = 'SetupAuditTrail' order by CreatedDate 
desc limit 1

[![enter image description here][1]][1]

SELECT id, Action, CreatedById, CreatedDate, CreatedBy.name, Display, 
Section from SetupAuditTrail where CreatedDate > 2015-10-
01T11:51:14.000+0000

[![enter image description here][2]][2]

But my question is how to concatenate both the queries to make it single query.I was trying like this but query is not working.

SELECT id, Action, CreatedById, CreatedDate, CreatedBy.name, Display, 
Section from SetupAuditTrail where CreatedDate > select 
EventOccuranceDate__c from RMSEventLog__c where Datasource__c = 
'Salesforce' AND EventCategory__c = 'SetupAuditTrail' order by CreatedDate 
desc limit 1

Best Answer

The query you are trying will not compile in Salesforce because this type of LEFT INNER JOIN is not Supported.

What you can do is First Store the result of first query in a variable and then perform the second query.

DateTime/Date dt = [select EventOccuranceDate__c from RMSEventLog__c where Datasource__c = 'Salesforce' AND EventCategory__c = 'SetupAuditTrail' order by CreatedDate desc limit 1].EventOccuranceDate__c ;

[SELECT id, Action, CreatedById, CreatedDate, CreatedBy.name, Display, Section from SetupAuditTrail where CreatedDate >: dt]

OR simply like this:

[SELECT id, Action, CreatedById, CreatedDate, CreatedBy.name, Display, Section from SetupAuditTrail where CreatedDate >: [select EventOccuranceDate__c from RMSEventLog__c where Datasource__c = 'Salesforce' AND EventCategory__c = 'SetupAuditTrail' order by CreatedDate desc limit 1].EventOccuranceDate__c]]

Please make sure that EventOccuranceDate__c is of DateTime Type.

Related Topic