+ Start a Discussion
Robert Goldberg 9Robert Goldberg 9 

How Can I Pre-Populate Apex InputText field on Visualforce page?

I have created a Visualforce page with a custom controller that is essentially a search page that I've embedded within the Lead page.  It gives my users the ability to quickly search for duplicates without leaving the page.  The issue is, I'm trying to figure out how to prepopulate the search, so they don't have to copy the email address to the search bar.

Here's the Controller:

Public with sharing class LeadOppsearchclas{
 Public List<Lead>leadList{get;set;}
 Public List<Opportunity>optyList{get;set;}
   Public String searchStr{get;set;}
   Public LeadOppsearchclas(){
   }
 
 public LeadOppsearchclas(ApexPages.StandardController stdController){
 }
 
    Public void soslDemo_method(){
       leadList = New List<Lead>();
   optyList = New List<Opportunity>();
   
   if(searchStr.length() > 1){
   String searchStr1 = '*'+searchStr+'*';
   String searchQuery = 'FIND \'' + searchStr1 + '\' IN ALL FIELDS RETURNING  Lead (Id,Name,Email,metro__c,statusname__c,Listing_MLS__c,date_entered_into_Lead_router__c,listing_amount__c,listing_agent__c,referral_fee__c,last_activity_subject__c),Opportunity(Id,Name,Account_Email__c,Opportunity_EMail__c,metro__c,statusname__c,Owner_Name__c,date_entered_into_Lead_router__c,listing_amount__c,listing_agent__c,agent__c,referral_fee__c,last_activity_subject__c)';
   List<List <sObject>> searchList = search.query(searchQuery);
   leadList = ((List<Lead>)searchList[0]);
      optyList = ((List<Opportunity>)searchList[1]);
   if(leadList.size() == 0 && optyList.size() == 0){
       apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Sorry, no results returned with matching string..'));
       return;
   }
   }
   else{
   apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Please enter at least two characters..'));
   return;
   }
  }
}

And the Visualforce Page:

<apex:page standardcontroller="Lead" extensions="LeadOppsearchclas">
  <apex:form >
  <apex:inputText value="{!searchStr}"/>
    <apex:commandButton value="Search in Lead, Opportunity" action="{!soslDemo_method}"                reRender="lead,error,oppt" status="actStatusId"/>
    <apex:actionStatus id="actStatusId">
                <apex:facet name="start" >
                    <img src="/img/loading.gif"/>                    
                </apex:facet>
    </apex:actionStatus>
  </apex:form>
 
    <apex:outputPanel title="" id="error">
     <apex:pageMessages ></apex:pageMessages>
     </apex:outputPanel>
 
 <apex:pageBlock title="Opportunities" id="oppt">
    <apex:pageblockTable value="{!optyList}" var="opty">
     <apex:column headervalue="Name"><apex:outputLink value="/{!opty.ID}" target="_blank">{!opty.name}</apex:outputLink></apex:column>
     <apex:column value="{!opty.Account_Email__c}"/>
     <apex:column value="{!opty.Metro__c}"/>
     <apex:column value="{!opty.Owner_Name__c}"/>
     <apex:column value="{!opty.StatusName__c}"/>
     <apex:column value="{!opty.Date_Entered_Into_Lead_Router__c}"/>
     <apex:column value="{!opty.Listing_Amount__c}"/>
     <apex:column value="{!opty.Listing_Agent__c}"/>
     <apex:column value="{!opty.Agent__c}"/>
     <apex:column value="{!opty.Referral_Fee__c}"/>
     <apex:column value="{!opty.Last_Activity_Subject__c}"/>


       </apex:pageblockTable>
    </apex:pageBlock>
 
    <apex:pageBlock title="Leads" id="lead">
    <apex:pageblockTable value="{!leadList }" var="lead">
     <apex:column headervalue="Name"><apex:outputLink value="/{!lead.ID}" target="_blank">{!lead.name}</apex:outputLink></apex:column>
     <apex:column value="{!lead.email}"/>
     <apex:column value="{!lead.Metro__c}"/>
     <apex:column value="{!lead.StatusName__c}"/>
     <apex:column value="{!lead.Date_Entered_Into_Lead_Router__c}"/>
     <apex:column value="{!lead.Listing_Amount__c}"/>
     <apex:column value="{!lead.Listing_MLS__c}"/>
     <apex:column value="{!lead.Listing_Agent__c}"/>
     <apex:column value="{!lead.Referral_Fee__c}"/>
     <apex:column value="{!lead.Last_Activity_Subject__c}"/>


 </apex:pageblockTable>
    </apex:pageBlock>
</apex:page>
Best Answer chosen by Robert Goldberg 9
Matt SmelserMatt Smelser
Try something like this:
public Lead currentLead;
 
 public LeadOppsearchclas(ApexPages.StandardController stdController){
	this.currentLead = (Lead)stdController.getRecord();
	this.currentLead = [SELECT Id, Email FROM Lead WHERE Id = :this.currentLead.Id];
	searchStr = this.currentLead.Email;
 }

 

All Answers

Matt SmelserMatt Smelser
Robert-
You can use the standard controller to get the information from the current lead and then populate searchStr with the email address from the lead.
Robert Goldberg 9Robert Goldberg 9
Matt,
Thanks for the reply - but where/how would I do that?
Matt SmelserMatt Smelser
Try something like this:
public Lead currentLead;
 
 public LeadOppsearchclas(ApexPages.StandardController stdController){
	this.currentLead = (Lead)stdController.getRecord();
	this.currentLead = [SELECT Id, Email FROM Lead WHERE Id = :this.currentLead.Id];
	searchStr = this.currentLead.Email;
 }

 
This was selected as the best answer
Robert Goldberg 9Robert Goldberg 9
Unfortunately, if I were to do that, I would lose significant pieces of my existing page, as I'd have to competely rewrite my original controller. 
Matt SmelserMatt Smelser
Robert-
I am sorry but I don't see any issue with adding the above snipet of code to your controller. Please explain why you think you would need to rewrite your code.