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

 before proceeding.

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

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

global with sharing class AccountWebService {

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

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

    global static void deleteAccount() {
        RestRequest request = RestContext.request;
        String AccId = request.requestURI.substring(
        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.

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


  1. It's very useful blog post with inforamtive and insightful content and i had good experience with this information.I have gone through CRS Info Solutions Home which really nice. Learn more details About Us of CRS info solutions. Here you can see the Courses CRS Info Solutions full list. Find Student Registration page and register now.Find this real time DevOps Training and great teaching. Join now on Selenium Training online course. Upskill career with Tableau training by crs info solutions. Latest trending course is Salesforce Lightning training with excellent jobs.

  2. Its a wonderful post and very helpful, thanks for all this information. You are including better information regarding this topic in an effective way. T hank you so much.

    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery

  3. IEEE Project Domain management in software engineering is distinct from traditional project deveopment in that software projects have a unique lifecycle process that requires multiple rounds of testing, updating, and faculty feedback. A IEEE Domain project Final Year Projects for CSE system development life cycle is essentially a phased project model that defines the organizational constraints of a large-scale systems project. The methods used in a IEEE DOmain Project systems development life cycle strategy Project Centers in India provide clearly defined phases of work to plan, design, test, deploy, and maintain information systems.

    This is enough for me. I want to write software that anyone can use, and virtually everyone who has an internet connected device with a screen can use apps written in JavaScript. JavaScript Training in Chennai JavaScript was used for little more than mouse hover animations and little calculations to make static websites feel more interactive. Let’s assume 90% of all websites using JavaScript use it in a trivial way. That still leaves 150 million substantial JavaScript Training in Chennai JavaScript applications.

  4. You should be a part of a contest for one of the highest quality websites on the internet. I am going to highly recommend this web site!
    Click Here for more
    Jio Information Available
    Check 2019-20 List
    Find Helpline Resources


Powered by Blogger.