I have got a lot of research and recherche done as this simple where clause doesn't work because of it self-reference.

I want to display all leads and opportunities who have the same "datefield x" as the actually shown lead in a visual force standard list controller. Has anybody an idea how to solve this?

Kind regards. Constantin Work-around filter input as field reference?


<apex:page controller="c2" extensions="extension_current_lead">
  <apex:pageBlock title="Leads" >
  <apex:PageBlockTable value="{!acts}" var="a">
  <apex:column value="{!a.Name}"/>
  <apex:column value="{!a.Budget__c}"/>
  <apex:column value="{!a.Eventart__c}"/>
  <apex:column value="{!a.Gaestezahl__c}"/>
  <apex:PageBlockTable value="{!acts2}" var="a">
  <apex:column value="{!a.Name}"/>
  <apex:column value="{!a.Amount}"/>
  <apex:column value="{!a.EventArtC__c}"/>
  <apex:column value="{!a.AnzahlDerGaeste_C__c}"/>

apex class

public class c2{

List<Lead> currentLeads = [SELECT DatumEvent__c FROM Lead WHERE Id = :currentId];

if (!currentLeads.isEmpty()) {
Date currentDate = currentLeads[0].DatumEvent__c;

List<Lead> acts = [SELECT Id, Name, Budget__c, Eventart__c, Gaestezahl__c FROM Lead WHERE DatumEvent__c = :currentDate];
List<Opportunity> acts2 = [SELECT Name, Amount, EventArtC__c, AnzahlDerGaeste_C__c FROM Opportunity WHERE DatumEvent__c = :currentDate];

public List<Lead> getacts(){
return acts;
public List<Opportunity> getacts2(){
return acts2;

Error: Kompilierungsfehler: Expecting '}' but was: 'if' in Zeile 5, Spalte 1

Update: I created an extension class as suggested.

public class extension_current_lead

public Lead lead{get;set;}
public extension_current_lead ()
    Id id=apexpages.curentPage().getParameters().get('ID');
    kead=[select Id, Name, Budget__c, Eventart__c, Gaestezahl__c from Lead where ID=:id];

As long as you have the ID for the currently displayed Lead, which you should be able to get from the standard controller passed to an extension controller, you can query its date then using this date query the Leads and Opportunities that have that same date.

List<Lead> currentLeads = [SELECT YourDate__c FROM Lead WHERE Id = :currentId];

if (!currentLeads.isEmpty()) {
    Date currentDate = currentLeads[0].YourDate__c;

    List<Lead> leads = [SELECT Id, ... FROM Lead WHERE YourDate__c = :currentDate];
    List<Opportunity> opps = [SELECT Name, ... FROM Opportunity WHERE YourDate__c = :currentDate];

    // Now process the leads and opportunities that matched in whatever way is required

If you have a page that starts something like:

<apex:page standardController="Lead" extensions="MyLeadExtension">

Then your MyLeadExtension can have a constructor that gets the current Lead ID:

public with sharing class MyLeadExtension {
    private Id currentId = null;

    public MyLeadExtension(ApexPages.StandardController controller) {
        currentId = controller.getRecord().Id;


  • Hello, thank you for your answer and your code. :) That sounds good. Is it also possible to check the Opportunities at the same time? "standard controller passed to an extension controller" how can I do this how is this called so I can make forther research... you know I am a bloody beginner :) – Con Apr 29 '19 at 11:25
  • I updated my pseudo-code to illustrate how you can also query these. If you want the opportunities associated with leads it would be up to you to work out how to query the required relationship information (I am not familiar with this part of the OOTB object model in Salesforce). – Phil W Apr 29 '19 at 15:06
  • Hello Phil, Thank you very much. I will try to do it this way. Just for understanding where to put the code... I already have a VF Page and a Class. What I need to do is .... create is an extension controller and put your code in the class listed above right? – Con Apr 29 '19 at 15:11
  • In terms of the controllers, it depends on how you structure your page. At the top of the page source you declare the custom code for your <apex:page> either with a controller attribute (indicates the class name for your controller) or standardController (indicating the type of object) plus one or more extensions (the class name(s) for your extension controllers). An extension controller receives the standard controller object as a constructor parameter. You call getRecord() to get the SObject shown on the page, and you can get the Id from there. – Phil W Apr 29 '19 at 15:12
  • pretty hard for a beginner -_- but I understand what you mean. I will come back with my codes.. :) – Con Apr 29 '19 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.