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
LitlleNinjaLitlleNinja 

Apex controller path from javascript

Hi,

I created an home page component made with javascript.

It looks fine except that i don't  how to call my controller from my javascript. When i did from javascript from a visualforce page but from full javascript component it looks like the path is different.

 

I already read this article but it doesn't help( or maybe i just did'nt understand)

http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#CSHID=pages_js_remoting.htm|StartTopic=Content%2Fpages_js_remoting.htm|SkinName=webhelp

 

Note: when i inspected with firebug i saw this message:

 

Uncaught ReferenceError: AutoCompleteController is not defined

 

 

Here my Controller : 

global with sharing class AutoCompleteController {
    private final Movie__c mov; 
       
        // Instance fields
        public String searchTerm {get; set;}
        public String selectedMovie {get; set;}
        
        // Constructor
        public AutoCompleteController() {
                
        }
         public AutoCompleteController(ApexPages.StandardController stdController) {
        this.mov= (Movie__c)stdController.getRecord();
    }
        // JS Remoting action called when searching for a movie name
    @RemoteAction
    global static List<Movie__c> searchMovie(String searchTerm) {
        System.debug('Movie Name is: '+searchTerm );
        List<Movie__c> movies = Database.query('Select Id, Name,BillingPostalCode__c from Movie__c where BillingPostalCode__c like \'%' + String.escapeSingleQuotes(searchTerm) + '%\'');
        return movies;
    }
    
    @RemoteAction
    global static List<Movie__c> searchMovie2(String searchTerm2) {
        System.debug('Movie Name is: '+searchTerm2);
        List<Movie__c> movies = Database.query('Select Id, Name,BillingPostalCode__c from Movie__c where BillingPostalCode__c like \'%' + String.escapeSingleQuotes(searchTerm2) + '%\'');
        return movies;
    }
}

 Here my javascript

 

:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css"/>
<script type="text/javascript">
    /*Create a new variable j$ just to avoid any conflicts with other libraries which may be using $.*/
    var j$ = jQuery.noConflict();
    /*Capture the list of countries in a Array.*/
    var countryArray = ['India', 'USA', 'China','FInland','Norway','Netherlands','England','Ukraine','Russia','Japan','Korea','Burma','Srilanka','Iran','Iceland','Canada','Rome','Australia','Armenia','Albania','Afghanisthan'];
    /*on Document ready*/
    j$(document).ready(function(){
        var PLACEHOLDER = 'Enter Movie Name Here'; 
        var movieObjects;
        var queryTerm;
        j$('[id$=00Nb00000063dLB]').autocomplete({
            minLength: 2,
            source: function(request, response) {
                        queryTerm = request.term;
                        
						   AutoCompleteController.searchMovie2(request.term, function(result, event){
                            if(event.type == 'exception') {
                                  alert(event.message);
                            } else {
                                 movieObjects = result;
                                 response(movieObjects);
                            }
                        });
                   },
            focus: function( event, ui ) {
                    j$('[id$=00Nb00000063dLL]').val( ui.item.Name );
                    j$('[id$=00Nb00000063dLB]').val( ui.item.BillingPostalCode__c );
                    return false;
                    },
            select: function( event, ui ) {
                        j$('[id$=00Nb00000063dLL]').val( ui.item.Name );
                        return false;
                    },
         })
         
         .data( "autocomplete" )._renderItem = function( ul, item ) {
            var entry = "<a>" + item.Name + " " +item.BillingPostalCode__c;
           
            entry = entry + "</a>";
            entry = entry.replace(queryTerm, "<b>" + queryTerm + "</b>");
            return j$( "<li></li>" )
                .data( "item.autocomplete", item )
                .append( entry )
                .appendTo( ul );
        };
            
        /* Add or remove placeholder values*/
        j$('[id$=00Nb00000063dLL]').val(PLACEHOLDER);
       j$('[id$=00Nb00000063dLL]').on("focus",  function(event){
            j$tgt = j$(event.target);
            if(j$tgt.val() === PLACEHOLDER ){
                j$tgt.val('');
                j$tgt.removeClass('placeHolder');
            }
        });
        j$('[id$=00Nb00000063dLL]').on( "blur",  function(event){
            j$tgt = j$(event.target);
            if(j$tgt.val() === '' ){
                j$tgt.val(PLACEHOLDER);
                j$tgt.addClass('placeHolder');
            }
        });
		
    });
</script>  

 

 

sfdcfoxsfdcfox
Even though the class and methods are global, it won't be loaded unless the page uses it as the controller or an extension. Also, if your class is part of a managed package, the namespace will also have an effect:

mynamespace.AutoCompleteController.searchMovie(searchTerm, callback)
LitlleNinjaLitlleNinja

My class are not from a managed package because i develop it by myself.

It looks like my classes don't have a namespace.
how can i find it? or as you said wich extension should i use to make it work?

LitlleNinjaLitlleNinja

i checked one more time, my controller doesn't have any namespace.

 http://img11.hostingpics.net/pics/845371error.jpg

sfdcfoxsfdcfox
If your code is in an organization that has a defined namespace (you have the words, "Developing <package name>, latest version" in the upper-right corner), you have a namespace. Even if the class or page isn't part of the package, it still needs the namespace. You might also try code similar to the following:

var searchMovieFn = {!$RemoteAction.AutoCompleteController.searchMovie};

You can then call the function using:

searchMovieFn(searchTerm);

If it complains about a missing variable, then you'll know you haven't properly included the controller in the page.
LitlleNinjaLitlleNinja

It's looks like doesn't work ='(.

 

I really don't understand why è_è

LitlleNinjaLitlleNinja

I also tried to define the controller with <apex:component> but it's not working :/

 

<apex:component controller='AutoCompleteController'>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css"/>


<script type="text/javascript">
    /*Create a new variable j$ just to avoid any conflicts with other libraries which may be using $.*/
    var j$ = jQuery.noConflict();
    /*Capture the list of countries in a Array.*/
    var countryArray = ['India', 'USA', 'China','FInland','Norway','Netherlands','England','Ukraine','Russia','Japan','Korea','Burma','Srilanka','Iran','Iceland','Canada','Rome','Australia','Armenia','Albania','Afghanisthan'];
    /*on Document ready*/
    j$(document).ready(function(){
        var PLACEHOLDER = 'Enter Movie Name Here'; 
        var movieObjects;
        var queryTerm;
		
		
        j$('[id$=00Nb00000063dLB]').autocomplete({
            minLength: 2,
            source: function(request, response) {
                        queryTerm = request.term;
                        
						  AutoCompleteController.searchMovie2(request.term, function(result, event){
                            if(event.type == 'exception') {
                                  alert(event.message);
                            } else {
                                 movieObjects = result;
                                 response(movieObjects);
                            }
                        });
                   },
            focus: function( event, ui ) {
                    j$('[id$=00Nb00000063dLL]').val( ui.item.Name );
                    j$('[id$=00Nb00000063dLB]').val( ui.item.BillingPostalCode__c );
                    return false;
                    },
            select: function( event, ui ) {
                        j$('[id$=00Nb00000063dLL]').val( ui.item.Name );
                        return false;
                    },
         })
         
         .data( "autocomplete" )._renderItem = function( ul, item ) {
            var entry = "<a>" + item.Name + " " +item.BillingPostalCode__c;
           
            entry = entry + "</a>";
            entry = entry.replace(queryTerm, "<b>" + queryTerm + "</b>");
            return j$( "<li></li>" )
                .data( "item.autocomplete", item )
                .append( entry )
                .appendTo( ul );
        };
            
        j$('[id$=00Nb00000063dLL]').on( "blur",  function(event){
            j$tgt = j$(event.target);
            if(j$tgt.val() === '' ){
                j$tgt.val(PLACEHOLDER);
                j$tgt.addClass('placeHolder');
            }
        });
		
    });
</script>  
</apex:component>

 

LitlleNinjaLitlleNinja

hi

the goal of my development change.

 

i have to make an autocomplete on the shipping zipcode and shipping city.

all the zipcode and city are in a spécial object called "Code_Postal__c"

 

Here my crontroller :

global with sharing class AutoCompleteController {
    //private final Movie__c mov; 
    private Code_Postal__c cpCheck;
	private Account accToCheck;
	
        // Instance fields
        public String searchTerm {get; set;}
        public String selectedMovie {get; set;}
        
        // Constructor
        public AutoCompleteController() {
                
        }
		public AutoCompleteController(ApexPages.StandardController stdController) {
		this.accToCheck = (Account)stdController.getRecord();
        //this.mov= (Movie__c)stdController.getRecord();
    }
	
	// JS Remoting action called when searching for a cp
    @RemoteAction
    global static List<Code_Postal__c> searchMovie(String searchTerm) {
        System.debug('Movie Name is: '+searchTerm );
        List<Code_Postal__c> movies = Database.query('Select Commune__c, Code_Postal__c from Code_Postal__c where Code_Postal__c like \'%' + String.escapeSingleQuotes(searchTerm) + '%\'');
        return movies;
    }
}

 

 

here my script :

 

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css"/>
<script type="text/javascript">
    /*Create a new variable j$ just to avoid any conflicts with other libraries which may be using $.*/
    var j$ = jQuery.noConflict();
    /*Capture the list of countries in a Array.*/
    var countryArray = ['India', 'USA', 'China','FInland','Norway','Netherlands','England','Ukraine','Russia','Japan','Korea','Burma','Srilanka','Iran','Iceland','Canada','Rome','Australia','Armenia','Albania','Afghanisthan'];
    /*on Document ready*/
    j$(document).ready(function(){
        var PLACEHOLDER = 'Enter Code_Postal__c Here'; 
        var movieObjects;
        var queryTerm;
		
        j$('[id$=acc18zip]').autocomplete({
            minLength: 2,
            source: function(request, response) {
                        queryTerm = request.term;
                        
						   AutoCompleteController.searchMovie2(request.term, function(result, event){
                            if(event.type == 'exception') {
                                  alert(event.message);
                            } else {
                                 movieObjects = result;
                                 response(movieObjects);
                            }
                        });
                   },
            focus: function( event, ui ) {
                    j$('[id$=acc18zip]').val( ui.item.Code_Postal__c );
                    j$('[id$=acc18city]').val( ui.item.Commune__c );
                    return false;
                    },
            select: function( event, ui ) {
                        j$('[id$=acc18zip]').val( ui.item.Code_Postal__c );
                        return false;
                    },
         })
         
         .data( "autocomplete" )._renderItem = function( ul, item ) {
            var entry = "<a>" + item.Code_Postal__c + " " +item.Commune__c;
           
            entry = entry + "</a>";
            entry = entry.replace(queryTerm, "<b>" + queryTerm + "</b>");
            return j$( "<li></li>" )
                .data( "item.autocomplete", item )
                .append( entry )
                .appendTo( ul );
        };
            
        /* Add or remove placeholder values*/
		j$('[id$=acc18zip]').val(PLACEHOLDER);
		j$('[id$=acc18zip]').on("focus",  function(event){
            j$tgt = j$(event.target);
            if(j$tgt.val() === PLACEHOLDER ){
                j$tgt.val('');
                j$tgt.removeClass('placeHolder');
            }
        });
        j$('[id$=acc18zip]').on( "blur",  function(event){
            j$tgt = j$(event.target);
            if(j$tgt.val() === '' ){
                j$tgt.val(PLACEHOLDER);
                j$tgt.addClass('placeHolder');
            }
        });
		
    });
</script>  

 and i still have the problem... when i launched the editing mode my shipping zip code was populate with the value "Enter Code_Postal__c Here" but the search doesnt work.

 

when i inspected the page with firebug i saw this :

http://img4.hostingpics.net/pics/449322error.jpg

if someone could help me please T_T i'm pretty bad with javascript