Integration

Custom Apex WebService using REST

Custom Apex WebService using REST- Salesforce

Custom Apex WebService using REST


We can create custom WebServices either by using REST or SOAP. This post will explain, how to create Custom Apex WebService using REST and how to test it using REST Explorer.

If you are new to REST API then refer 

http://www.worldofsalesforce.com/2017/04/salesforce-rest-api-with-example.html

 before proceeding.


If you are interested in knowing more about SOAP webServices then refer 

http://www.worldofsalesforce.com/2016/05/creating-custom-webservice-in-salesforce.html

.Lets start with creating a apex class and exposing it as WebService.
Expose a Class as a REST Service:   To expose a class as rest resource you need to define you apex class as Global and define underlying methods as Global static. The class should be annotated with @RestResource(urlMapping='/xxx/')  and method with individual Rest methods eg:- @HttpGet. 

Copy and past the below code to create a Rest Web service class which is responsible for creation ,deletion and query of Account record.


@RestResource(urlMapping='/AccountRest/*')
global with sharing class AccountWebService {

    @HttpGet
    global static Account getAcccById() {
        RestRequest request = RestContext.request;
        // grab the AccountId from the end of the URL
        String AccId = request.requestURI.substring(
          request.requestURI.lastIndexOf('/')+1);
        Account result =  [SELECT Id,Name
                        FROM Account
                        WHERE Id = :AccId];
        return result;
    }

    @HttpPost
    global static ID createAccount(String AccName) {
        Account thisAcc = new Account(
            Name=AccName);
        insert thisAcc;
        return thisAcc.Id;
    }   

    @HttpDelete
    global static void deleteAccount() {
        RestRequest request = RestContext.request;
        String AccId = request.requestURI.substring(
            request.requestURI.lastIndexOf('/')+1);
        Account thisAcc = [SELECT Id FROM Account WHERE Id = :AccId];
        delete thisAcc;
    }   

}


Testing Rest WebService To test the Apex custom Rest WebService you can use Rest Explorer utility built
by me. Click on the below link and login to the instance where you have created the apex class. 

http://www.worldofsalesforce.com/p/world-of-salesforce.html

After logging In you would be presented a screen as shown below to start the rest Test.
Salesforce Rest Explorer

Now we will create a record in salesforce using POST method call. Select POST Radio button on the explorer. Change the URI from /services/data/v39.0 to /services/apexrest/AccountRest/.  
Here AccountRest is the URL mapping define in the Apex class @RestResource annotation.

Now in the method body pass the parameter name of the account as JSON body As. 
{
"AccName": "Testing Rest Webservice"
}

and press execute . See the snapshot below for more clarity.

Salesforce Rest Explorer

Copy the response Id and check in salesforce if the record is created .

Now lets try to fetch the details using GET method.
Click on GET Radio button and append the URI with the ID you want to search account for.
Example: /services/apexrest/AccountRest/0012800001HfIsZAAV 
Replace id in green with your org's account record id. 

Press execute to see the result. This would hit the getAccById method of our class and return the matching account name. 
See the snapshot below: 


Salesforce Rest Explorer

Let me know if you face any issue through comments
  

About Saurabh Dua

0 comments:

Post a Comment

Powered by Blogger.