1

I have a set of Ids for Opportunities which have a specific record type and a new owner. They're in a set so that I can check that the Ids are not included in a list later in the code, using

reassignedOpps.contains(o2.Id) == false

I now need to fetch the complete Opportunity records so that I can populate a string of Opportunity field values.

But I get the following error for line

for(Opportunity oNewOwner : reassignedOpps){

Loop variable must be of type Id

How can I fetch the complete Opportunity records from the reassignedOpps set so that I can use their field values?

trigger OpportunityTargetUpdate on Opportunity (after update) {

        Set<Id> reassignedOpps = new Set<Id>();
        for(Opportunity o1: oppIds){

            Opportunity oldOpp = Trigger.OldMap.get(o1.Id);
            if(oldOpp != null
              &&
              oldOpp.OwnerId != o1.OwnerId)
            {
                reassignedOpps.add(o1.Id);
            }
        }

        if(reassignedOpps.isEmpty() == true){
            System.debug('no Opportunities had new owners');
        }

            Set<String> identifiers = new Set<String>();
            // ERROR HERE
            for(Opportunity oNewOwner : reassignedOpps){
                identifiers.add(oNewOwner.Identifier__c);  
            }


        List<Opportunity> updatedOpps = new List<Opportunity>();
        for(Opportunity o2 : oppIds){

            //check whether Close Date month or year has changed but not the owner
            Opportunity oldOpp1 = Trigger.OldMap.get(o2.Id);
            if(oldOpp1 != null 
               &&
               // ! cannot use contains to check List only Set
               reassignedOpps.contains(o2.Id) == false
               &&
               (oldOpp1.CloseDate.month() != o2.CloseDate.month()
                || // OR
                oldOpp1.CloseDate.year() != o2.CloseDate.year()
               )){
                   updatedOpps.add(o2);
               }
        }
        if(updatedOpps.isEmpty() == true){
            System.debug('no Opportunities had new close dates');
        }
1
  • 1
    You really don't need any of your == true expressions above, if (bool == true) is always the same is if (bool).
    – Adrian Larson
    Commented Jun 16, 2015 at 16:26

2 Answers 2

1

Instead of looping through the set of IDs you would want to use your Ids to limit the Query . You can put that right into your for loop, something like this:

for (Opportunity oNewOwner: [SELECT Id, OwnerId FROM Opportunity
                            WHERE Id IN :reassignedOpps]) {
    // Your code here
}

The error is stating the variable you are looping through reassignedOpps are Ids and not Opportunities.

7
  • Thanks, is there any way to work with the list of reassignedOpps without an SOQL query?
    – Alex S
    Commented Jun 16, 2015 at 15:24
  • @AlexS You should be able to create a list of opportunities and just add the opportunity from the Trigger to the list instead of adding it's id to the set.
    – Jenny B
    Commented Jun 16, 2015 at 15:26
  • That would be ideal but I need to be able to check that the Opportunities in the reassignedOpps list aren't included in another list of Opportunities later in my code (see above).
    – Alex S
    Commented Jun 16, 2015 at 15:28
  • @AlexS what about 2 lists, reassigned list and updated list, so if it doesn't meet the reassigned list criteria then in your else statement check if it meets your other criteria and add it to the update list?
    – Jenny B
    Commented Jun 16, 2015 at 15:30
  • That would be perfect : )
    – Alex S
    Commented Jun 16, 2015 at 15:31
0
trigger OpportunityTargetUpdate on Opportunity (after update) {
    Set<String> identifiers = new Set<String>();
    List<Opportunity> updatedOpps = new List<Opportunity>();
    for(Opportunity newOpp: Trigger.new){
        Opportunity oldOpp = Trigger.OldMap.get(newOpp.Id);

        if(oldOpp.OwnerId != newOpp.OwnerId){
            identifiers.add(newOpp.Identifier__c);
        }else if( oldOpp.CloseDate.month() != newOpp.CloseDate.month() || oldOpp.CloseDate.year() != newOpp.CloseDate.year() ){
            updatedOpps.add(newOpp);
        }
    }
    if(identifiers.isEmpty()){
        System.debug('no Opportunities had new owners');
    }
    if(updatedOpps.isEmpty()){
        System.debug('no Opportunities had new close dates');
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .