You need to sign in to do that
Don't have an account?
Derek Davis 7
How do I reference a variable/sting in a Where clause?
Hello,
I have an Apex Controller and a Visualforce page. My controller does several things, amoung them it gets values of a List; and those values are used to populate a Drop Down Field of Departments names on the Visualforce page. The list only shows departments that are related to a facility id. Below is an exerpt from my controller:
This Works:
This Doesn't Work:
The variable myFacilityid is populated in the controller (code to populate the variable not shown here, but I do know it's working because I reference this variable in a different searchquery as \''+myFacilityid+'\').
I also attempted to reference the variable as:
How do I reference the variable in the Where clause shown above?
I have an Apex Controller and a Visualforce page. My controller does several things, amoung them it gets values of a List; and those values are used to populate a Drop Down Field of Departments names on the Visualforce page. The list only shows departments that are related to a facility id. Below is an exerpt from my controller:
This Works:
List<Department__c> Departments = [select id ,name from Department__c where Facility__c = '00160000010NjFa'] ;
This Doesn't Work:
List<Department__c> Departments = [select id ,name from Department__c where Facility__c = {!myFacilityid} ] ;
The variable myFacilityid is populated in the controller (code to populate the variable not shown here, but I do know it's working because I reference this variable in a different searchquery as \''+myFacilityid+'\').
I also attempted to reference the variable as:
- \''+myFacilityid+'\'
- :myFacilityid
- {!myFacilityid}
How do I reference the variable in the Where clause shown above?
varFacilityID
in the controller you can do this, so that the value will be set to myFacilityid.
myFacilityid= ApexPages.currentPage().getParameters().get('varFacilityID');
If required you can call search method also in the constructor for the first time otherwise just get the parameter value and assign it to myFacilityId.
Please do let me know if it helps you.
Regards,
Mahesh
All Answers
List<Department__c> Departments = [select id ,name from Department__c where Facility__c =: myFacilityid] ;
This will work
In the variable is a primitive datat type and you want to compare it with field in a table then you can use below:
String myFacilityId = '00160000010NjFa';
List<Department__c> Departments = [select id ,name from Department__c where Facility__c =: myFacilityId] ;
If the variable is a Collection data type and you want to compare it with the fiend in a table then you can use below
Set<String> myFacilityIdSet = new Set<String>();
.....// Need to add the facility ids into it.
List<Department__c> Departments = [select id ,name from Department__c where Facility__c IN: myFacilityIdSet] ;
Please do let me know if it helps you.
Regards,
Mahesh
String myFacilityid = '00160000010NjFa';
List<Department__c> Departments = [select id ,name from Department__c where Facility__c = :myFacilityid ] ;
NOTE:- There should not be any space between : and myFacilityid
Let us know if this will help you
It doesn't matter whether you have a space or not, I tried both ways and both are working.
Regards,
Mahesh
Below is my full controller code. Line 31 was initially in question. Please let me know if you have any suggestions on how to resolve. This code works fine, and displays the dynamically populated drop down field on the visualforce page when I reference a specific Id, but it is still not working when I reference a the variable. What am I missing?
Thanks in advance for any additional assistance!
Please print the "myFacilityid" and show us the debug logs.
Also make sure that whatever the specific Id you added initially and value from the debug, make sure that both are same.
Regards,
Mahesh
I need to somehow automatically populate the variable's value in the controller when the page is initially loaded, and then perhaps refresh the page immediately so the values with show in the picklist? Any suggestions?
This is the code to display the drop down field on the visualforce page, but it makes since that it would have no value since the Facility ID doesn't get sent to the controller until the page until the "Search records" button is clicked.
Thanks again for any assistance!
varFacilityID
in the controller you can do this, so that the value will be set to myFacilityid.
myFacilityid= ApexPages.currentPage().getParameters().get('varFacilityID');
If required you can call search method also in the constructor for the first time otherwise just get the parameter value and assign it to myFacilityId.
Please do let me know if it helps you.
Regards,
Mahesh