+ Start a Discussion
Carter85Carter85 

Need help with a lookup field search.

I've got a custom VF page in which I'm trying to allow users to run a search from to find specific form numbers when ordering supplies.  However, I don't think I've set it up properly and could use some help trying to figure out where I've gone wrong because the search seems to at least execute, but not return any values to the intended table.

The lines from my controller governing the search are here:

public String query {get; set;}
public List<MG_Forms__c> inventory {get; set;}

public PageReference runQuery()
{
if(query.length() < 2){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Search string must be at least two characters.' );
ApexPages.addMessage(myMsg);
return null;
}
List<List<MG_Forms__c>> searchResults=[FIND :query IN ALL FIELDS RETURNING MG_Forms__c (Form_Number__c, Name, Form_Title__c, State_Specific__c, Cancellable__c, In_Use__c)];
inventory=searchResults[0];
return null;
}

 

 

The VF snippet for the search in the main page is here:

<script>
 var newWin=null;
 function openLookupPopup(name, id)
 {
  var url="/apex/Popup?namefield=" + name + "&idfield=" + id;
  newWin=window.open(url, 'Popup','height=500,width=600,left=100,top=100,resizable=no,scrollbars=yes,toolbar=no,status=no');
  if (window.focus)
  {
   newWin.focus();
  }
   return false;
    }
 function closeLookupPopup()
 {
    if (null!=newWin)
    {
       newWin.close();
    } 
 }
    </script>
      <apex:pageBlockSection >
          <apex:panelGroup >
          <apex:outputLabel for="item1">Select form number for Item 1:</apex:outputLabel>
         <apex:panelGroup >
<apex:outputLabel for="item1">Select form number for Item 1:</apex:outputLabel>
<apex:inputHidden value="{!MG_Forms__c.Name}" id="targetId" />
<apex:inputText size="70" value="{!MG_Forms__c.Form_Number__c}" id="targetName" onFocus="this.blur()" disabled="false"/> <a href="#" onclick="openLookupPopup('{!$Component.targetName}', '{!$Component.targetId}'); return false">Lookup</a>

 And the VF for the Popup to conduct the search here:

<apex:page StandardController="MG_Forms__c" extensions="SupplyOrderController" sidebar="false" showheader="false">
<script language="javascript">
window.onload = new function()
{
// bring popup window to front
window.focus();
var ele=document.getElementById('{!$Component.form.block.section.query}');
if (ele)
{
ele.focus();
}
}

function fillIn(name, id)
{
var winMain=window.opener;
if (null==winMain)
{
winMain=window.parent.opener;
}
var ele=winMain.document.getElementById('{!$CurrentPage.parameters.namefield}');
ele.value=name;
ele=winMain.document.getElementById('{!$CurrentPage.parameters.idfield}');
ele.value=id;
CloseWindow();
}

function CloseWindow()
{
var winMain=window.opener;
if (null==winMain)
{
winMain=window.parent.opener;
}
winMain.closeLookupPopup();
}
</script>

<apex:messages />
<apex:form id="form" >

<div style="width 100%">
<apex:pageBlock title="Lookup" id="block">

<apex:pageBlockSection id="section">
Enter search text and click Go<br/>
<apex:inputText value="{!query}" id="query"/>
<apex:commandButton value="Go" action="{!runQuery}"/>
</apex:pageBlockSection>
</apex:pageBlock>

<apex:pageBlock >
<apex:pageBlockSection columns="1">
<apex:pageBlockTable value="{!inventory}" var="MG_Forms__c">
<apex:column headerValue="Form Number">
<apex:outputLink value="#" onclick="fillIn('{!MG_Forms__c.Form_Number__c}', '{!MG_Forms__c.Name}')">{!MG_Forms__c.Form_Title__c}</apex:outputLink>
</apex:column>
<apex:column headerValue="State Specific" value="{!MG_Forms__c.State_Specific__c}"/>
<apex:column headerValue="Cancellable" value="{!MG_Forms__c.Cancellable__c}"/>
<apex:column headerValue="In Use" value="{!MG_Forms__c.In_Use__c}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
<button type="button" onclick="CloseWindow();">Close Window</button>
</div>
</apex:form>
</apex:page>
Best Answer chosen by Admin (Salesforce Developers) 
sushant sussushant sus
try this in controller

String searchquery='FIND \''%'+ searchText + '% '\' IN ALL FIELDS RETURNING Content_Item__c (id,name), Content__c';

list<list<Content_Item__c>> searchList = search.query(searchquery);
searchResults = ((List<Content_Item__c>)searchList[0]);

All Answers

sushant sussushant sus
try this in controller

String searchquery='FIND \''%'+ searchText + '% '\' IN ALL FIELDS RETURNING Content_Item__c (id,name), Content__c';

list<list<Content_Item__c>> searchList = search.query(searchquery);
searchResults = ((List<Content_Item__c>)searchList[0]);
This was selected as the best answer
Carter85Carter85

Thank you for the suggestion, though unfortunately it does not seem to work either unless I made a mistake in adapting it to what I have currently.  I tried:

String searchResults='FIND \''%'+ query + '% '\' IN ALL FIELDS RETURNING Form_Number__c (id,name), MG_Forms__c';

list<list<MG_Forms.Form_Number__c>> inventory = search.query(searchResults);
searchResults = ((List<MG_Forms.Form_Number__c>)inventory[0]);

 And replaced 'searchText' with 'query' in accordance with some other edits I've made, which should be reflected above.  This did not return a result either.  However, the line:

searchResults = ((List<MG_Forms.Form_Number__c>)inventory[0]);

 was returning a cannot assign SOObject to string error, so it may still be my fault for not adapting your suggestion properly, but even when I commented it out try it without the error the list returned was still blank for whatever reason, so any additional suggestions you may have would definitely be helpful.

sushant sussushant sus
searchresult is a string and it is returining sobject .please refer this

http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL.htm

sushant
Carter85Carter85

Thanks, you were right, I was just mis-reading the previous suggestion and had a logic error elsewhere.