You need to sign in to do that
Don't have an account?
Nalini 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.
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.
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
Can you change you code based on the below sample one?
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",{});
Anyway you have declared method as RemoteAction so you can invoke same method as follows
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.
If you still want to use AJAX API request , following is working code of page and controller.
Hope it helps you.
Thanks,
Anjith kumar.
add below code snippet before calling sforce.apex.execute()
sforce.connection.sessionId = "{!$Api.Session_ID}";