You need to sign in to do that
Don't have an account?
Stephen 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);
}
}
}
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);
}
}
}
Try the code below:
Regards.
Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
"Invalid field product2 for SObject OpportunityLineItem"
Any ideas to resolve this issue?
My bad! You have to query for Product2 name field as well, i.e:
Regards.
Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
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
Puzzling, I tested the code above and it's working. Can you share your entire code so I can have a look?
Regards.
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;
}
}
}