+ Start a Discussion
Swetha Sfdc1Swetha Sfdc1 

Using jQuery’s AutoComplete in Salesforce Soql Help need

Hi Friends Thanks in advance,
Agenda: insted of "Account" object I need Organization__C
I want this feature for My custom Object (i.e., Organization__C) where can i have place my custom object insted of 'Account' Object.

When i replace Account with my custom object I am facing Below error
"Visualforce Error  System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Organization__C.Name"

Working Code
Apex Class
public class AutoCompleteDemoController{
    public list<account> getAccountList(){
        return [select id,name from account limit 25];
    }
}
VF Page
<apex:page controller="AutoCompleteDemoController">
    <!--Make sure you have the Javascript in the same order that I have listed below.-->
    <script src="https://code.jquery.com/jquery-1.8.2.js"></script>
    <script src="/soap/ajax/26.0/connection.js" type="text/javascript"></script>
    <script src="https://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css"/>
    <script type="text/javascript">
        var j$ = jQuery.noConflict();
        var apexAccountList =[];
         
        //use the <!-- <apex:repeat> -->tag to iterate through the list returned from the class and store only the names in the javascript variable.
        <apex:repeat value="{!accountList}" var="accList">
            //Store the name of the account in the array variable.
            apexAccountList.push('{!accList.name}');
        </apex:repeat>
         
        //We will establish a connection salesforce database using the sforce.connection.init(sessionID, ServerURL) function.
        var sid = '{!$Api.Session_ID}';
        var server = "https://" + window.location.host + "/services/Soap/u/26.0";
        sforce.connection.init(sid, server);
         
        //We will query the contact object using the sforce.connection.query function. This will return 200 results.
        var result = sforce.connection.query("select Name from Contact");
        var records = result.getArray("records");
        var javascriptContactList =[];
         
        //Iterate thru the list of contact and store them in a javascript simple array variable which will then assign it to the source of the autocomplete.
        for(i=0;i<records.length;i++){
            javascriptContactList[i]=records[i].Name;
        }
        //on Document ready
        j$(document).ready(function(){
             
            j$("#apexaccountautocomplete").autocomplete({
                source : apexAccountList
            });
            j$("#sfjscontactautocomplete").autocomplete({
                source : javascriptContactList
            });
        });
    </script>
     
    <apex:form>
        <b>Account(This uses the Apex class to display the list)</b><input type="text" id="apexaccountautocomplete"/><br/><br/>
        <b>Contact(This uses the salesforce's ajax toolkit to display the list)</b><input type="text" id="sfjscontactautocomplete"/>
    </apex:form>
     
</apex:page>

Thanks
Swetha

 
Waqar Hussain SFWaqar Hussain SF
Please add "Name" field where you are quering Organization__c LIKE
 
Select Name from Organization__c

 
Waqar Hussain SFWaqar Hussain SF
Can you please paste your controller here. So that I can fully understand your problem.
Swetha Sfdc1Swetha Sfdc1
'Organization__C' is the Object. instead of Name Object i have 'ITdept__C'
Means - instead of 'Account' : 'Organization__C'
             instead of id,name i have 'ITdept__C'
I have tried like this
public class AutoCompleteDemoController{
    public list<Organization__C> getAccountList(){
        return [select ITdept__C from Organization__C];
    }
}
I tried this Still I am facing same VF error
Need some changes in Jquery Script

Thanks
Swetha
Swetha Sfdc1Swetha Sfdc1
Apex class
public class AutoCompleteDemoController{
    public list<Organization__C> getAccountList(){
        return [select ITdept__C from Organization__C];
    }
}
VF Page
 
<apex:page controller="AutoCompleteDemoController">
    <!--Make sure you have the Javascript in the same order that I have listed below.-->
    <script src="https://code.jquery.com/jquery-1.8.2.js"></script>
    <script src="/soap/ajax/26.0/connection.js" type="text/javascript"></script>
    <script src="https://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css"/>
    <script type="text/javascript">
        var j$ = jQuery.noConflict();
        var apexAccountList =[];
         
        //use the <!-- <apex:repeat> -->tag to iterate through the list returned from the class and store only the names in the javascript variable.
        <apex:repeat value="{!accountList}" var="accList">
            //Store the name of the account in the array variable.
            apexAccountList.push('{!accList.name}');
        </apex:repeat>
         
        //We will establish a connection salesforce database using the sforce.connection.init(sessionID, ServerURL) function.
        var sid = '{!$Api.Session_ID}';
        var server = "https://" + window.location.host + "/services/Soap/u/26.0";
        sforce.connection.init(sid, server);
         
        //We will query the contact object using the sforce.connection.query function. This will return 200 results.
        var result = sforce.connection.query("select Name from Contact");
        var records = result.getArray("records");
        var javascriptContactList =[];
         
        //Iterate thru the list of contact and store them in a javascript simple array variable which will then assign it to the source of the autocomplete.
        for(i=0;i<records.length;i++){
            javascriptContactList[i]=records[i].Name;
        }
        //on Document ready
        j$(document).ready(function(){
             
            j$("#apexaccountautocomplete").autocomplete({
                source : apexAccountList
            });
            j$("#sfjscontactautocomplete").autocomplete({
                source : javascriptContactList
            });
        });
    </script>
     
    <apex:form>
        <b>Account(This uses the Apex class to display the list)</b><input type="text" id="apexaccountautocomplete"/><br/><br/>
        <b>Contact(This uses the salesforce's ajax toolkit to display the list)</b><input type="text" id="sfjscontactautocomplete"/>
    </apex:form>
     
</apex:page>
Thanks
Waqar Hussain SFWaqar Hussain SF
public class AutoCompleteDemoController{
    public list<Organization__C> getAccountList(){
        return [select ITdept__c, ITdept__r.Name from Organization__C];
    }
}

Or I think you should be 
 
public class AutoCompleteDemoController{
    public list<Organization__C> getAccountList(){
        return [select ITdept__c, Name from Organization__C];
    }
}

 
TanDTanD

Hello, 

The code is fine for STANDARD object, but not working in CUSTOM object. What did I miss?
Name__c is a custom field in D__c object

//We will query the Journal custom object using the sforce.connection.query function..
        var resultJ = sforce.connection.query("select Name__c from D__c"); 
        var recordsJ = resultJ.getArray("recordsJ");
        var javascriptDList =[];
         
        //Iterate thru the list of D
        for(j=0;j<recordsJ.length;j++){
            javascriptDList[j]=recordsJ[j].Name__c;
        }

        inside Script: 

j$("#sfjsDautocomplete").autocomplete({                 
source : javascriptDList             });