Wednesday, 13 April 2016

Rest API in Salesforce | Execute Rest API on workbench | Test class for Rest API


Now learning in salesforce to much easy as compare to our time. The Salesforce Trailhead now offers a fun way to learn everything related to Salesforce Integration
1) https://developer.salesforce.com/trailhead/module/apex_integration_services?utm_campaign=trailhead&utm_source=sfdc&utm_medium=chatter-success

Pleae check above Trailhead module for detail knowledge of web service.

REST API

The Force.com REST API lets you integrate with Force.com applications using simple HTTP methods, in either XML or JSON formats, making this an ideal API for developing mobile applications or external clients. Force.com also supports Apex REST, which lets you create Web services on Force.com using Apex





Create REST Apex class


@RestResource(urlMapping='/api/Account/*')
global with sharing class MyFirstRestAPIClass
{
@HttpGet
global static Account doGet()
{
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String AccNumber = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE AccountNumber = :AccNumber ];
return result;
}

@HttpDelete
global static void doDelete()
{
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String AccNumber = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE AccountNumber = :AccNumber ];
delete result;
}

@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;
}

}


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/.


Test Class for REST API

@IsTest
private class MyFirstRestAPIClassTest
{
    static testMethod void testGetMethod()
    {
        Account acc = new Account();
        acc.Name='Test';
        acc.AccountNumber ='12345';
        insert acc;
        
        RestRequest request = new RestRequest();
        request.requestUri ='/services/apexrest/api/Account/12345';
        request.httpMethod = 'GET';
        RestContext.request = request;
        Account acct = MyFirstRestAPIClass.doGet();
        System.assert(acct != null);
        System.assertEquals('Test', acct.Name);
        
    }

    static testMethod void testPostMethod()
    {
        RestRequest request = new RestRequest();
        request.requestUri ='/services/apexrest/api/Account/12345';
        request.httpMethod = 'POST';
        RestContext.request = request;
        String strId = MyFirstRestAPIClass.doPost('Amit','2345678','12345');
        System.assert(strId !=null );
    }

    static testMethod void testDeleteMethod()
    {
        Account acc = new Account();
        acc.Name='Test';
        acc.AccountNumber ='12345';
        insert acc;
        
        RestRequest request = new RestRequest();
        request.requestUri ='/services/apexrest/api/Account/12345';
        request.httpMethod = 'DELETE';
        RestContext.request = request;
        MyFirstRestAPIClass.doDelete();
        
        List<Account> ListAcct = [SELECT Id FROM Account WHERE Id=:acc.id];
        System.assert(ListAcct.size() ==0 );
    }

}


Execute your Apex REST class in workbench

Step 1:- Open https://workbench.developerforce.com/
Step 2:- Select Environment as Production and select the checkbox to agree the terms and condition then select Login with Salesforce.


NOTE:- If you are already login then only above step will work other wise you need to enter user name and password.

Step 3:- In the Workbench tool select Utilities > REST Explorer



Step 4:- In the REST Explorer window paste the following url in the box

Create Account Call
Method :- Post
URL :- /services/apexrest/api/Account
Body:-
{
  "name" : "Test Rest",
  "phone" : "123456789",
  "AccountNumber" : "12345"

}



Get Account Call

Method :- Get
URL :- /services/apexrest/api/Account/12345
Body:- NA




Please check below post for more information
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_rest_methods.htm
2) https://developer.salesforce.com/page/Getting_Started_with_the_Force.com_REST_API





<<PREVIOUS       NEXT>>