i have one rest class for insert account , it's working with workbench.developerforce.com

but now i want to call same using HTML or JavaScript , how can i do ?

My Code :

global class InsertAccount{
global static String InsertAccount()

    // Insert Account Using Post Method

    Account  acc = new Account();
    String jsonStr = null;

    if (null != RestContext.request.requestBody){

    jsonStr = RestContext.request.requestBody.toString();
    Map<String, object> m = (Map<String, object>)JSON.deserializeUntyped(jsonStr );
    system.debug('******'+m );
    insert acc;
    return 'Account Inserted';

global static String InsertAccountRest()

           // Insert Account Using Get Method

    Account  acc1 = new Account();
    insert acc1;

    return 'Account Inserted';



First of all, ensure that your Class is global and method is webservice like below -

global class MyClass
    webservice static void myMethod() // you can pass parameters
         // Do something

Next if you are calling from Button Click then ensure that Button behaviour is as below-

  • Behaviour : Execute Javascript
  • Content source : On-Click Javascript

Ensure that you include appropriate JS files in the Script-


Method can be called as below -


If method accepts a parameter, the same can be passed in the third argument comma seperated as below -

   sforce.apex.execute("MyClass","myMethod",{requestString:"Sample Request",endpoint:"www.a.com/b"}"});

Now if you are calling from a VF page then the code remains same but ensure that you have added scripts to the top as below:

<apex:includeScript value="/soap/ajax/30.0/connection.js"/>
<apex:includeScript value="/soap/ajax/30.0/apex.js"/>

If you are calling from Outside Salesforce then I would suggest -

  1. Setting OAUTH in salesforce by creating a connected App. (App Setup>Create>Apps)
  2. Change the URL mapping to the top to display appropriate URL. ( This will be endpoint)
  3. then you can use either Curl or SOAP UI tool or hurl.it to check if your class gives proper response.

The process is bit lengthy to explain, and I found a good link that sums it all - http://www.oyecode.com/2014/08/start-building-your-own-rest-api-in.html

| improve this answer | |
  • i want to call outside of salesforce? – Parth Thakkar Aug 6 '15 at 8:58
  • You can directly hit the endpoint and pass the required parameter in body with appropriate request (GET,POST). Are you getting any error in this approach? Did you authenticate yourself before calling endpoint? – Amit Bangad Aug 6 '15 at 9:11
  • how ? Can you give me example ? i dont know about endpoint – Parth Thakkar Aug 6 '15 at 9:14
  • Did the answer help ? – Amit Bangad Aug 11 '15 at 9:30
  • not yet work on it ! – Parth Thakkar Aug 11 '15 at 9:57

You will get CORS error while trying to do that https://en.wikipedia.org/wiki/Cross-origin_resource_sharing

As your browser and Salesforce will give error corresponding to same.

Update - try this in Spring 15. https://developer.salesforce.com/blogs/developer-relations/2015/01/spring-15-preview-cors-force-com-rest-api.html

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.