Tuesday, 27 October 2020

Create Rest API in Salesforce | Execute Rest API on Workbench

 Step 1: Create an Apex class for Rest API.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
@RestResource(urlMapping='/api/Account/*')
global with sharing class MyFirstRestAPIClass
{
    @HttpPost
    global static String doPost(String name,String phone,String AccountNumber ) 
    {
        Account acc = new Account();
        acc.Name = name;
        acc.Phone = phone;
        acc.AccountNumber = AccountNumber ;
        insert acc;
        
        return acc.id;
    }
}

Step 2: Open the Workbench and log in with Salesforce Credential.

Step 3: Click on "Utilities (1)" and then click on the "REST Explorer".



Step 4: Select the POST radio button (2).

Step 5: Use the REST API URL (3).

As you can see, the class is annotated with @RestResource(urlMapping='/api/Account/*).
The base endpoint for Apex REST is
https://instance.salesforce.com/services/apexrest/.

The URL mapping is appended to the base endpoint to form the endpoint for your REST service.
For example, in the class example, the REST endpoint is
https://instance.salesforce.com/services/apexrest/api/Account/.

Step 6: Use the Request Body in JSON format and click on the Execute button (4).

{
  "name" : "My Rest API Account",
  "phone" : "9876543210",
  "AccountNumber" : "54321"
}

Step 7: Finally you will get the response with status code 200 (5).

Thanks for spending your valuable time with us.

No comments:

Post a Comment

Total Pageviews