+ Start a Discussion
Kelly KKelly K 

call apex method in html home component?

Hi All,

I've created an apex controller that uses SOSL for a custom search and a related visualforce page to display the results. I've also created a home page HTML component that contains a text field and button that would ideally call the method from my controller and update the visualforce page with the results.

I'm new to this part of coding and I'm not sure how to get the javascript in the HTML component to speak to my controller and redirect to the page with the results. Does anyone have any documentation, examples, or a good website reference? Any help would be greatly appreciated!

My HTML code for the home page component looks like this:
<form>
<input type="text" name="searchText" size="18" onkeypress="return noenter(event)"><button type="button" onclick="openSearch()">Go!</button>
<br><input type="checkbox" value="itemsOwned">Limit to items I own
</form>

The controller is simple:
public with sharing class CustomSearch {

public String searchText {get; set;}
public Boolean itemsOwned {get; set;}

public Id currentUser {
  get {
    if (currentUser == null)
     currentUser = userInfo.getUserId();
   
    return currentUser;
   }
  set;
}

public Account[] accountResults {get; set;}
public Contact[] contactResults {get; set;}
public Lead[] leadResults {get; set;}
public Opportunity[] opportunityResults {get; set;}

public PageReference doSearch() { 
  String queryString;
 
  if(!itemsOwned)
   queryString = 'FIND \'' + searchText +'*'+'\' IN ALL FIELDS RETURNING ' +
        'Account (Id, Name, Customer_ID__c, Status__c, Phone, Billable_Docs_Original__c, BillingStreet, BillingCity, BillingState), ' +
        'Contact (Id, Name, Title, Phone, Email, Account_Status__c, AccountId, MailingStreet, MailingState, Inactive__c, Navicure_User_Entity__c), ' +
        'Lead (Id, Name, Company, Status_of_Lead__c, Title, Phone, Email, Street, City, State), ' +
        'Opportunity (Id, Name, Type, AccountId, StageName, CloseDate, OwnerId)';
  else
   queryString = 'FIND \'' + searchText +'*'+'\' IN ALL FIELDS RETURNING ' +
        'Account (Id, Name, Customer_ID__c, Status__c, Phone, Billable_Docs_Original__c, BillingStreet, BillingCity, BillingState WHERE OwnerId = \'' + currentUser + '\'), ' +
        'Contact (Id, Name, Title, Phone, Email, Account_Status__c, AccountId, MailingStreet, MailingState, Inactive__c, Navicure_User_Entity__c WHERE OwnerId = \'' + currentUser + '\'), ' +
        'Lead (Id, Name, Company, Status_of_Lead__c, Title, Phone, Email, Street, City, State WHERE OwnerId = \'' + currentUser + '\'), ' +
        'Opportunity (Id, OwnerId, Name, Type, AccountId, StageName, CloseDate, OwnerId WHERE OwnerId = \'' + currentUser + '\')';
   

  try {
   list<list<sObject>> query = search.query(queryString);

   accountResults = ((list<Account>) query[0]);
   contactResults = ((list<Contact>) query[1]);
   leadResults = ((list<Lead>) query[2]);
   opportunityResults = ((list<Opportunity>) query[3]);  
  
  }
      catch (QueryException ex){
      ApexPages.addMessages(ex);
      return null;
      }
     
      catch (Exception exx){
       ApexPages.addMessages(exx);
       return null;
      }     

  return null;
    }
}
hitesh90hitesh90
Hello,

Add your javascript code within form tag. Below is the example how to add Javascript in HTML.

<form>
<script type="text/javascript">
  function openSearch(){
   window.location = "/apex/YourVFpageName";
  }
</script>

<input type="text" name="searchText" size="18" onkeypress="return noenter(event)">
<button type="button" onclick="openSearch()">Go!</button>
<br>
<input type="checkbox" value="itemsOwned">Limit to items I own
</form>

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

Kelly KKelly K
Hi Hitesh,

Do you have an example of how to call an apex class to pass parameters in and to run a function with javascript? Only thing I can find is info on javascript remoting, but I'm not convinced it's needed since the HTMl is located within salesforce. Am I thinking about this incorrectly?

Thanks!