Thursday 30 August 2012

Adding a Class as a REST Resource - Apex

Prerequisites:
  • Tutorial #1: Creating Warehouse Custom Objects
  • Tutorial #2: Using the Developer Console
  • Tutorial #8: sObjects and the Database
Let’s add a class with two methods and expose it through Apex REST.
  1. In the Developer Console, click the Repository tab.
  2. In the Setup Entity Type section, click Classes, and then click New.
  3. For the class name, enter MerchandiseManager and click OK.
  4. Delete the auto-generated code and add the following.
    @RestResource(urlMapping='/Merchandise/*')
    global with sharing class MerchandiseManager {
      
        @HttpGet
        global static Merchandise__c getMerchandiseById() {
            RestRequest req = RestContext.request;        
            String merchId = req.requestURI.substring(
                                      req.requestURI.lastIndexOf('/')+1);
            Merchandise__c result = 
                           [SELECT Name,Description__c,Price__c,Total_Inventory__c
                            FROM Merchandise__c 
                            WHERE Id = :merchId];
            return result;
        }
      
        @HttpPost
        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,
                Total_Inventory__c=inventory);
            insert m;
            return m.Id;
        }
    }
  5. Click Save.

Tell Me More...

  • The class is global and defined with the @RestResource(urlMapping='/Invoice_Statement__c/*') annotation. Any Apexclass 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 URLhttps://instance.salesforce.com/services/apexrest/. 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.
  • The class contains two global static methods defined with Apex REST annotations. All Apex REST methods must be global static.
  • The first class method, getMerchandiseById, is defined with the @HttpGet annotation.
    • The @HttpGet annotation exposes the method as a REST API that is called when an HTTP GET request is sent from the client.
    • This method returns the merchandise item that corresponds to the ID sent by the client in the request URI.
    • It obtains the request and request URI through the Apex static RestContext class.
    • 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.
  • 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.

2 comments:

  1. How can we pass parameters to createMerchandise() method from client..?

    ReplyDelete
  2. Nice one. nice explanation of creating rest based web-service in salesforce

    ReplyDelete