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
afoxmicroedgeafoxmicroedge 

Create custom object record from Apex

Hello-

 

I have created a custom search page for use in our Customer Portal. I want to track what customers are searching for so have created a custom object Search_Log__c.  Each time somebody executes search I want to create a new record on this custom object populating a field Search_term__c on the custom object with whatever they search for. 

 

I am having trouble figuring out how to go about doing this. Haven't found relevant documentation or message board posts that have guided me in right direction yet. 

 

Relevant code from VF page:

  <div id="search-box">
   <form name="frmSearch" id="search-form">
      <script type="text/javascript">
       function noenter(e){
            if(window.event){
            key = window.event.keyCode; 
            } else{
            key = e.which; //firefox
            }
            if(key == 13) {
            var ele=document.getElementById("search-button");
            ele.click();
            return false;
            } else{
            return true;
             }
           }
    </script> 
      <input type="text" id="search-text" name="searchText" value="{!portalSearchModel.searchTerm}" onkeypress="return noenter(event);" placeholder="What are you looking for?" />
      <input type="button" id="search-button" name="btnSearch" value="Search" onclick="insertSearchTerm()" />
  </form>
 </div>


<script type='text/javascript'> <!--executes search by passing search term to URL-->
    function insertSearchTerm(){
        var searchTerm = document.getElementById("search-text").value; 
        var url="https://c.na11.visual.force.com/apex/portalcasesearch?s="+searchTerm; 
        window.location = url; 
        return false;
    }                
</script>

 

Controller:

public with sharing class PortalCaseSearchUserContExt {
    
    public final PortalCaseSearchModel portalSearchModel { get; private set; }
    
    public Boolean disableSearch { get; private set; }
             
    public String getSearchTerm(){
         
         if(ApexPages.currentPage().getParameters().get('searchTerm') != null){
             portalSearchModel.searchTerm = ApexPages.currentPage().getParameters().get('searchTerm');
         }
         portalSearchModel.searches();
         //Search_Term__c st = new Search_Term__c();
         //st.Search_Term = portalSearchModel.searchTerm;
         //st.User__c
         //User u = [Select id from user where id =: UserInfo.getUserId()];
         return portalSearchModel.searchTerm;
     }
    
    public PortalCaseSearchUserContExt(ApexPages.StandardController controller) {
        this.disableSearch = false;
        try {
          this.portalSearchModel = new PortalCaseSearchModel((User)controller.getRecord());
          /*If(UserInfo.getProfileId == '00eA0000000rUFC'){
              product = 'GIFTS';
          }else if(UserInfo.getProfileId == '00eA0000000rTWS'){
              product = 'FIMS';
          }*/
          
        } catch(PortalSearchException searchException) {
          ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, searchException.getMessage()));
          this.disableSearch = true;
        }
    }
     static testMethod void searchTest(){
        whiteSpaceTracking.setAlreadyCreatedWSP();
        User u = [SELECT Id, Name FROM User WHERE Id=:UserInfo.getUserId()];
        ApexPages.StandardController er = new ApexPages.standardController(u);
        PortalCaseSearchUserContExt psmer = new PortalCaseSearchUserContExt(er);
        System.currentPageReference().getParameters().put('s', 't');
        //System.currentPageReference().getParameters().put('bad', 't');
        ApexPages.StandardController sh = new ApexPages.standardController(u);
        PortalCaseSearchUserContExt psmsh = new PortalCaseSearchUserContExt(sh);
        System.currentPageReference().getParameters().put('s', 'test');
        ApexPages.StandardController sc = new ApexPages.standardController(u);
        PortalCaseSearchUserContExt psm = new PortalCaseSearchUserContExt(sc);
        psm.portalSearchModel.solutionSearch.nextSolutions();
        psm.portalSearchModel.solutionSearch.previousSolutions();
        psm.portalSearchModel.solutionAPSearch.nextSolutions();
        psm.portalSearchModel.solutionAPSearch.previousSolutions();
        psm.portalSearchModel.solutionFIMSSearch.nextSolutions();
        psm.portalSearchModel.solutionFIMSSearch.previousSolutions();
        psm.portalSearchModel.ideaSearch.nextIdeas();
        psm.portalSearchModel.ideaSearch.previousIdeas();
        psm.portalSearchModel.ideaFIMSSearch.nextIdeas();
        psm.portalSearchModel.ideaFIMSSearch.previousIdeas();
        psm.portalSearchModel.ideaAPSearch.nextIdeas();
        psm.portalSearchModel.ideaAPSearch.previousIdeas();
        //psm.portalSearchModel.questionSearch.nextQuestions();
        //psm.portalSearchModel.questionSearch.previousQuestions();
        //psm.portalSearchModel.replySearch.nextReplies();
        //psm.portalSearchModel.replySearch.previousReplies();
        psm.portalSearchModel.caseFIMSSearch.nextCases();
        psm.portalSearchModel.caseFIMSSearch.previousCases();
        psm.portalSearchModel.caseAPSearch.nextCases();
        psm.portalSearchModel.caseAPSearch.previousCases();
        psm.portalSearchModel.caseSearch.nextCases();
        psm.portalSearchModel.caseSearch.previousCases();
        psm.portalSearchModel.caseSearch.nextCases();
        
        psm.getSearchTerm();
        
        DictionarySearch ds = new DictionarySearch('test');
        ds.getSynonymSearchWords();
        ApexPages.currentPage().getParameters().put('searchTerm','test');

 

Cory CowgillCory Cowgill

Try something like this:

 

VF: 

 

<apex:commandButton value="Search" action="{!performSearch}/> <!-- Use Apex Command to execute controller method, passing along viewState information-->

 

Apex Controller

 

public ApexPages.PageReference performSearch()

{

     portalSearchModel.searches();

    Search_Log__c searchLog = new SearchLog(); // instantiate your SOBject for inser

    searchLog.searchTearm = portalSearchModel.searchTerm();

    searchLog.user = UserInfog.getUserId(); // No Need to Query, you can get this directly fro UserInfo object.

    insert searchLog; // Perform Insert 

 

}

 

I would suggest getting a better understanding of how Visualforce and Apex (MVC) works so that you know how to properly pass your values back and forth from your VF Page using ViewState, etc.

 

http://wiki.developerforce.com/page/An_Introduction_to_Visualforce

 

http://wiki.developerforce.com/page/An_Introduction_to_Visualforce_View_State

 

Hope that helps. 

 

afoxmicroedgeafoxmicroedge

Thanks for the response.

 

Is there a way to call an apex method and some javascript written in VF page from the same button? 

 

Currently my search executes by clicking a button in an html form which calls some JS. Was never able to get command button to execute my search properly.