You need to sign in to do that
Don't have an account?
I am getting input values as null. Please help and expalin the reason?
<apex:page controller="ContactSearchController" >
<apex:form >
<apex:pageMessages />
<apex:pageBlock title="Enter Inputs">
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!Searchresult}" value="Search"/>
</apex:pageBlockButtons>
<apex:pageblockSection >
<apex:inputField value="{!c.Lastname}"/>
<apex:inputField value="{!c.Phone}"/>
<apex:inputField value="{!c.Email}"/>
<apex:inputField value="{!c.AccountId}"/>
</apex:pageblockSection>
</apex:pageBlock>
<apex:pageblock title="Search Results" rendered="{!showsearchresults}">
<apex:pageblockTable value="{!conList}" var="c">
<apex:column value="{!c.lastname}"/>
<apex:column value="{!c.phone}"/>
<apex:column value="{!c.email}"/>
<apex:column value="{!c.accountId}"/>
</apex:pageblockTable>
</apex:pageblock>
</apex:form>
</apex:page>
public class ContactSearchController{
public Contact C {get;set;}
public list<contact> conList{get;set;}
public boolean showsearchresults{get;set;}
public ContactSearchController(){
//C = new Contact();
showsearchresults=false;
}
public pagereference Searchresult(){
C = new Contact();
conList=new list<contact>();
if(c.Lastname== null || c.Lastname=='' && c.Email==null || c.Email=='' && c.Phone==null || c.Phone =='' && c.AccountId==null || c.AccountId ==''){
System.debug('Value of lastname'+ c.Lastname);
System.debug('Value of Email'+ c.Email);
System.debug('Value of Phone'+ c.Phone);
System.debug('Value of AccountId'+ c.AccountId);
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please enter values for all input fields'));
showSearchResults=false;
return null;
}
System.debug('Size of list of contacts before SOQL'+conList.size());
conList = [Select lastname,email,phone,accountId from Contact where (lastname=:c.Lastname and email=:c.Email and phone=:c.Phone and accountId=:c.AccountId)and (lastname !=null and email!=null and phone!=null and accountId!=null)];
System.debug('Size of list of contacts after SOQL'+conList.size());
System.debug('values of list of contacts after SOQL'+conList);
if(conList.size()==0){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'No matching Contact record found');
ApexPages.addMessage(myMsg);
showSearchResults=false;
return null;
}
else if(conList.size()>0){
showSearchResults=true;
return null;
}
return null;
}
}
>> As all conditions are in AND operator, it should be changed to OR
Line 12 of controller should be changes as follows:-
#2 Why we have used private set in while declaration of conlist and searchresults.
>> As these variables are read only and we never need values of these to be passed back to controller (in your context it doesn't matter)
#3 Why we have initialized conlist again in search button even If we had already initialized in controller.
>> because we don't want to display existing records if new search was done with empty fields.
All Answers
removing C = new Contact(); should return you result.
Public string lastName{get; set;}
Public string email{get; set;}
Public string phone{get; set;}
Public string accountId{get;set;}
and, in your VF page, change <apex:inputField value="{!c.Lastname}"/> .. to .. <apex:inputText value="{!lastName}" ... etc.
in your controller query string, change the where clause lastname=:c.Lastname .. to .. lastname=:lastName .. etc.
Also, would it better to have Account name as a search parameter rather than account Id? Of course that's upto you.
Hi Rajendra,
Now I have initialized both contact list and contact instance in controller, then even if I am not entering any inputs I am getting error as No Matching Record found which means contact list is null.
Case 2 :- If I am entering all values and hitting search, then getting error as
System.StringException: Invalid id:
Error is in expression '{!Searchresult}' in component <apex:commandButton> in page contactsearchpage: Class.ContactSearchController.Searchresult: line 13, column 1
Class.ContactSearchController.Searchresult: line 13, column 1
Hi Frank,
Even I too thought of taking input text at first instance but then I have to create custom lookup field for Account. My requirement is to input Account from look up along with email, phone and last name so result will come out as all matching records of associated acount.
change your page to use a standard controller, and use your class as an extension .. ie.
<apex:page standardController="Contact" extensions="ContactSearchController" >
and then you could have the Account input as ... <apex:inputField value="{!Contact.account}"/>
.. and the other search fields as <apex:inputText>
Rajendra
1) When I am inputting all values for fields where it should show me search result I am getting below error message.
Visualforce Error
System.StringException: Invalid id:
Error is in expression '{!Searchresult}' in component <apex:commandButton> in page contactsearchpage: Class.ContactSearchController.Searchresult: line 13, column 1
Class.ContactSearchController.Searchresult: line 13, column 1
2) when I am leaving all fields as blank and hitting search then getting error message on page as "No matching Contact record found."
Let me know if you used same code as I sent?
@Rajendra's code is 100% correct
use them
like
VF page -
Controller
Regards
Virendra
Rajendra Yes I have used same code and I am getting search results. But for blank input fields I am getting erroe message as "No matching Record found" but it should be "Please enter all input values."
Also please explain the changes you made I mean I wanted to the basic mistakes which I was doing so that going forward I should not repeat them.
1) Why we have used private set in while declaration of conlist and searchresults.
2) Why we have initialized conlist again in search button even If we had already initialized in controller.
>> As all conditions are in AND operator, it should be changed to OR
Line 12 of controller should be changes as follows:-
#2 Why we have used private set in while declaration of conlist and searchresults.
>> As these variables are read only and we never need values of these to be passed back to controller (in your context it doesn't matter)
#3 Why we have initialized conlist again in search button even If we had already initialized in controller.
>> because we don't want to display existing records if new search was done with empty fields.