Not sure if this is entirely possible. I have been asked to create a checkbox field on the Account object. Below is the SOQL query that would list all accounts that have Opportunities that are 'Closed Won' with the Product Family 'VMS'. Basically, the Field Formula would do a lookup similar to below query and be marked true if the Account has any Opportunities that 'Closed Won' with the Product Family, 'VMS'.
My second option would be to create a custom apex page maybe have a dropdown to select Product Family to filter the query and then return a list.
Select AccountID, Account.Name
From Opportunity
WHERE Id IN
(Select OpportunityId From OpportunityLineItem WHERE Product_Family__c='VMS')
AND StageName='Closed Won'
GROUP BY AccountID, Account.Name
ORDER BY Account.Name
Not finished and can use some cleanup of the code, but using the wrapper class controller, I was able to accomplish what I needed in the short term. I need to add features later.
public class wrapperClassController {
public List<aAccount> accountList {get; set;}
public List<Opportunity> oppt {get; set;}
public String selected {get; set;}
public List<aAccount> getFilterAccounts(String option) {
if(selected == 'VEMS') {
Map<Id, Opportunity> opptMap = new Map<Id, Opportunity>([Select ID, AccountID From Opportunity o WHERE Id IN (Select OpportunityId From OpportunityLineItem WHERE Product_Family__c LIKE 'VEMS%') AND StageName='Closed Won']);
oppt = opptMap.values();
}
else if(selected == 'Renewal') {
Map<Id, Opportunity> opptMap = new Map<Id, Opportunity>([Select ID, AccountID From Opportunity o WHERE Id IN (Select OpportunityId From OpportunityLineItem WHERE Product_Family__C='Renewal') AND StageName='Closed Won']);
oppt = opptMap.values();
}
option = selected;
if (option == 'None' || option == null) {
return null;
}
else {
Set<Id> accountSet = new Set<Id>();
for(Opportunity o: oppt) {
accountSet.Add(o.AccountID);
}
if(accountList == null) {
accountList = new List<aAccount>();
for(Account a: [SELECT Id, Name FROM Account WHERE ID in:accountSet LIMIT 10]) {
accountList.add(new aAccount(a));
}
}
return accountList;
}
}
public List<aAccount> getAccounts() {
return getFilterAccounts(selected);
}
public PageReference refreshSelected() {
accountList = null;
getAccounts();
return null;
}
public PageReference processSelected() {
List<Account> selectedAccounts = new List<Account>();
for(aAccount aAcct: getAccounts()) {
if(aAcct.selected == true) {
selectedAccounts.add(aAcct.acct);
}
}
System.debug('These are the selected Accounts...');
for(Account acct: selectedAccounts) {
system.debug(acct);
}
accountList=null;
return null;
}
public class aAccount {
public Account acct {get; set;}
public Boolean selected {get; set;}
public aAccount(Account a) {
acct = a;
selected = false;
}
}
}
Then for the very basic apex page.
<apex:page controller="wrapperClassController">
<apex:form >
<apex:selectList id="selected_list" value="{!selected}" required="false" size="1">
<apex:selectOption itemvalue="None" itemLabel="--None--"/>
<apex:selectOption itemvalue="VEMS" itemLabel="VEMS"/>
<apex:selectOption itemvalue="Renewal" itemLabel="Renewal"/>
<apex:actionSupport event="onchange" action="{!refreshSelected}"/>
</apex:selectList>
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="Process Selected" action="{!processSelected}" rerender="table"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!accounts}" var="a" id="table">
<apex:column >
<apex:inputCheckbox value="{!a.selected}"/>
</apex:column>
<apex:column value="{!a.acct.Name}" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>