Skip to main content
more examples of relationship queries
Source Link
eyescream
  • 24.2k
  • 5
  • 59
  • 96

: is sufficient indication that you want to bind another variable.

List<OpportunityHistory> oppHistories = [SELECT Id, Probability, OpportunityID 
    From OpportunityHistory 
    where OpportunityID IN :opportunities];

Or even better - with one query:

SELECT Opportunity.OwnerId, Owner.Name,
    (SELECT Id, OldValue, NewValue FROM Histories WHERE Field = 'Probability'),
    (SELECT Id, Probability, StageName FROM OpportunityHistories)
FROM Opportunity
WHERE LastModifiedDate = LAST_N_DAYS:7

This goes to 2 related lists, regular "field history tracking" as well as special thing available only for opportunities that tracks Probability & Stage only. Very useful if you need to have them automapped to the Opportunity.

Yet another format that wastes only 1 query:

SELECT Opportunity.OwnerId, Opportunity.Owner.Name, Probability, StageName
FROM OpportunityHistory 
WHERE Opportunity.LastModifiedDate = LAST_N_DAYS:7
ORDER BY OpportunityId, CreatedDate

: is sufficient indication that you want to bind another variable.

List<OpportunityHistory> oppHistories = [SELECT Id, Probability, OpportunityID 
    From OpportunityHistory 
    where OpportunityID IN :opportunities];

Or even better:

SELECT Opportunity.OwnerId, Owner.Name,
    (SELECT Id, OldValue, NewValue FROM Histories WHERE Field = 'Probability')
FROM Opportunity
WHERE LastModifiedDate = LAST_N_DAYS:7

: is sufficient indication that you want to bind another variable.

List<OpportunityHistory> oppHistories = [SELECT Id, Probability, OpportunityID 
    From OpportunityHistory 
    where OpportunityID IN :opportunities];

Or even better - with one query:

SELECT Opportunity.OwnerId, Owner.Name,
    (SELECT Id, OldValue, NewValue FROM Histories WHERE Field = 'Probability'),
    (SELECT Id, Probability, StageName FROM OpportunityHistories)
FROM Opportunity
WHERE LastModifiedDate = LAST_N_DAYS:7

This goes to 2 related lists, regular "field history tracking" as well as special thing available only for opportunities that tracks Probability & Stage only. Very useful if you need to have them automapped to the Opportunity.

Yet another format that wastes only 1 query:

SELECT Opportunity.OwnerId, Opportunity.Owner.Name, Probability, StageName
FROM OpportunityHistory 
WHERE Opportunity.LastModifiedDate = LAST_N_DAYS:7
ORDER BY OpportunityId, CreatedDate
Source Link
eyescream
  • 24.2k
  • 5
  • 59
  • 96

: is sufficient indication that you want to bind another variable.

List<OpportunityHistory> oppHistories = [SELECT Id, Probability, OpportunityID 
    From OpportunityHistory 
    where OpportunityID IN :opportunities];

Or even better:

SELECT Opportunity.OwnerId, Owner.Name,
    (SELECT Id, OldValue, NewValue FROM Histories WHERE Field = 'Probability')
FROM Opportunity
WHERE LastModifiedDate = LAST_N_DAYS:7