I need to fire a Trigger when an Opportunity record is updated and Stage = Closed Won and there is a specific value in the name of the related Opportunity Product.
What's the syntax for this IF statement need to be?

Here's my class thus far:

public static void ConnectCustomerWin(list<Opportunity> oppList, map<id,Opportunity> oldProjectMap) {
        Set<Id> ConnectOppAccounts = new Set<Id>();
        if (oldProjectMap != null){
            for (Opportunity o : oppList) {
                //Find Oppty records where Stage = Closed won and have a Opportunity Product that starts with Connect
                if (o.StageName == 'Closed Won' && o.OpportunityLineItems__r.Product2 == 'Connect'){ //line in question
  • Product2 will return an ID, you need to traverse the relationship to get at the field you are trying to evaluate – Eric Feb 6 '15 at 21:04
  • I'm having troubles with the syntax to traverse the relationship to at the "Name". Do you know what that would be? – John_Henry Feb 10 '15 at 19:29

Short answer, a single SOQL query will not do this since the OLI (Opportunity Product) can be many records so which one would it be.

What you can do is add a roll up summary on the Opportunity for a count of OLI's with that value, then use that field in your query as a criteria.

Or you could use

[Select ID From Opportunity, (Select ID From OpportunityLineItems__r Where FIELD = VALUE] From Opportunity]

Then you could iterate over the opportunity records and check the size of the OpportunityLineItems__r. If the size of the records matches your criteria, add the ID of the opportunity to a Set<ID> then process the Opportunities in the Set<ID>

  • I wasn't able to resolve my issue with this. Perhaps I should clarify what I'm looking for - I need to find all Opportunities that have Stage = Closed Won and have an Opportunity Product that begins with "Connect". I'm looking for the syntax that would need to go into the If statement. I updated the code in my original post to clarify this as well. – John_Henry Feb 6 '15 at 20:26

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.