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
sales@myvarmasales@myvarma 

vf page to search for opportunities using name

i mm trying to create vf page search field 
when i search with name should get opportunities with that name 
and we should get option to edit their it self
Best Answer chosen by sales@myvarma
BALAJI CHBALAJI CH
You can try using Like operator in SOQL. Please see below for more information:
http://salesforcedeveloperblog.blogspot.in/2011/07/like-operator-in-soql.html
https://developer.salesforce.com/forums/?id=906F00000008ny2IAA

Example: 
Opp = [select id, Name, Amount from Opportunity where name LIKE: Name+'%' ];

 

All Answers

BALAJI CHBALAJI CH
Hi Pradeep,

Please find below sample VF page and Controller. Here, I am querying only one Opportunity, you can change it as per your requirement.

VF Page: OppNameSearch
<apex:page controller = "OppNameSearchController" >
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection title="Search Filter" >
                Opportunity Name: <apex:inputText value="{!Name}" />
                <apex:commandButton value="Search" action="{!method}" />
            </apex:pageBlockSection>
            
            <apex:pageBlockSection title="Searched Record" rendered="{!show}" >
                <apex:inlineEditSupport showOnEdit="saveButton, cancelButton" 
                                        hideOnEdit="editButton" event="ondblclick" 
                                        changedStyleClass="myBoldClass" resetFunction="resetInlineEdit">
                    <apex:outputField value="{!Opp.Name}"></apex:outputField>
                    <apex:outputField value="{!Opp.Amount}"/>
                </apex:inlineEditSupport>
                <apex:commandButton action="{!save}" />
            </apex:pageBlockSection>
        </apex:pageBlock>        
    </apex:form>
</apex:page>

Controller: OppNameSearchController​
public class OppNameSearchController {
    public string Name{set;get;}
    public Opportunity Opp{set;get;}
    public boolean show{set;get;}
    
    public void method()
    {
        Opp = [select id, Name, Amount from Opportunity where name=: Name limit 1];
        show = true;
    }
    
    public void save()
    {
        Opportunity op = Opp;
        update op;
    }
}

Please let us know if that helps you.

Best Regards,
BALAJI
sales@myvarmasales@myvarma
bro its showing error after clicking search button
System.QueryException: List has no rows for assignment to SObject
Error is in expression '{!method}' in component <apex:commandButton> in page oppnamesearch: Class.OppNameSearchController.method: line 8, column 1
Class.OppNameSearchController.method: line 8, column 1
BALAJI CHBALAJI CH
It is showing the error because there are no records with the given Name. Change the code like below so that it will notify you wothout the Exception.

VF Page:
<apex:page controller="OppNameSearchController" >
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection title="Search Filter" >
                Opportunity Name: <apex:inputText value="{!Name}" />
                <apex:commandButton value="Search" action="{!method}" />
            </apex:pageBlockSection>
            <apex:pageMessages></apex:pageMessages>
            
            <apex:pageBlockSection title="Searched Record" rendered="{!show}" >
                <apex:inlineEditSupport showOnEdit="saveButton, cancelButton" 
                                        hideOnEdit="editButton" event="ondblclick" 
                                        changedStyleClass="myBoldClass" resetFunction="resetInlineEdit">
                    <apex:outputField value="{!Opp.Name}"></apex:outputField>
                    <apex:outputField value="{!Opp.Amount}"/>
                </apex:inlineEditSupport>
                <apex:commandButton action="{!save}" value="Save" />
            </apex:pageBlockSection>
        </apex:pageBlock>        
    </apex:form>
</apex:page>

Controller:
public class OppNameSearchController {
    public string Name{set;get;}
    public Opportunity Opp{set;get;}
    public boolean show{set;get;}
    
    public void method()
    {
        Opp=Null;
        integer OppCount=[select count() from opportunity where name=: Name limit 1];
        if(OppCount != 0)
        {
            Opp = [select id, Name, Amount from Opportunity where name=: Name limit 1];
            show = true;
        }
        else
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'There are no Opportunities with the Name -'+Name);
            ApexPages.addMessage(myMsg);
            show = false;
        }
    }
    
    public void save()
    {
        Opportunity op = Opp;
        update op;
    }
}

Please let us know if that helps you.

Best Regards,
BALAJI
sales@myvarmasales@myvarma
i must get result of all opportunities list when we type one letter p and
every opprotunity named p as starting letter must be result
BALAJI CHBALAJI CH
You can try using Like operator in SOQL. Please see below for more information:
http://salesforcedeveloperblog.blogspot.in/2011/07/like-operator-in-soql.html
https://developer.salesforce.com/forums/?id=906F00000008ny2IAA

Example: 
Opp = [select id, Name, Amount from Opportunity where name LIKE: Name+'%' ];

 
This was selected as the best answer
sales@myvarmasales@myvarma
when i m previewing with k as input its throwing error like this 

System.QueryException: List has more than 1 row for assignment to SObject
Error is in expression '{!method}' in component <apex:commandButton> in page oppnamesearch: Class.OppNameSearchController.method: line 13, column 1
 
sales@myvarmasales@myvarma
limit 20 i have given but i dnt no y its still throwing a error