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
skyfall007skyfall007 

InputText value not fetched in controller

<apex:pageBlockSectionItem >
<apex:outputLabel for="txtAvailableContacts" id="searchContactsLabel" value="Search for Contacts"></apex:outputLabel>
<apex:inputText id="txtSearchContacts" value="{!searchContactsText}"></apex:inputText>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:commandButton id="searchContactsBtn" onclick="searchContacts(); return false;" value="Find Contacts"></apex:commandButton>
</apex:pageBlockSectionItem>

======================================================================================

public string searchContactsText {get; set;}

public pageReference getAvailableSearchedContacts()

{

availableContacts_SearchAll = [Select id, name, email from Contact where name like : '%' + searchContactsText + '%'];
System.Debug('$$$$#### '+searchContactsText);
return null;
}

======================================================================================

VF Page is not passing the input text to controller and hence query returns all the contacts and not according to searchcontact string on VF page.

bob_buzzardbob_buzzard

Can you post the code to the searchContacts (is it an actionfunction?)  - that invokes the action method so is the key.

skyfall007skyfall007

Yes, its an action function

 

<apex:actionFunction name="searchContacts" action="{!getAvailableSearchedContacts}" rerender="availableContacts2" />

skyfall007skyfall007

I still can't figure out the reason why it is not fetching the value on controller side.

bob_buzzardbob_buzzard

I think its the colon in your search query - as you are building the search string by concatenation, you don't need that.  I've also found that it doesn't like the wildcarding in the SOQL - I usually create a wildcarded string and bind that in.   E.g.

 

public pageReference getAvailableSearchedContacts()
{
   String searchStr='%' + searchContactsText + '%';
   availableContacts_SearchAll = [Select id, name, email from Contact where name like :searchStr];
   System.Debug('$$$$#### '+searchContactsText);
   return null;
}

 

skyfall007skyfall007

Thanks for your reply.

 

But I did try what you mentioned earlier in my code. It doesn't do any good. It still displays all the list of contacts.

 

The system.debug statements are empty for searchContactsText (and searchStr in your case will give %%)and hence the value is not getting carried from VF page to controller class.

skyfall007skyfall007

Also, I just tried..

<script type="text/javascript">
function doSave(srch) {
searchContacts(document.getElementById(srch).value);
}
</script>

 

<apex:actionFunction name="searchContacts" action="{!getAvailableSearchedContacts}" rerender="availableContacts2" >
<apex:param id="aname" name="searchContactsText" value="" /></apex:actionFunction>

 

 

<apex:pageBlockSectionItem >
<apex:outputLabel for="txtAvailableContacts" id="searchContactsLabel" value="Search for Contacts"></apex:outputLabel>
<apex:inputText id="txtSearchContacts" value="{!searchContactsText}"></apex:inputText>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:commandButton id="searchContactsBtn" onclick="doSave('{!$Component.searchContactsText}'); return false;" value="Find Contacts"></apex:commandButton>
</apex:pageBlockSectionItem>

 

=========================================================

public pageReference getAvailableSearchedContacts()
{

searchContactsText=Apexpages.currentPage().getParameters().get('searchContactsText');

String searchStr='%' + searchContactsText + '%';
availableContacts_SearchAll = [Select id, name, email from Contact where name like : searchStr Order By name];

return null;
}

 

This time around, doesnt diplay any list of contacts.

 

 

bob_buzzardbob_buzzard

I don't think you need to go the actionfunction route, and I'm wondering if the fact that your action method starts with 'get' is causing an issue somewhere, or there's some race condition I'm not spotting.

 

You should just be able to invoke the action method from the command button:

 

<apex:pageBlockSectionItem >
<apex:outputLabel for="txtAvailableContacts" id="searchContactsLabel" value="Search for Contacts"></apex:outputLabel>
<apex:inputText id="txtSearchContacts" value="{!searchContactsText}"></apex:inputText>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:commandButton id="searchContactsBtn" action="searchContacts" value="Find Contacts"></apex:commandButton>
</apex:pageBlockSectionItem>

 controller:

public pageReference searchContacts()
{
String searchStr='%' + searchContactsText + '%';
availableContacts_SearchAll = [Select id, name, email from Contact where name like : searchStr Order By name];
return null;
}

 

 

 

skyfall007skyfall007

<apex:outputPanel id="availableContacts2">
<apex:pageBlock title="Search All Contacts">
<apex:pageBlockSection Columns="2">
<apex:pageBlockSectionItem >
<apex:outputLabel for="txtAvailableContacts" id="searchContactsLabel" value="Search for Contacts"></apex:outputLabel>
<apex:inputText id="txtSearchContacts" value="{!searchContactsText}"></apex:inputText>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:commandButton id="searchContactsBtn" action="{!searchContacts}" value="Find Contacts"></apex:commandButton>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockTable value="{!availableContacts_SearchAll}" var="item">
<apex:column width="30%">
<apex:facet name="header">Action</apex:facet>
<apex:outputPanel onclick="showLoading(); addSelectedContact('{!item.id}', 'TO'); hidemeandsiblings(this);" styleClass="btn">Add TO</apex:outputPanel>
<apex:outputPanel onclick="showLoading(); addSelectedContact('{!item.id}', 'CC'); hidemeandsiblings(this);" styleClass="btn">Add CC</apex:outputPanel>
<apex:outputPanel onclick="showLoading(); addSelectedContact('{!item.id}', 'BCC'); hidemeandsiblings(this);" styleClass="btn">Add BCC</apex:outputPanel>
</apex:column>
<apex:column value="{!item.name}" width="30%" />
<apex:column value="{!item.email}" width="40%" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:outputPanel>

 

======================================================

public pageReference searchContacts()
{

String searchStr='%' + searchContactsText + '%';
availableContacts_SearchAll = [Select id, name, email from Contact where name like : searchStr Order By name];

return null;
}

 

===========================================================

 

Now, it doesnt return any list of contacts matching the serachText

bob_buzzardbob_buzzard

Does that mean that in this case the search term made it into the action method?

skyfall007skyfall007

I really appreciate your help.

 

But the search term didnot get fetched in my controller class.

 

But I figured out the error but not the solution yet. I created a second clone page with just the search contact section of code which I showed you and it worked absolutely fine by getting only those contacts matching the searchtext. So, I came back to my original page and removed that section out the rerender div and it worked fine. But when I place the section inside that div and it doesnt work.

 

Still not sure what is wrong with that rerender div as it is refreshing the page and search text becomes empty and hence it generates a list of all contacts.

bob_buzzardbob_buzzard

The rerender should work fine.  All that is related to is the output of the changed elements in the page.  The commandbutton will save back the value entered by the user into the property.  I've used this technique in a couple of projects without any problems.   Can you post your full controller and page (using the code icon in the editor toolbar so that the formatting is retained)?

skyfall007skyfall007

Its a huge piece of code and I don't want you to get lost figuring out what is going on. Let me shorten it and give the related piece only so that it will be easier for you.