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
Jayati AroraJayati Arora 

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;
    }
}

Best Answer chosen by Jayati Arora
Rajendra ORajendra O
#1 I am getting erroe message as "No matching Record found" but it should be "Please enter all input values."
>> As all conditions are in AND operator, it should be changed to OR
Line 12 of controller should be changes as follows:-
if((c.Lastname == null || c.Lastname=='') || (c.Email==null || c.Email=='') || (c.Phone==null || c.Phone =='') || c.AccountId==null){
#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

Rajendra ORajendra O
At line 11 you are reinitializing the contact instance. So it will loose any data you submitted from UI.
removing C = new Contact(); should return you result.
Jim JamJim Jam
Your seach boxes are bound to a contact object that is not populated with a record which I why I think you're getting null values. Try creating properties for your search parameters .. ie.
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.
Jayati AroraJayati Arora

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

Rohit SharmaGRohit SharmaG
Remove C = new Contact(); from the Searchresult() method. 

Jayati AroraJayati Arora

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.
 

Rajendra ORajendra O
Could you try this and let me know the error message:-
public class ContactSearchController{
    public Contact C {get;set;}
    public list<contact> conList{get; private set;}
    public boolean showsearchresults {get; private set;}

    public ContactSearchController(){
        C = new Contact();
        showsearchresults = false;
	conList = new list<contact>();
    }
    public pagereference Searchresult(){
	conList = new list<contact>();
	if(c.Lastname == null || c.Lastname=='' && c.Email==null || c.Email=='' && c.Phone==null || c.Phone =='' && c.AccountId==null){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please enter values for all input fields'));
	    showSearchResults=false;
	    return null;
        }
        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)];
        showSearchResults = !conList.isEmpty(); 
		
	if(!showSearchResults){
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'No matching Contact record found');
            ApexPages.addMessage(myMsg);
            showSearchResults=false;
	}
            
        return null;
    }
}

Jim JamJim Jam
If you want the Account field to be a lookup you could try this ..

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>
Jayati AroraJayati Arora

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."

Rajendra ORajendra O
at line 13 you must be comparing c.accountid != '' ? If so that's the error, we can't compare id like this.
Let me know if you used same code as I sent?
Virendra ChouhanVirendra Chouhan
Hi Jayanti

@Rajendra's code is 100% correct 

use them 
like 
VF page -
<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>

Controller 
public class ContactSearchController{
    public Contact C {get;set;}
    public list<contact> conList{get; private set;}
    public boolean showsearchresults {get; private set;}

    public ContactSearchController(){
        C = new Contact();
        showsearchresults = false;
    conList = new list<contact>();
    }
    public pagereference Searchresult(){
       if(c.Lastname == null || c.Lastname=='' && c.Email==null || c.Email=='' && c.Phone==null || c.Phone =='' && c.AccountId==null){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please enter values for all input fields'));
        showSearchResults=false;
        return null;
        }
        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)];
        showSearchResults = !conList.isEmpty(); 
        
    if(!showSearchResults){
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'No matching Contact record found');
            ApexPages.addMessage(myMsg);
            showSearchResults=false;
    }
            
        return null;
    }
}

Regards
Virendra
Jayati AroraJayati Arora

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.

 

Rajendra ORajendra O
#1 I am getting erroe message as "No matching Record found" but it should be "Please enter all input values."
>> As all conditions are in AND operator, it should be changed to OR
Line 12 of controller should be changes as follows:-
if((c.Lastname == null || c.Lastname=='') || (c.Email==null || c.Email=='') || (c.Phone==null || c.Phone =='') || c.AccountId==null){
#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.
This was selected as the best answer
Jayati AroraJayati Arora
Thanks a lot Rajendra. You were very helpful.  :)