Add a Class as a REST Resource | Techila
single,single-post,postid-824,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive

Add a Class as a REST Resource

Add a Class as a REST Resource

Let us add a class with 2 methods and expose it through Apex REST.

1.       In the Developer Console, click File | New | Apex Class.

2.       For the class name, enter MerchandiseManager and click OK.

3.       Delete the auto-generated code and add the following.


global with sharing class MerchandiseManager {


global static Merchandise__c getMerchandiseById() {

 RestRequest req = RestContext.request;    

 String merchId = req.requestURI.substring(


Merchandise__c result = [SELECT Name,Description__c,Price__c,Total_Inventory__c FROM Merchandise__c  

WHERE Id = :merchId];

return result;



 global static String createMerchandise(String name, String description, Decimal price, Double inventory) {

 Merchandise__c m = new Merchandise__c( Name=name,  Description__c=description, Price__c=price,


  insert m;

  return m.Id;



4.       Click File | Save

  1. The class is global and defined with the @RestResource(urlMapping=’/Merchandise/*’) annotation. Any Apex class you want to expose as a REST API must be global and annotated with the @RestResource annotation. The parameter of the@RestResource annotation, urlMapping, is used to uniquely identify your resource and is relative to the base URL The base URL and the urlMapping value form the URI that the client sends in a REST request. In this case, the URL mapping contains the asterisk wildcard character, which means that the resource URI can contain any value after /Merchandise/. In Step 3 of this tutorial, we’ll be appending an ID value to the URI for the record to retrieve.
  2. The class contains 2 global static methods defined with Apex REST annotations. All Apex REST methods must be global static.
  3. The first class method, getMerchandiseById, is defined with the @HttpGet annotation.
    1. The @HttpGet annotation exposes the method as a REST API that is called when an HTTP GET request is sent from the client.
    2. This method returns the merchandise item that corresponds to the ID sent by the client in the request URI.
    3. It obtains the request and request URI through the Apex static RestContext class.
    4. It then parses the URI to find the value passed in after the last / character and performs a SOQL query to retrieve the merchandise record with this ID. Finally, it returns this record.
  4. The second class method, createMerchandise, is defined with the @HttpPost annotation. This annotation exposes the method as a REST API and is called when an HTTP POST request is sent from the client. This method creates a merchandise record using the specified data sent by the client. It calls the insert DML operation to insert the new record in the database and returns the ID of the new merchandise record to the client.


“Explore – Techila Global Services, A Salesforce development company”

Author: techila

No Comments

Post A Comment