Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I am trying to get a list of OpportunitiyHistory for selection of Opportunities I do:

   // Get all the opportunities modified in a date range.   
   List<Opportunity> opportunities = [SELECT Opportunity.OwnerId, Owner.Name 
            FROM Opportunity    
            where Opportunity.LastModifiedDate = LAST_N_DAYS:7];

   // Now get the corresponding history for these opportunities 
   List<OpportunityHistory> oppHistories = [SELECT Id, Probability, OpportunityID 
              From OpportunityHistory 
              where OpportunityID in:=opportunities];

I get:

Multiple markers at this line
    - Save error: unexpected 
     token: '='
    - unexpected token: '='

Any ideas what I am doing wrong?

share|improve this question
3  
You should have: WHERE OpportunityID in :opportunities, at the moment you have WHERE OpportunityID in :=opportunities –  Joe Dec 11 '12 at 16:49
    
@Joe Please post as answer .That was correct from you –  Mohith Kumar Dec 11 '12 at 17:01
add comment

3 Answers

up vote 7 down vote accepted

: 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
share|improve this answer
add comment

Remove the "=" from the last line and you're good. You have in:= list, you need in: list

share|improve this answer
add comment

The placeholder syntax doesnt need the = it should just be :opportunities

   // Now get the corresponding history for these opportunities 
   List<OpportunityHistory> oppHistories = [SELECT Id, Probability, OpportunityID 
              From OpportunityHistory 
              where OpportunityID in:opportunities];

Although if you dont need other information you can combine your queries to:

List<OpportunityHistory> oppHistories = [SELECT Id, OpportunityId 
    FROM OpportunityHistory 
    WHERE OpportunityId IN (
        SELECT Id 
            FROM Opportunity    
            WHERE Opportunity.LastModifiedDate = LAST_N_DAYS:7
    )
];
share|improve this answer
1  
you don't even need the semi-join, you can do SELECT Id, OpportunityId FROM OpportunityHistory where opportunity.lastModifiedDate = LAST_N_DAYS:7 –  superfell Dec 11 '12 at 17:07
    
I tried that in the dev console and it kept refusing to recognise the relationship, hence the sub query above :) –  Jon Hazan Dec 11 '12 at 17:42
    
worked fine for me from SoqlX –  superfell Dec 11 '12 at 17:44
    
I guess i must have had some typo as your right its working fine for me as well –  Jon Hazan Dec 11 '12 at 17:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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