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
KevSnellKevSnell 

DropDown query list of Users then add to query

Hi all,

 

I'm sure this is possible but I currently can't find the answer so looking for some help or direction.

 

I want to create a drop down in a VF page that is a list of active users with their associated ID so when a user is selected from the dropdown the ID is added to the below query replacing the userinfo.getuserid.  

 

Public static  integer getWeb_Corporate()
    {
    integer Web_Corporate = [SELECT count() FROM Lead where OwnerId =:UserInfo.getUserId() AND LeadSource='Web-Corporate' AND IsConverted = False AND List_Assignment__c != 'Corporate'  LIMIT 1000];
    return Web_Corporate ;
    }    

 


Any help would be appreciated.

Kev 


 

Best Answer chosen by Admin (Salesforce Developers) 
bvramkumarbvramkumar

I see that you are binding <p>{!Web_Corporate}</p> to the page which is supposed to invoke getWeb_Corporate(). But the getter can not be static

 

So your property should be ....

 

Public integer getWeb_Corporate()
{
  integer Web_Corporate = Database.query('SELECT count() FROM Lead where OwnerId =: userId AND LeadSource='Web- Corporate' AND IsConverted = False AND List_Assignment__c != 'Corporate' LIMIT 1000');
return Web_Corporate ;
}

 

All Answers

bvramkumarbvramkumar

Create a property in controller like below

 

public string userId{get;set;} 

 

assign this to the value attribute of your <apex:selectlist value="{!userId}" onchange="<call an action function>"

 

create an actoin function that will do a rerender of the panel that you want to be refreshed. The function that is to be called upon triggering action Function should have no code.

 

And in your getter property.... do the dynamic query instead of direct query by changing the user id parameter. like below:

 

Public static integer getWeb_Corporate()
{
  integer Web_Corporate = Database.query('SELECT count() FROM Lead where OwnerId =: userId AND LeadSource='Web- Corporate' AND IsConverted = False AND List_Assignment__c != 'Corporate' LIMIT 1000');

return Web_Corporate ;
}

KevSnellKevSnell

Thanks, however, I can't seem to get it to work.

 

I have the below as a test:

 

Apex Class - get an error:

Error: Compile Error: expecting a right parentheses, found 'Web' at line 7 column 107

 

This seems to be referring to the query.


public class TESTDROPDOWN {       
   
public string userId{get;set;}    
 
Public static integer getWeb_Corporate()
{
  integer Web_Corporate = Database.query('SELECT count() FROM Lead where OwnerId =: userId AND LeadSource='Web- Corporate' AND IsConverted = False AND List_Assignment__c != 'Corporate' LIMIT 1000');
return Web_Corporate ;
}
 
}

 

And a test VF Page - item valuing being the User ID

<apex:page Controller="TESTDROPDOWN">

    <apex:sectionHeader title="Test" subtitle="Test" />
        
        <apex:form >
        <apex:pageblock >
         <apex:pageBlockSection columns="2" title="List Options" collapsible="false">
        <apex:pageBlockSectionItem >
             <apex:outputLabel value="Show" />
            <apex:selectList id="filter" size="1" value="{!userId}">
                <apex:selectoption itemLabel="-----Select Type-----" itemValue="" /> 
                <apex:selectOption itemValue="12345" itemLabel="KEVIN" /> 
                <apex:selectOption itemValue="12345" itemLabel="DAVE" />             
            </apex:selectList>
        </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        </apex:pageBlock>
        </apex:form>

        <p>{!UnQualified}</p>


</apex:page>

 

bvramkumarbvramkumar

your dynamic query should be like below.... with single quotes escaped...

 

Public static integer getWeb_Corporate()
{
  integer Web_Corporate = Database.query('SELECT count() FROM Lead where OwnerId =: userId AND LeadSource=\'Web- Corporate\' AND IsConverted = False AND List_Assignment__c != \'Corporate\' LIMIT 1000');
return Web_Corporate ;
}

 

 

KevSnellKevSnell

Thanks, however, I now get the error:

 

Error: Compile Error: Illegal assignment from LIST<SObject> to Integer at line 6 column 5

 

bvramkumarbvramkumar

Ok... I anticipated this... try this...

 

Public static integer getWeb_Corporate()
{
integer Web_Corporate = Database.countQuery('SELECT count() FROM Lead where OwnerId =: userId AND LeadSource=\'Web- Corporate\' AND IsConverted = False AND List_Assignment__c != \'Corporate\' LIMIT 1000');

return Web_Corporate ;
}

 

 

 

KevSnellKevSnell

Thanks that fixed the issue, however, now when I run the page I get the below:

 

caused by: System.QueryException: Variable does not exist: userId

 

Class.AAATESTDROPDOWN.getWeb_Corporate: line 8, column 1

 

The page code I'm using is the below:

 

<apex:page Controller="AAATESTDROPDOWN">

    <apex:sectionHeader title="Test" subtitle="Test" />
        
        <apex:form >
        <apex:pageblock >
         <apex:pageBlockSection columns="2" title="List Options" collapsible="false">
        <apex:pageBlockSectionItem >
             <apex:outputLabel value="Show" />
            <apex:selectList id="filter" size="1" value="{!userId}">
                <apex:selectOption itemValue="12345" itemLabel="KEVIN" /> 
                <apex:selectOption itemValue="12345" itemLabel="DAVE" />             
            </apex:selectList>
        </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        </apex:pageBlock>
        </apex:form>

        <p>{!Web_Corporate}</p>

</apex:page>

 

I'm guessing this is because the query isn't get sent a value when the page loads.

bvramkumarbvramkumar

I see that you are binding <p>{!Web_Corporate}</p> to the page which is supposed to invoke getWeb_Corporate(). But the getter can not be static

 

So your property should be ....

 

Public integer getWeb_Corporate()
{
  integer Web_Corporate = Database.query('SELECT count() FROM Lead where OwnerId =: userId AND LeadSource='Web- Corporate' AND IsConverted = False AND List_Assignment__c != 'Corporate' LIMIT 1000');
return Web_Corporate ;
}

 

This was selected as the best answer
KevSnellKevSnell

Thanks that fixes the first part now I just need to work out, how to pass the drop down value to the query.