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
TarentTarent 

REST API

Hi

 

Please tell me about REST API with example?

Navatar_DbSupNavatar_DbSup

The Force.com REST API provides a clean and simple approach to integrating with the Force.com platform. As a developer, you will be able to access your data using simple HTTP methods, using either XML or JSON formats, making this an ideal API for developing mobile applications or external clients.

Example:-

1. Create an Apex class in your instance, by clicking Your Name | Setup | Develop | Apex Classes | New and add this code to your new class:

@RestResource(urlMapping='/Account/*')

global with sharing class MyRestResource {

    @HttpDelete

    global static void doDelete() {

        RestRequest req = RestContext.request;

        RestResponse res = RestContext.response;

        String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);

        Account account = [SELECT Id FROM Account WHERE Id = :accountId];

        delete account;

    }

 

    @HttpGet

    global static Account doGet() {

        RestRequest req = RestContext.request;

        RestResponse res = RestContext.response;

        String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);

        Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE Id = :accountId];

        return result;

    }

 

  @HttpPost

    global static String doPost(String name,

        String phone, String website) {

        Account account = new Account();

        account.Name = name;

        account.phone = phone;

        account.website = website;

        insert account;

        return account.Id;

    }

}

 

2. To call the doGet method from a client, open a command-line window and execute the following cURL command to retrieve an account by ID:

curl -H "Authorization: Bearer sessionId" "https://instance.salesforce.com/services/apexrest/Account/accountId"

Replace sessionId with the <sessionId> element that you noted in the login response.

Replace instance with your <serverUrl> element.

Replace accountId with the ID of an account which exists in your organization.

After calling the doGet method, Salesforce returns a JSON response with data such as the following:

 

{

  "attributes" :

    {

      "type" : "Account",

      "url" : "/services/data/v22.0/sobjects/Account/accountId"

    },

  "Id" : "accountId",

  "Name" : "Acme"

 

}

 

3. Create a file called account.txt to contain the data for the account you will create in the next step.

 

{

  "name" : "Wingo Ducks",

  "phone" : "707-555-1234",

  "website" : "www.wingo.ca.us"

}

 

 

 

for more detail follow below links:

 

http://wiki.developerforce.com/page/Getting_Started_with_the_Force.com_REST_API

http://www.salesforce.com/us/developer/docs/api_rest/index.htm

http://www.salesforce.com/us/developer/docs/api_rest/api_rest.pdf

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

TarentTarent

Thanks Navatar