+ Start a Discussion
Derek Davis 7Derek Davis 7 

Pass a Variable from a URL into a Visualforce Page/Controller Extension

Hello,

I am trying to pass a variable from a URL into my Controller Extension, and then use that variable in a SOQL Where Clause within the same extension. I still learning to code and I'm not doing somthing correctly. Any guidance would be much appreciated!

This is what I have:

Passing the variable through the URL:
  • There's a simple varable called "varFacilityID" assigned a value via the URL. For example: /apex/search_service_request?varFacilityID=00160000010NjFa
 
  • I included the following parameter on my Visualforce Page:
    <apex:param name="varFacilityID" value="{!$CurrentPage.parameters.varFacilityID}"/>
    
  • I added the following string to my controller:
    //get Facility ID //     
    public String MyFacilityID = ApexPages.currentPage().getParameters().get('varFacilityID')

Adding the variable into the where clause:
  • I have the following query... (Note: This was working prior to adding the additional "where" condition for MyFacilityID.
    //perform search - return specified fields from the record that meets the where clause // 
       public void search(){  
           string searchquery='select Philips_Site_Number__r.name,Name,SR_Created_Date__c,Status__c,Subject__c,Priority__c,id from service_request__c where Philips_Site_Number__r.Facility__c = MyFacilityID AND Philips_Site_Number__r.name like \'%'+searchstring+'%\' order by createddate DESC Limit 20';
         sr= Database.query(searchquery);  
       }
This is the full code below:

Controller Extension:
public class ServiceRequestSearchController {
public list <Service_Request__c> sr {get;set;}  
 
//get Facility ID //     
public String MyFacilityID = ApexPages.currentPage().getParameters().get('varFacilityID');
    
        
   public string searchstring { get; set;}  
   public ServiceRequestSearchController(ApexPages.StandardController controller) {  
   }     
    
    //perform search - return specified fields from the record that meets the where clause // 
   public void search(){  
       string searchquery='select Philips_Site_Number__r.name,Name,SR_Created_Date__c,Status__c,Subject__c,Priority__c,id from service_request__c where Philips_Site_Number__r.Facility__c = MyFacilityID AND Philips_Site_Number__r.name like \'%'+searchstring+'%\' order by createddate DESC Limit 20';
     sr= Database.query(searchquery);  
   }  
   public void clear(){  
   sr.clear();  
   } 
 }



Visual Force Page:
<apex:page standardController="Service_Request__c" showHeader="false" extensions="ServiceRequestSearchController">  
If your Service Request has been Accepted, then click the "Search for Work Order" link to see the details of your Service Request and related Work Order. <br></br>
  <apex:form >  
 <apex:param name="varFacilityID" value="{!$CurrentPage.parameters.varFacilityID}"/>
 <apex:inputText value="{!searchstring}" label="Input"/>   
  <apex:commandButton value="Search records" action="{!search}"/>  
  <apex:commandButton value="Clear records" action="{!search}"/>  
   <apex:pageBlock title="Search Result">  
    <apex:pageblockTable width="80%" value="{!sr}" var="s">  
     <apex:column headerValue="Asset Name" >
        <apex:outputText >{!s.Philips_Site_Number__r.Name}</apex:outputText>
     </apex:column>        
     <apex:column headerValue="Service Request #" >
        <apex:outputText >{!s.Name}</apex:outputText>
     </apex:column>  
     <apex:column headerValue="SR Status" >  
        <apex:outputText >{!s.Status__c}</apex:outputText>  &nbsp;<apex:outputText rendered="{!s.Status__c='Accepted'}" ><a href="A PRIVATE URL IS LOCATED HERE... THIS HAS BEEN INTENTIONALLY REMOVED PRIOR TO POSTING ON SFDC DEV FORMS AS IT DOESN'T RELATE TO ISSUE" Target="_blank">(Search for Work Order)</a></apex:outputText>
     </apex:column>   
    <apex:column headerValue="Problem Reported" >  
        <apex:outputText >{!s.Subject__c}</apex:outputText>  
     </apex:column>     
    <apex:column headerValue="Priority" >  
        <apex:outputText >{!s.Priority__c}</apex:outputText>  
     </apex:column>         
     <apex:column headerValue="Created Date" >  
        <apex:outputText >{!s.SR_Created_Date__c}</apex:outputText>  
     </apex:column>           
    </apex:pageBlockTable>     
   </apex:pageBlock>  
  </apex:form>  
 </apex:page>



 
Nikhil Sharma 17Nikhil Sharma 17
First of all you need to take the search result in a list and your query be something like this
  public list<contact> contact = new list<contact>();
   
    public list<contact> getcontact(){
    contact = [SELECT Id,Name FROM contact where NAME LIKE :'%'+ search +'%' And id= :MyFacilityID];
    return contact;
   
   
 }

 
Sumit Kumar Singh 9Sumit Kumar Singh 9
Derek Davis 7, 
I think you have missed ':
string searchquery='select Philips_Site_Number__r.name,Name,SR_Created_Date__c,Status__c,Subject__c,Priority__c,id from service_request__c where Philips_Site_Number__r.Facility__c =: MyFacilityID AND Philips_Site_Number__r.name like \'%'+searchstring+'%\' order by createddate DESC Limit 20';

Thanks,
Sumit Kumar Singh.