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
Nalini KasturiNalini Kasturi 

sforce.apex.execute not working , i found no such property in object in sforce.

Hi team,
 
sforce.apex.execute not working , i found no such property in object when i printed sforce and navigated into it. In lot of forms many people mentioned to use sforce.apex.execute to invoke apex class in visualforce page.

The following are the references i added . 

<apex:includeScript value="https://ap2.salesforce.com/soap/ajax/34.0/apex.js"/> <apex:includeScript value="https://ap2.salesforce.com/soap/ajax/34.0/connection.js"/>

and in the visualforce page i added below code  in the <script> tag


var retStr='';       
        console.log(sforce);
        retStr = sforce.apex.execute("RequestData", "getData",{}); 

I am using chrome to work.
Did this feature got discontinued.
My purpose is to invoke an apex code from the visualforce page.

 
AnjithKumarAnjithKumar
Hello Nalini,

Do you mean invoke apex code from javascript of Visualforce page?
Then there are several ways to invoke apex method from javascript of Visualforce page.

1. Using Remote Action methods https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_RemoteAction.htm
2. Using Remote Objects (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_remote_objects_example_simple.htm (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_remote_objects_example_simple.htm" target="_blank))
3. Using ActionFunction ( https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm" target="_blank))
4. Ajax invoking using SOAP api (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_and_ajax.htm (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_and_ajax.htm" target="_blank))


Let me know if you find its help you.
Thanks,
Anjith Kumar
Chandra Sekhar CH N VChandra Sekhar CH N V
Hi,

Can you change you code based on the below sample one?
 
{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")} 
var conid = '{!Contact.Id}'; 
var message = ""; 
var t = confirm("Do you wish to update address...?"); 
window.alert('method called'); 
if (t == true) { 
try{ 
message = sforce.apex.execute("MyWebService","myMethod",{contactId:conid}); 
window.alert('method called1'); 
}catch(err){ 
message += "Error description: " + err.description + "\n\n"; 
} 
window.alert(message); 
document.location.reload(true); 
}

 
Nalini KasturiNalini Kasturi
Thank you Anjith and Chandra, the main issue is i am unable to use sforce.apex.execute , it is not working showing sforce.apex.execute undefined. 
Nalini KasturiNalini Kasturi
To be more clear i need to invoke class from apex and in that class i will write logic to get some data.
Chandra Sekhar CH N VChandra Sekhar CH N V
Is your controller class declared as 'global'? and also the method should be declared with 'webservice' keyword.
Nalini KasturiNalini Kasturi
I am getting Cannot read Property 'execute' of undefined.
Chandra Sekhar CH N VChandra Sekhar CH N V
Can you please paste your code here?
Nalini KasturiNalini Kasturi
My apex class code is 

global with sharing class RequestData{
    public RequestData(ApexPages.StandardController controller) {
    }
    public void RequestData() { } // empty constructor
    
    @RemoteAction
    global static String getData() {
       String myTemplatesEndPoint='my tedy url';
        HttpRequest req = new HttpRequest();
           req.setEndpoint(myTemplatesEndPoint);
           req.setMethod('GET');
         //  if(token!=null)
           //{
             // req.setHeader('test', token);
         //  }
          
            
           Http http = new Http();
           HTTPResponse res = http.send(req);
          
     
          System.debug('test');
          System.debug(res.getBody());
      
      return res.getBody();
    }
   
    
}
The following are the references i added . 

<apex:includeScript value="https://ap2.salesforce.com/soap/ajax/34.0/apex.js"/> <apex:includeScript value="https://ap2.salesforce.com/soap/ajax/34.0/connection.js"/>

and in the visualforce page i added below code  in the <script> tag


var retStr='';       
        console.log(sforce);
        retStr = sforce.apex.execute("RequestData", "getData",{}); 
AnjithKumarAnjithKumar
Hello Nalini,

Anyway you have declared method as RemoteAction so you can invoke same method as follows
<script>     
    RequestData.getData(function(response){
    console.log(response);
    },{escape: true});
 </script>

For more info on RemoteAction methods https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_js_remoting_example.htm.

Thanks,
Anjith kumar.
AnjithKumarAnjithKumar
Hello Nalini,

If you still want to use AJAX API request , following is working code of page and controller.
 
<apex:page>
    <apex:includeScript value="/soap/ajax/29.0/connection.js"/>
    <apex:includeScript value="/soap/ajax/29.0/apex.js"/>
   <script>     
    var result = sforce.connection.login("username", "password+securityToken");
	var result = sforce.apex.execute("RequestData","getData",{});
    </script>
</apex:page>
 
global with sharing class RequestData{
    
    public void RequestData() { } // empty constructor
    
    Webservice global static String getData() {
        String myTemplatesEndPoint='my tedy url';
        HttpRequest req = new HttpRequest();
        req.setEndpoint(myTemplatesEndPoint);
        req.setMethod('GET');
        Http http = new Http();
        HTTPResponse res = http.send(req);
        return res.getBody();
    }
    
    
}
Hope it helps you.


Thanks,
Anjith kumar.
Chetan BhavsarChetan Bhavsar
Sometimes the session ID isn't set correctly, so you have to set it yourself.
add below code snippet before calling sforce.apex.execute()
sforce.connection.sessionId = "{!$Api.Session_ID}";