You need to sign in to do that
Don't have an account?
Salesforce & Javascript / Ajax - Trying to use a parameters
Hi,
Following this post (made by myself) :
Hi,
I'm trying to make a autocomple with a home component.So far i found a way to it but it's perfect and i want to use parameters.
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<Code_Postal__c> searchCP2(string searchTerm) {
list<Code_Postal__c> matchingCP = new list<Code_Postal__c>();
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 :
<link rel="stylesheet" href="https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="/soap/ajax/15.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/15.0/apex.js" type="text/javascript"></script>
<script>
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;
}
});
});
}
</script>
Controller :
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;
}
}
My javascript component:
<link rel="stylesheet" href="https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="/soap/ajax/15.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/15.0/apex.js" type="text/javascript"></script>
<script>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 = sforce.apex.execute("cpSearch2", "searchCP", {});
var staheArray = stages.toString().split("+");
$ = jQuery.noConflict();
$(function() {
var availableTags = staheArray;
$( "#acc18zip" ).autocomplete({
source: availableTags
});
$( "#acc18zip" ).on("autocompleteselect", function( event, ui ){
selectedArray = ui.item.value.split(" - ");
$("#acc18zip").val(selectedArray[0]);
$("#acc18city").val(selectedArray[1]);
return false;
});
});
}
</script>