function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Stephen NStephen N 

Find Opportunities with Products matching a criteria

 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 for this.  Below is the class I currently have with the line in question notated: 

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
                ConnectOppAccounts.add(o.accountid);
                }
            }
        }
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Try the code below:
 
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') {
					for(OpportunityLineItem oppItem : o.OpportunityLineItems){ 
						if(oppItem.Product2.startsWith('Connect')) {
							ConnectOppAccounts.add(o.accountid);
							break;
						}
				
					}
				}
			}
		}

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Stephen NStephen N
Thanks for the code.  I tried this out but received the following error:

"Invalid field product2 for SObject OpportunityLineItem"

Any ideas to resolve this issue?
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello, 

My bad! You have to query for Product2 name field as well, i.e:
 
List<Opportunity> oppList = [SELECT Id, AccountId, StageName, (SELECT Product2.Name FROM OpportunityLineItems) FROM Opportunity];
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') {
            for(OpportunityLineItem oppItem : o.OpportunityLineItems){ 
                if(oppItem.Product2.Name.startsWith('Connect')) {
                    ConnectOppAccounts.add(o.accountid);
                    break;
                }
                
            }
        }
    }
    
}

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Stephen NStephen N
Now I receive this error:

Invalid foreign key relationship: OpportunityLineItem.Product2

Since the Product is related to the Opportunity line Item, I would have expected this to work...perhaps this syntax isn't traversing the relationship as expected 
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Puzzling, I tested the code above and it's working. Can you share your entire code so I can have a look?

Regards.
Stephen NStephen N
Here is the entire class, I've commeneted out the first method that is not applicable.  Appreciate your assitance!


public with sharing class OpportunityUtil {

    /* public static void ChatterPostForNewCustomerWin(list<Opportunity> oppList) {
        
        set<ID> accIDs = new set<ID>();
        set<ID> oppIDsToProcess = new set<ID>();
        list<FeedItem> chatterPosts = new list<FeedItem>();
        
        string ChatterGroupID = [select id from CollaborationGroup where name = 'New Customer Wins' limit 1].id;
        
        for (Opportunity o : oppList) {
            accIDs.add(o.accountid);
        }
        
        for (Account a : [select id, Count_of_Closed_Won_Opportunities__c from Account where id in : accIDs and Count_of_Closed_Won_Opportunities__c = 0]) {
            oppIDsToProcess.add(a.id);
        }
        
        for (Opportunity o : [select id, accountid, closedate, ownerid, name, account.name, account.NumberOfEmployees, account.Industry, Consulting_Type__c, account.BillingCity, account.BillingState, account.BillingCountry from Opportunity where accountid in : oppIDsToProcess and stagename = 'Closed Won']) {
            
            String oppURL = URL.getSalesforceBaseUrl().toExternalForm() + '/' + o.id;
            String Location = o.account.BillingCity + ', ' + o.account.BillingState + ', ' + o.account.BillingCountry;
             FeedItem post = new FeedItem();
             //post.ParentId = UserInfo.getUserId();
             post.ParentId = ChatterGroupID;
             post.Title = o.Name;
             post.Body = 'Client: ' + o.account.name  + '\n' + 'Industry: ' + o.account.Industry + '\n' + 'Number of Employees: ' + o.account.NumberOfEmployees + '\n' + 'Location: ' + Location + '\n' + 'Consulting Type: ' + o.Consulting_Type__c;
             post.LinkUrl = oppURL;
             post.Type = 'LinkPost';
            insert post;
        }

    } */

    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') {
                    for(OpportunityLineItem oppItem : o.OpportunityLineItems){
                        if(oppItem.Product2.Name.startsWith('Connect')) {  //error is occuring at this time
                            ConnectOppAccounts.add(o.accountid);
                            break;
                        }
                    }
                }    
            }
        }
        // insert 
        else 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') {
                    for(OpportunityLineItem oppItem : o.OpportunityLineItems){
                        if(oppItem.Product2.Name.startsWith('Connect')) {
                            ConnectOppAccounts.add(o.accountid);
                            break;
                        }
                    }
                }  
            }
        }

        List <Account> AccountsToUpdate = [SELECT id, Magento_Connect_Customer__c, SF_Connect_Customer__c FROM Account WHERE id in:ConnectOppAccounts];
        
        for (Account item: AccountsToUpdate){
            item.Magento_Connect_Customer__c = true;
            item.SF_Connect_Customer__c = true;
        }
    }
}