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
Janno RipJanno Rip 

Display Leads and Accounts in same <apex:panelGrid>

Hello everyone,

In my controller I query for leads and accounts to display them in a googlemap and also in a panelGrid:

User-added image
However what I fail to achieve is to display the leads and accounts in the same panelGroup and not 2 single ones
public List<Lead> warehouses  {get; private set;} 
public List<Account> Pot {get; private set;}
So far I have 2 seperate queries. One for 'warehouse' (leads) and one for 'Pot' (accounts) - (here a snippet)
 
warehouses =  [ 
                               SELECT Id,Street,company, Name,
                               FROM Lead
                               WHERE DISTANCE(LocateCity__c, GEOLOCATION(:dlat,:dlon), 'km') < :decimal.valueof(myInput)
                              ORDER BY DISTANCE(LocateCity__c, GEOLOCATION(:dlat,:dlon), 'km')  
                               LIMIT :recordLimit
                      ];
 
pot = [ 
                               SELECT Name,ShippingCity,ShippingPostalCode
                               FROM Account
                               WHERE  DISTANCE(LocateCity__c, GEOLOCATION(:dlat,:dlon), 'km') < :decimal.valueof(myInput) 
                              ORDER BY DISTANCE(LocateCity__c, GEOLOCATION(:dlat,:dlon), 'km')  
                               LIMIT :recordLimit
               ];
Since I do not know any better I created for each an apex:PanelGrid in my visualforce page:

To Display Leads:
<apex:pageBlockSectionItem >
<apex:outputPanel styleClass="container-fluid">
<div style="height:300px; overflow:auto !important;"> 
    
<apex:repeat value="{!warehouses}" var="war">

<apex:panelGrid columns="2" columnClasses="col1, col2"> 
<apex:panelGroup >
                                  
<apex:variable var="url" value="GoogleDistance | https://www.google.com/maps?saddr=+{!currentAccount.Geolocation__c}&daddr=+{!war.Geolocation__c}" />

<apex:outputLink rendered="{!IF(NOT(CONTAINS(selectedField,'Jobads')), true, false)}" value="{!MID(url, FIND('|', url) + 1, LEN(url))}" target="_blank">{!distances[war.id]}</apex:outputLink>  
                   
</apex:panelGroup>                        
</apex:panelGrid>
</apex:repeat> 
</div> 
</apex:outputpanel>    
</apex:pageBlockSectionItem> 
To Display Accounts:
 
<apex:pageBlockSectionItem >
<apex:outputPanel styleClass="container-fluid">
<div style="height:300px; overflow:auto !important;">  
   
<apex:repeat value="{!pot}" var="pote">
                            
<apex:panelGrid columns="2" columnClasses="col1, col2"> 
<apex:panelGroup >
<apex:variable var="url" value="GoogleDistance | https://www.google.com/maps?saddr=+{!currentAccount.Geolocation__c}&daddr=+{!pote.Geolocation__c}" />
<apex:outputLink rendered="{!IF(NOT(CONTAINS(selectedField,'Jobads')), true, false)}" value="{!MID(url, FIND('|', url) + 1, LEN(url))}" target="_blank">{!distances[pote.id]}</apex:outputLink>  
</apex:panelGroup>                              
</apex:panelGrid>                 
</apex:repeat> 
</div> 
</apex:outputpanel>            
</apex:pageBlockSectionItem>

I have read some articles about wrapper classes but struggle to adapt this to my problem.

Can anyone point me in the right direction? To put leads and accounts in the same panelgrid instead of 2 seperate one for each objects?

Thanks!


 

AnudeepAnudeep (Salesforce Developers) 
Hi Janno, 

You can use a Wrapper class to hold both leads and accounts. To get started, you can look at the following example that will hold both contacts and leads
public class PersonService {

    public class Person {

        String id;
        String firstName;
        String lastName;
        String company;
        String email;
        String phone;
        String sObjectType;

    }

    public static List searchByEmail(String email) {

        // list of Person objects to return
        List people = new List();

        // issue the sosl search
        List<list> searchResults = [FIND :email IN EMAIL FIELDS RETURNING
            Contact (Id, Account.Name, Email, Phone, FirstName, LastName),
            Lead (Id, Company, FirstName, LastName, Email, Phone)];

        // cast the results by sObjec type
        List contacts = ((List)searchResults[0]);
        List leads = ((List)searchResults[1]);

        // a each contact found as a Person
        for (Integer i=0;i<contacts.size();i++) {
            Person p = new Person();
            p.id = contacts[i].Id;
            p.firstName = contacts[i].FirstName;
            p.lastName = contacts[i].LastName;
            p.company = contacts[i].Account.Name;
            p.email = contacts[i].Email;
            p.phone = contacts[i].Phone;
            p.sObjectType = 'Contact';
            people.add(p);
        }

        // a each lead found as a Person
        for (Integer i=0;i<leads.size();i++) {
            Person p = new Person();
            p.id = leads[i].Id;
            p.firstName = leads[i].FirstName;
            p.lastName = leads[i].LastName;
            p.company = leads[i].Company;
            p.email = leads[i].Email;
            p.phone = leads[i].Phone;
            p.sObjectType = 'Lead';
            people.add(p);
        }

        System.debug('Returning people: '+people);

        return people;

    }
}
This example is take from https://blog.jeffdouglas.com/2009/02/24/returning-contacts-and-leads-with-custom-wrapper-class/

Anudeep