Salesforce & Javascript / Ajax – Trying to use a parameters-Collection of common programming errors
Following this post :
Define Apex controller in javascript home component
I tried to improved it, because the limit of Salesforce for an SOQL request is 50k records.
The Code_Postal__c object can have more than 50k of records.
I saw that we can parse item in javascript (i’m pretty bad with this language). The idea is to make a dynamic call of my controller when the user will start typing the postal code ( id of the field = #acc18zip ). But i got an error with my item list, so i came back to use my string but it didn’t work. When i tried to find the error with the chrome console or firebug i saw this error into my console :
Uncaught SyntaxError: Unexpected end of input
and when i start typing a postal code:
event.returnValue is deprecated. Please use the standard event.preventDefault() instead.
My updated controller is this one :
global class cpSearch2{
webService static String searchCP() {
String pickValues='';
for(Code_Postal__c cp : [Select Commune__c, Code_Postal__c from Code_Postal__c ]){
pickValues = pickValues +cp.Code_Postal__c+ ' - ' + cp.Commune__c+'+';
}
return pickValues;
}
webService static string searchCP2(string searchTerm) {
String pickValues='';
for(Code_Postal__c cp : [Select Commune__c, Code_Postal__c from Code_Postal__c where Code_Postal__c LIKE :searchTerm]){
pickValues = pickValues +cp.Code_Postal__c+ ' - ' + cp.Commune__c+'+';
}
return pickValues;
}
/*
Function with list of object
webService static list searchCP2(string searchTerm) {
list matchingCP = new list();
for(Code_Postal__c cp : [Select Commune__c, Code_Postal__c from Code_Postal__c where Code_Postal__c LIKE :searchTerm]){
matchingCP.add(cp);
}
return matchingCP;
}*/
}
and the updated javascript is :
var url = document.URL;
if(url.indexOf('001')!=-1)
{
var sid = document.cookie.match(' sid=([^;]*)')[1];
sforce.debug.trace=true;
sforce.connection.sessionId = sid; ;
var stages;
var cpObjects;
var queryTerm;
$ = jQuery.noConflict();
$(function()
{
$( "#acc18zip" ).autocomplete({
source:function( request, response ) {
queryTerm = request.term;
stages = sforce.apex.execute("cpSearch2", "searchCP2", {queryTerm,function(result, event)});
if(event.type == 'exception') {
alert(event.message);
} else {
cpObjects = stages.toString().split("+");
response(cpObjects);
}
},
focus: function( event, ui ) {
$("#acc18zip").val(selectedArray[0]);
$("#acc18city").val(selectedArray[1]);
return false;
},
select: function( event, ui ) {
console.log(ui.item);
selectedArray = ui.item.split(" - ");
$("#acc18zip").val(selectedArray[0]);
$("#acc18city").val(selectedArray[1]);
return false;
}
});
});
}
Trying to follow this :