-5

VisualForce Page:

<apex:page standardController="Account" extensions="OpportunitySearchController">  
    <style type="text/css">
        body {background: #F3F3EC; padding-top: 15px}
    </style>

    <apex:form >
        <apex:pageBlock title="Search for Opportunities by Keyword" id="block" mode="edit">
            <apex:pageMessages />

            <apex:pageBlockSection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel for="searchText">Keyword</apex:outputLabel>
                    <apex:panelGroup >
                    <apex:inputText id="searchText" value="{!searchText}"/>
                    <apex:commandButton value="Search" action="{!search}" rerender="resultsBlock" status="status"/>
                    </apex:panelGroup>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:actionStatus id="status" startText="Searching... please wait..."/>
            <apex:pageBlockSection id="resultsBlock" columns="1">
                <apex:pageBlockTable value="{!searchResults}" var="o" rendered="{!NOT(ISNULL(searchResults))}">
                    <apex:column headerValue="Name">
                        <apex:outputLink value="/{!o.Id}">{!o.Name}</apex:outputLink>
                    </apex:column>
                    <apex:column value="{!o.StageName}"/>
                    <apex:column value="{!o.Amount}"/>
                    <apex:column value="{!o.CloseDate}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>

        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Code:

public class OpportunitySearchController { //added an instance varaible for the standard controller private ApexPages.StandardController controller {get; set;} // the actual account private Account a; // the results from the search. do not init the results or a blank rows show up initially on page load public List searchResults {get;set;}

// the text in the search box
public string searchText {
    get {
        if (searchText == null) searchText = 'Acme'; // prefill the serach box for ease of use
        return searchText;
    }
    set;
}

public OpportunitySearchController(ApexPages.StandardController controller) {

    //initialize the stanrdard controller
    this.controller = controller;
    this.a = (Account)controller.getRecord();

}

// fired when the search button is clicked
public PageReference search() {
    if (searchResults == null) {
        searchResults = new List<opportunity>(); // init the list if it is null
    } else {
        searchResults.clear(); // clear out the current results if they exist
    }
    // Note: you could have achieved the same results as above by just using:
    // searchResults = new List<categoryWrapper>();

    // use some dynamic soql to find the related opportunities by name
 String qry = 'Select o.Id, o.Name, o.StageName, o.CloseDate, o.Amount from Opportunity o Where AccountId = \'' +a.Id+ '\' And o.Name LIKE \'%'+searchText+'%\' Order By o.Name';
    searchResults = Database.query(qry);
    return null;
}

} error:Visualforce Error Help for this Page

System.QueryException: invalid ID field: null Error is in expression '{!search}' in component in page accountsearch: Class.OpportunitySearchController.search: line 39, column 1

Class.OpportunitySearchController.search: line 39, column 1

  • 3
    You did not escape you quotes with a \ – Eric Jun 4 '17 at 9:25
0

You are missing backslash in your SOQL where you are trying to create it using string. Please try the below query instead of your line, and try to save.

String qry = 'Select o.Id, o.Name, o.StageName, o.CloseDate, o.Amount from Opportunity o Where AccountId = \'' +a.Id+ '\' And o.Name LIKE \'%'+searchText+'%\' Order By o.Name';
  • Visualforce Error Help for this Page System.QueryException: invalid ID field: null Error is in expression '{!search}' in component <apex:commandButton> in page accountsearch: Class.OpportunitySearchController.search: line 39, column 1 Class.OpportunitySearchController.search: line 39, column 1 – user45705 Jun 6 '17 at 13:41
  • Can you please post your latest code, where you are facing the issue. – Nitish Singhal Jun 6 '17 at 13:47
  • I Posted the latest code Please help me @Nitish Singhal – user45705 Jun 8 '17 at 12:32
  • Hi, why do you need to include the a.id in the query. You are developing a search functionality, and I don't think any id will be available before search. So, please remove the search criteria of Id from your query. – Nitish Singhal Jun 15 '17 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.