+ Start a Discussion
Sarju MulmiSarju Mulmi 

sforce.apex.execute not working

All,

I'm trying to call an apex function from a javascript button. I've read the developer guide and several relevant posts on this forum, but still can find out the issue. I have not created any namespace prefix for my dev org. The conroller is different from the Helper class with the web service method.
VF page:
<apex:page showHeader="false" >
  
 <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 type="text/javascript">
    
    function testRemote(){
        var id = sforce.apex.execute("HelperClass","getContactId",{});
        alert('id is: '+id);
        }
 
 </script>
  
 <div>  
    <button onclick="testRemote();">
      Remote
    </button>
  </div>
 </apex:page>

Apex class for webservice:
global class HelperClass {
    
    webService static String getContactId(){
       
        String id = '003j0000002CQ3B';
        return id;
    	}
}
Much appreciated,

Sarju
 
Best Answer chosen by Sarju Mulmi
Nayana KNayana K
<apex:page showHeader="false" >
<apex:includeScript value="/soap/ajax/36.0/connection.js"/>
<apex:includeScript value="/soap/ajax/36.0/apex.js"/>
 
 <script type="text/javascript">
    
    function testRemote(){
        sforce.connection.sessionId = '{!$Api.Session_ID}';
        try {
          var id = sforce.apex.execute("HelperClass","getContactId",{});
                alert('id is: '+id);
        } catch(e) {
          alert('Exception'+e)
        }
        
        }
 
 </script>
  
 <div>  
    <button onclick="testRemote();">
      Remote
    </button>
  </div>
 </apex:page>

 

All Answers

Nayana KNayana K
<apex:page showHeader="false" >
<apex:includeScript value="/soap/ajax/36.0/connection.js"/>
<apex:includeScript value="/soap/ajax/36.0/apex.js"/>
 
 <script type="text/javascript">
    
    function testRemote(){
        sforce.connection.sessionId = '{!$Api.Session_ID}';
        try {
          var id = sforce.apex.execute("HelperClass","getContactId",{});
                alert('id is: '+id);
        } catch(e) {
          alert('Exception'+e)
        }
        
        }
 
 </script>
  
 <div>  
    <button onclick="testRemote();">
      Remote
    </button>
  </div>
 </apex:page>

 
This was selected as the best answer
Manish BhatiManish Bhati
Hi Sarju,

The code is not having session ID so it is breaking.
Use below code:-
<apex:page showHeader="false" >
  
 <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 type="text/javascript">
    
    function testRemote(){
        sforce.connection.sessionId = '{!$Api.Session_ID}';
        var id = sforce.apex.execute("HelperClass","getContactId",{});
        alert('id is: '+id);
        }
 
 </script>
  
 <div>  
    <button onclick="testRemote();">
      Remote
    </button>
  </div>
 </apex:page>
Hope this helps.
 
Ankit Gupta@ DeveloperAnkit Gupta@ Developer
Hi Sarju,

Try below vf page code:

<apex:page showHeader="false" >
  
 <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 src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

 
 <script type="text/javascript">
    
    function testRemote(){
       sforce.connection.sessionId="{!$Api.Session_ID}";
        var id = sforce.apex.execute("ahome.HelperClass","getContactId",{});
        alert('id is: '+id);
        }
 
 </script>
  
 <div>  
    <button onclick="testRemote();">
      Remote
    </button>
  </div>
 </apex:page>

If any name space is registering in your org replace it with “ahome” otherwise remove the ahome. From code before replacing you vf page code.
 
 
Sarju MulmiSarju Mulmi
Thanks all. Worked like a charm!!
My only gripe is not asking the forum sooner!!!!!:P
Renil RejithRenil Rejith
Hi, 
In the above apex class, for the getContactId() method sarju is not passing any parameters!! So, what is that method had some parameter say like an Id, Will the javascript code have any changes in the vf page or will it remain the same?

Thank u