You need to sign in to do that
Don't have an account?
CharlieLang
Dynamic Search
I have used the dynamic search code from Jeff Douglas http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/
i have run into some difficulties - the dynamic search is not working and i can not work it out so i added a submit button but this just gives an error
Attempt to de-reference a null object
this is the controller
public with sharing class ContactSearchController { // the soql without the order and limit private String soql {get;set;} // the collection of contacts to display public List <Contact> contacts {get;set;} // the current sort direction. defaults to asc public String sortDir { get { if (sortDir == null) { sortDir = 'asc'; } return sortDir; } set; } // the current field to sort by. defaults to last name public String sortField { get { if (sortField == null) {sortField = 'lastName'; } return sortField; } set; } // format the soql for display on the visualforce page public String debugSoql { get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; } set; } // init the controller and display some sample data when the page loads public ContactSearchController() { soql = 'select firstName, lastName, account.name, Employee_Number__c from contact where account.name != null'; runQuery(); } // toggles the sorting of query from asc<-->desc public void toggleSort() { // simply toggle the direction sortDir = sortDir.equals('asc') ? 'desc' : 'asc'; // run the query again runQuery(); } // runs the actual query public void runQuery() { try { contacts = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'); } catch (Exception e) { ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!')); } } // runs the search with parameters passed via Javascript public PageReference runSearch() { String firstName = Apexpages.currentPage().getParameters().get('firstName'); String lastName = Apexpages.currentPage().getParameters().get('lastName'); String accountName = Apexpages.currentPage().getParameters().get('accountName'); soql = 'select firstName, lastName, account.name, Employee_Number__c, from contact where account.name != null'; if (!firstName.equals('')) soql += ' and firstName LIKE \''+String.escapeSingleQuotes(firstName)+'%\''; if (!lastName.equals('')) soql += ' and lastName LIKE \''+String.escapeSingleQuotes(lastName)+'%\''; if (!accountName.equals('')) soql += ' and account.name LIKE \''+String.escapeSingleQuotes(accountName)+'%\''; // run the query again runQuery(); return null; } }
and this is the VF Page
If anyone can help me you would be a lifesaver!!!
|
I've got this working in my dev org. Issues were:
1. In the doSearch method, you had a bonus comma after the final parameter:
2. The searchServer actionfunction must rerender something for the parameters to be sent back, otherwise its just a page refresh.
3. Null protected the first/last/account name parameters in the runSearch method:
All Answers
I think the null pointer exception is coming from this section of code:
You aren't passing parameters from the submit button, so firstName, lastName and accountName will be null. If you then attempt to execute the equals method on these variables you are dereferencing a null pointer and hence the exception
Thanks Bob.
Any idea on how i might be able to fix it as i've really got no idea. thanks for your contined help!!
There's also a bonus comma in your query:
the comma after Employee_Number__c will cause the dynamic soql to fail.
That hasnt fixed it
Seems odd that the onkeyup part isnt working, thats the bit i can't get my head round....
I've got this working in my dev org. Issues were:
1. In the doSearch method, you had a bonus comma after the final parameter:
2. The searchServer actionfunction must rerender something for the parameters to be sent back, otherwise its just a page refresh.
3. Null protected the first/last/account name parameters in the runSearch method:
Bob. thats amazing. thatnkyou so much for your help.
One more question ;-)
any idea on how i can make each of the lines in the search results a link to the relavant page?
I usually change the name field to be something like:
would be so much easier if i could do this
Is contact.link available? I though that was just for html templates.
unfortunately i think it is just for templates :-(
The alternative is:
But that's not really any different IMHO.
got it...
just adding the below works
Thanks for the help bob!