You need to sign in to do that
Don't have an account?
Jean Grey 10
Display Accounts with Related Data on Visualforce Page
I'm trying to display Accounts on a Visualforce page with counts of tasks, leads, opps, owners, etc. I believe have all of the data in my controller but I'm not sure how to connect each variable to the appropriate account. Is Wrapper class the correct approach? What about a map? If anyone can share an idea or documentation that I could learn from it would be much appreciated.
public class WrapperDemoController{ //variables to hold all related data public Set<Id> accSet {get;set;} public List<Account> accList {get;set;} public Set<Id> conSet {get;set;} public List<Contact> conList {get;set;} public Set<Id> leadSet {get;set;} public List<Lead> leadList {get;set;} public List<Lead> leadListAll {get;set;} public DateTime nowDate {get;set;} public Date todayDate {get;set;} public Set<Id> whoSet {get;set;} public Set<Id> whatSet {get;set;} public Set<Id> ocrSet {get;set;} public List<OpportunityContactRole> ocrList {get;set;} public List<Opportunity> oppList {get;set;} public Set<Id> oppSet {get;set;} public Set<Id> taskSet {get;set;} public List<Task> taskList {get;set;} public Set<Id> repSet {get;set;} public List<User> repList {get;set;} /*Our Wrapper Class*/ public class TableRow{ public String accName {get;set;} public String domain {get;set;} public Integer countLeads {get;set;} public Integer countOpenTasks {get;set;} public Integer countReps {get;set;} public Integer countOpenOpps {get;set;} public Integer countExistingTasks {get;set;} } /*Public Property that will hold all the TableRows and our PageBlockTable can use this Variable to get the whole List of rows*/ public List<TableRow> rowList {get; set;} /*Constructor that runs a SOQL to get all the Records and build the List. This get executed automatically on the Page Load.*/ public WrapperDemoController(){ nowDate = system.now(); todayDate = system.today(); rowList = new List<TableRow>(); TableRow tr; //Sets whoSet = new Set<Id>(); accSet = new Set<Id>(); whatSet = new Set<Id>(); conSet = new Set<Id>(); leadSet = new Set<Id>(); ocrSet = new Set<Id>(); oppSet = new Set<Id>(); repSet = new Set<Id>(); //leads that have reporting matched account populated. This will be the list of Accounts we look at leadList = new List<Lead<([SELECT Id,OwnerId,Owner.Name,Matched_Account__c, Matched_Account__r.Name FROM Lead WHERE Matched_Account__c!=NULL AND IsConverted=FALSE]); system.debug('leadList '+leadList); //first fill the account set. This is the Set to find all related data for(Lead l : leadList){ accSet.add(l.Matched_Account__r.Id); whatSet.add(l.Matched_Account__r.Id); repSet.add(l.OwnerId); } //now get all the leads leadListAll = new List<Lead>([SELECT Id,OwnerId,Owner.Name,Matched_Account__c, Matched_Account__r.Name FROM Lead WHERE Matched_Account__r.Id IN :accSet]); //now fill the contact list conList = new List<Contact>([SELECT Id,OwnerId,Owner.Name,AccountId,Account.Name FROM Contact WHERE AccountId IN :accSet]); //now use the lead and contact lists to fill the whoSet for(Lead l :leadListAll){ whoSet.add(l.Id); repSet.add(l.OwnerId); } for(Contact c :conList){ whoSet.add(c.Id); conSet.add(c.Id); repSet.add(c.OwnerId); } //find all ocrs related to these contacts or accounts //might need to split into searching by accounts and contacts separately, to avoid SOQL limits ocrList = new List<OpportunityContactRole> ([SELECT Id,OpportunityId,ContactId,Contact.AccountId,Opportunity.StageName,Opportunity.Amount FROM OpportunityContactRole WHERE Opportunity.StageName !='Duplicate Quote' AND IsClosed=FALSE AND (ContactId IN :conSet OR Contact.AccountId IN :accSet)]); for(OpportunityContactRole ocr :ocrList){ oppSet.add(ocr.OpportunityId); conSet.add(ocr.ContactId); whoSet.add(ocr.ContactId); whatSet.add(ocr.OpportunityId); whatSet.add(ocr.Contact.AccountId); accSet.add(ocr.Contact.AccountId); repSet.add(ocr.Contact.OwnerId); repSet.add(ocr.Contact.Account.OwnerId); repSet.add(ocr.Opportunity.OwnerId); } //find all opps by account oppList = new List<Opportunity>([SELECT Id,StageName,Amount,OwnerId,Owner.Name FROM Opportunity WHERE IsClosed=FALSE AND CloseDate>:todayDate AND AccountId IN :accSet]); for(Opportunity o :oppList){ oppSet.add(o.Id); whatSet.add(o.Id); repSet.add(o.OwnerId); } //now find all open tasks related to any above data taskList = [SELECT Id,OwnerId,WhoId,WhatId FROM Tasks WHERE Status!='Completed' AND ActivityDate>:todayDate AND (Type = 'Call/Email' OR Type = 'Call_Email' OR Type = 'Call:TT' OR Type = 'Call:LM' OR Type = 'Call:Demo' OR Type = 'Call:Other' OR Type = 'Follow_up' OR Type = 'Follow up') AND (WhoId IN :whoSet OR WhatId IN :whatSet)]; /*Building the List of TableRows*/ /*Fetch all the Contacts and then build the List*/ for(Account a : accSet){ tr = new TableRow(); tr.accName = acc.Name; tr.domain = acc.Domain; tr.countLeads = ; tr.countOpenOpps = ; tr.countOpenTasks = ; tr.countReps = ; /*Add the TableRow to the List then and there*/ rowList.add(tr); } } }
All Answers
The wrapper is the best approach for achieving your requirement,
This is an example which helps you --
In this way, you can merge all accounts related tasks, leads, opps.
Thanks
Akshay
I was able to format the integer like so: But I still get the incorrect signature. Do I need to do some operation to change the matched account object to an ID for the map?
For the rep map (owners), I need to include owners of all records. I have owners for the leads, accounts, contacts and opps, but how do I get the task owners for the tasks that match the contact or lead? Because of the polymorphic restrictions I cannot query on the account field for those two objects. Do I need to create a new map of Contact/Lead to Account (from the main map), and compare in the query? Would it be easier/better design to simply populate the account Id field on all tasks with the account Id (if contact) or matched account id (if lead)?