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
Ramana123Ramana123 

when I click the search button page should redirect ,but no longer url exits error using page reference

controller

public class searchClass
{
    public List<Contact> contacts { get; set; }
    public String name { get; set; }
    
    public searchClass()
{
contacts = new List<Contact>();
}
    public pageReference searchcontacts()
    {
        contacts = [select Id
                    ,Name 
                    from Contact
                    where Name = :name];
        
        if(contacts.size() == 0) {    
               ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invalid Input.');
                ApexPages.addMessage(myMsg);
            //ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR,'No Records Found'));
                  }
        if(contacts.size() == 1) 
        {
            for(Contact con :contacts ){
// In this This section my problem not redirecting

                PageReference pageRef = new PageReference('con.Id');       
            
     
        return pageRef;
            }
        }
        return null;

        }       
        
}


page 



<apex:page controller="searchClass">
    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock >
            <apex:pageBlockSection id="contact-table" columns="1">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Name"/>
                    <apex:inputText value="{!name}" />
                </apex:pageBlockSectionItem>          
                <apex:commandButton value="Search" action="{!searchcontacts}" reRender="contact-table" >
                </apex:commandButton>
                <apex:pageBlockTable value="{!contacts}" var="c" >
                    <apex:column >
                        <apex:outputlink value="/{!c.Id}"> {!c.Name}</apex:outputlink>
                    </apex:column>                 
                </apex:pageBlockTable>
                
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>




 
SwethaSwetha (Salesforce Developers) 
HI Srikanth,
I tried your code in my org and see the error message as "Formula Expression is required on the action attributes."

You might want to try the approach mentioned in https://developer.salesforce.com/forums/?id=906F000000099FyIAI

Note: page reference cannot just return a string, or you get the error mentioned above.

If this information helps, please mark the answer as best.Thank you